diff --git a/rustfmt.toml b/rustfmt.toml index fd65c78d..a31f84ba 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,3 +1,3 @@ -edition = "2021" -max_width = 120 -fn_call_width = 120 +# edition = "2021" +# max_width = 120 +# fn_call_width = 120 diff --git a/src/file/toml.rs b/src/file/toml.rs index fb60503c..3f188f7d 100644 --- a/src/file/toml.rs +++ b/src/file/toml.rs @@ -133,11 +133,17 @@ pub fn history_file_path() -> Option<(PathBuf, String)> { Ok(_) => { // When testing let cwd = env::current_dir().unwrap(); - Some((cwd.join(PathBuf::from("test_data/history")), HISTORY_FILE_NAME.to_string())) - } - _ => { - home_dir().map(|home_dir| (home_dir.join(PathBuf::from(".config/fzf-make")), HISTORY_FILE_NAME.to_string())) + Some(( + cwd.join(PathBuf::from("test_data/history")), + HISTORY_FILE_NAME.to_string(), + )) } + _ => home_dir().map(|home_dir| { + ( + home_dir.join(PathBuf::from(".config/fzf-make")), + HISTORY_FILE_NAME.to_string(), + ) + }), } } @@ -155,7 +161,11 @@ pub fn create_or_update_history_file( fs::create_dir_all(history_directory_path.clone())?; } let mut history_file = File::create(history_directory_path.join(history_file_name))?; - history_file.write_all(toml::to_string(&Histories::from(new_history)).unwrap().as_bytes())?; + history_file.write_all( + toml::to_string(&Histories::from(new_history)) + .unwrap() + .as_bytes(), + )?; history_file.flush()?; Ok(()) @@ -262,7 +272,12 @@ args = "app1 build" for case in cases { match case.expect { - Ok(v) => assert_eq!(v, parse_history(case.content).unwrap(), "\nFailed: 🚨{:?}🚨\n", case.title,), + Ok(v) => assert_eq!( + v, + parse_history(case.content).unwrap(), + "\nFailed: 🚨{:?}🚨\n", + case.title, + ), Err(e) => assert_eq!( e.to_string(), parse_history(case.content).unwrap_err().to_string(), diff --git a/src/file/toml_old.rs b/src/file/toml_old.rs index b92e4a95..758f7169 100644 --- a/src/file/toml_old.rs +++ b/src/file/toml_old.rs @@ -15,7 +15,10 @@ impl Histories { for h in self.history.clone() { let mut commands: Vec = vec![]; for c in h.executed_targets { - commands.push(fzf_make_toml::HistoryCommand::new(model::runner_type::RunnerType::Make, c)); + commands.push(fzf_make_toml::HistoryCommand::new( + model::runner_type::RunnerType::Make, + c, + )); } // NOTE: In old format, the path includes the file name but new format does not. let mut makefile_path = PathBuf::from(h.path); @@ -66,14 +69,25 @@ mod test { after: fzf_make_toml::Histories::new(vec![fzf_make_toml::History::new( PathBuf::from("path"), vec![ - fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "command1".to_string()), - fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "command2".to_string()), + fzf_make_toml::HistoryCommand::new( + runner_type::RunnerType::Make, + "command1".to_string(), + ), + fzf_make_toml::HistoryCommand::new( + runner_type::RunnerType::Make, + "command2".to_string(), + ), ], )]), }]; for case in cases { - assert_eq!(case.after, case.before.into_histories(), "\nFailed: 🚨{:?}🚨\n", case.title,) + assert_eq!( + case.after, + case.before.into_histories(), + "\nFailed: 🚨{:?}🚨\n", + case.title, + ) } } @@ -131,7 +145,12 @@ executed-targets = ["run", "echo1"] for case in cases { match case.expect { - Ok(e) => assert_eq!(e, parse_history(case.content).unwrap(), "\nFailed: 🚨{:?}🚨\n", case.title,), + Ok(e) => assert_eq!( + e, + parse_history(case.content).unwrap(), + "\nFailed: 🚨{:?}🚨\n", + case.title, + ), Err(err) => assert_eq!( err.to_string(), parse_history(case.content).unwrap_err().to_string(), diff --git a/src/model/command.rs b/src/model/command.rs index f52125c9..33b38999 100644 --- a/src/model/command.rs +++ b/src/model/command.rs @@ -10,7 +10,12 @@ pub struct Command { } impl Command { - pub fn new(runner_type: runner_type::RunnerType, args: String, file_path: PathBuf, line_number: u32) -> Self { + pub fn new( + runner_type: runner_type::RunnerType, + args: String, + file_path: PathBuf, + line_number: u32, + ) -> Self { Self { runner_type, args, diff --git a/src/model/histories.rs b/src/model/histories.rs index 8cdbec75..63251ed7 100644 --- a/src/model/histories.rs +++ b/src/model/histories.rs @@ -24,7 +24,10 @@ impl Histories { // Update the whole histories. let mut new_histories = self.histories.clone(); - match new_histories.iter().position(|h| h.path == new_history.path) { + match new_histories + .iter() + .position(|h| h.path == new_history.path) + { Some(index) => { new_histories[index] = new_history; } @@ -196,7 +199,8 @@ mod test { for case in cases { assert_eq!( case.after, - case.before.append(path_to_append.clone(), case.command_to_append), + case.before + .append(path_to_append.clone(), case.command_to_append), "\nFailed: 🚨{:?}🚨\n", case.title, ) @@ -417,7 +421,12 @@ mod test { ]; for case in cases { - assert_eq!(case.after, case.before.append(case.command_to_append), "\nFailed: 🚨{:?}🚨\n", case.title,) + assert_eq!( + case.after, + case.before.append(case.command_to_append), + "\nFailed: 🚨{:?}🚨\n", + case.title, + ) } } } diff --git a/src/model/js_package_manager/js_package_manager_main.rs b/src/model/js_package_manager/js_package_manager_main.rs index 18d9cd16..b49799e1 100644 --- a/src/model/js_package_manager/js_package_manager_main.rs +++ b/src/model/js_package_manager/js_package_manager_main.rs @@ -81,7 +81,8 @@ impl JsPackageManager { if let Some(object) = v.as_object() { for (k, v) in object { let args = k.to_string(); - let line_number = files.line_index(file, k.start() as u32).number().to_usize() as u32; + let line_number = + files.line_index(file, k.start() as u32).number().to_usize() as u32; if let Some(v) = v.as_string() { result.push((args, v.to_string(), line_number)); } @@ -96,7 +97,9 @@ impl JsPackageManager { pub fn get_js_package_manager_runner(current_dir: PathBuf) -> Option { let entries = fs::read_dir(current_dir.clone()).unwrap(); - let file_names = entries.map(|e| e.unwrap().file_name().into_string().unwrap()).collect(); + let file_names = entries + .map(|e| e.unwrap().file_name().into_string().unwrap()) + .collect(); JsPackageManager::new(current_dir, file_names) } diff --git a/src/model/js_package_manager/pnpm.rs b/src/model/js_package_manager/pnpm.rs index 679ddc5b..44afb0f6 100644 --- a/src/model/js_package_manager/pnpm.rs +++ b/src/model/js_package_manager/pnpm.rs @@ -47,23 +47,30 @@ impl Pnpm { } pub fn new(current_dir: PathBuf, cwd_file_names: Vec) -> Option { - let package_json_exist = Iterator::find(&mut cwd_file_names.iter(), |&f| f == js::METADATA_FILE_NAME); - let lockfile_exist_in_current_dir = Iterator::find(&mut cwd_file_names.iter(), |&f| f == PNPM_LOCKFILE_NAME); + let package_json_exist = + Iterator::find(&mut cwd_file_names.iter(), |&f| f == js::METADATA_FILE_NAME); + let lockfile_exist_in_current_dir = + Iterator::find(&mut cwd_file_names.iter(), |&f| f == PNPM_LOCKFILE_NAME); let lockfile_exist_in_ancestors = file_util::find_file_in_ancestors(current_dir.clone(), vec![PNPM_LOCKFILE_NAME]); - match (package_json_exist, lockfile_exist_in_current_dir, lockfile_exist_in_ancestors) { + match ( + package_json_exist, + lockfile_exist_in_current_dir, + lockfile_exist_in_ancestors, + ) { (None, _, _) => None, - (Some(_), Some(_), _) => Pnpm::collect_workspace_scripts(current_dir.clone()).map(|commands| Pnpm { - path: current_dir, - commands, - }), - (Some(_), None, Some(_)) => { - Self::collect_scripts_in_package_json(current_dir.clone()).map(|commands| Pnpm { + (Some(_), Some(_), _) => { + Pnpm::collect_workspace_scripts(current_dir.clone()).map(|commands| Pnpm { path: current_dir, commands, }) } + (Some(_), None, Some(_)) => Self::collect_scripts_in_package_json(current_dir.clone()) + .map(|commands| Pnpm { + path: current_dir, + commands, + }), // Not a workspace children && not a workspace root // In this case, package manager can not be determined. (Some(_), None, None) => None, @@ -96,7 +103,9 @@ impl Pnpm { continue; } result.push(command::Command::new( - runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Pnpm), + runner_type::RunnerType::JsPackageManager( + runner_type::JsPackageManager::Pnpm, + ), // pnpm executes workspace script following format: `pnpm --filter {package_name} {script_name}` // e.g. `pnpm --filter app4 build` format!("--filter {} {}", name.clone(), key.as_str()), @@ -127,7 +136,9 @@ impl Pnpm { .filter(|(_, value, _)| !Self::use_filtering(value.to_string())) .map(|(key, _value, line_number)| { command::Command::new( - runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Pnpm), + runner_type::RunnerType::JsPackageManager( + runner_type::JsPackageManager::Pnpm, + ), key.to_string(), current_dir.clone().join(js::METADATA_FILE_NAME), *line_number, @@ -153,7 +164,10 @@ impl Pnpm { let output = String::from_utf8(output.stdout)?; // split by newline to remove unnecessary lines. - let lines = output.split("\n").filter(|l| !l.is_empty()).collect::>(); + let lines = output + .split("\n") + .filter(|l| !l.is_empty()) + .collect::>(); Ok(lines .iter() @@ -193,16 +207,34 @@ mod test { assert_eq!(true, Pnpm::use_filtering("pnpm -F app1".to_string())); assert_eq!(true, Pnpm::use_filtering("pnpm -F \"app1\"".to_string())); assert_eq!(true, Pnpm::use_filtering("pnpm --filter app2".to_string())); - assert_eq!(true, Pnpm::use_filtering("pnpm -r --filter app3".to_string())); - assert_eq!(true, Pnpm::use_filtering("pnpm -C packages/app3".to_string())); - assert_eq!(true, Pnpm::use_filtering("pnpm --dir packages/app3".to_string())); + assert_eq!( + true, + Pnpm::use_filtering("pnpm -r --filter app3".to_string()) + ); + assert_eq!( + true, + Pnpm::use_filtering("pnpm -C packages/app3".to_string()) + ); + assert_eq!( + true, + Pnpm::use_filtering("pnpm --dir packages/app3".to_string()) + ); assert_eq!(true, Pnpm::use_filtering("pnpm -F".to_string())); assert_eq!(true, Pnpm::use_filtering("pnpm --filter".to_string())); - assert_eq!(false, Pnpm::use_filtering("pnpm -C packages/app1 run test".to_string())); - assert_eq!(false, Pnpm::use_filtering("pnpm --filter app1 run test".to_string())); + assert_eq!( + false, + Pnpm::use_filtering("pnpm -C packages/app1 run test".to_string()) + ); + assert_eq!( + false, + Pnpm::use_filtering("pnpm --filter app1 run test".to_string()) + ); assert_eq!(false, Pnpm::use_filtering("yarn run".to_string())); assert_eq!(false, Pnpm::use_filtering("pnpm run".to_string())); assert_eq!(false, Pnpm::use_filtering("pnpm -r hoge".to_string())); - assert_eq!(false, Pnpm::use_filtering("yarn -r --filter app3".to_string())); + assert_eq!( + false, + Pnpm::use_filtering("yarn -r --filter app3".to_string()) + ); } } diff --git a/src/model/js_package_manager/yarn.rs b/src/model/js_package_manager/yarn.rs index 59dd78c1..2525d47e 100644 --- a/src/model/js_package_manager/yarn.rs +++ b/src/model/js_package_manager/yarn.rs @@ -130,10 +130,14 @@ impl Yarn { if let Ok(workspace_package_json_paths) = package_json_in_workspace { for path in workspace_package_json_paths { if let Ok(c) = path_to_content::path_to_content(&path) { - if let Some((name, parsing_result)) = js::JsPackageManager::parse_package_json(&c) { + if let Some((name, parsing_result)) = + js::JsPackageManager::parse_package_json(&c) + { for (key, _, line_number) in parsing_result { result.push(command::Command::new( - runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn), + runner_type::RunnerType::JsPackageManager( + runner_type::JsPackageManager::Yarn, + ), // yarn executes workspace script following format: `yarn workspace {package_name} {script_name}` // e.g. `yarn workspace app4 build` format!("workspace {} {}", name.clone(), key.as_str()), @@ -164,7 +168,9 @@ impl Yarn { .iter() .map(|(key, _value, line_number)| { command::Command::new( - runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn), + runner_type::RunnerType::JsPackageManager( + runner_type::JsPackageManager::Yarn, + ), key.to_string(), current_dir.clone().join(js::METADATA_FILE_NAME), *line_number, diff --git a/src/model/just/just_main.rs b/src/model/just/just_main.rs index 72aec16e..03796eed 100644 --- a/src/model/just/just_main.rs +++ b/src/model/just/just_main.rs @@ -74,7 +74,10 @@ impl Just { } fn get_command(&self, command: command::Command) -> Option { - self.to_commands().iter().find(|c| **c == command).map(|_| command) + self.to_commands() + .iter() + .find(|c| **c == command) + .map(|_| command) } fn find_justfile(current_dir: PathBuf) -> Option { @@ -105,12 +108,14 @@ impl Just { 'recipe: for recipes_and_its_siblings in tree.root_node().named_children(&mut tree.walk()) { if recipes_and_its_siblings.kind() == "recipe" { let mut should_skip = false; - recipes_and_its_siblings.children(&mut tree.walk()).for_each(|attr| { - let attr_name = &source_code[attr.byte_range()]; - if attr_name.contains("private") { - should_skip = true; - } - }); + recipes_and_its_siblings + .children(&mut tree.walk()) + .for_each(|attr| { + let attr_name = &source_code[attr.byte_range()]; + if attr_name.contains("private") { + should_skip = true; + } + }); if should_skip { continue; } @@ -278,7 +283,8 @@ clippy: ]; for case in cases { - let commands = Just::parse_justfile(PathBuf::from("justfile"), case.source_code.to_string()); + let commands = + Just::parse_justfile(PathBuf::from("justfile"), case.source_code.to_string()); assert_eq!(commands, case.expected, "{}", case.name); } } diff --git a/src/model/make/make_main.rs b/src/model/make/make_main.rs index fc9b65bd..108ccca7 100644 --- a/src/model/make/make_main.rs +++ b/src/model/make/make_main.rs @@ -69,7 +69,10 @@ impl Make { } fn get_command(&self, command: command::Command) -> Option { - self.to_commands().iter().find(|c| **c == command).map(|_| command) + self.to_commands() + .iter() + .find(|c| **c == command) + .map(|_| command) } // I gave up writing tests using temp_dir because it was too difficult (it was necessary to change the implementation to some extent). @@ -128,9 +131,24 @@ impl Make { path: env::current_dir().unwrap().join(Path::new("Test.mk")), include_files: vec![], targets: Targets(vec![ - command::Command::new(runner_type::RunnerType::Make, "target0".to_string(), PathBuf::from(""), 1), - command::Command::new(runner_type::RunnerType::Make, "target1".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "target2".to_string(), PathBuf::from(""), 7), + command::Command::new( + runner_type::RunnerType::Make, + "target0".to_string(), + PathBuf::from(""), + 1, + ), + command::Command::new( + runner_type::RunnerType::Make, + "target1".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "target2".to_string(), + PathBuf::from(""), + 7, + ), ]), } } @@ -237,7 +255,10 @@ mod test { assert_eq!( expect, - Make::specify_makefile_name(env::current_dir().unwrap(), tmp_dir.to_string_lossy().to_string()), + Make::specify_makefile_name( + env::current_dir().unwrap(), + tmp_dir.to_string_lossy().to_string() + ), "\nFailed: 🚨{:?}🚨\n", case.title, ); @@ -268,13 +289,33 @@ mod test { path: Path::new("path").to_path_buf(), include_files: vec![], targets: Targets(vec![ - command::Command::new(runner_type::RunnerType::Make, "test".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "run".to_string(), PathBuf::from(""), 4), + command::Command::new( + runner_type::RunnerType::Make, + "test".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "run".to_string(), + PathBuf::from(""), + 4, + ), ]), }, expect: vec![ - command::Command::new(runner_type::RunnerType::Make, "test".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "run".to_string(), PathBuf::from(""), 4), + command::Command::new( + runner_type::RunnerType::Make, + "test".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "run".to_string(), + PathBuf::from(""), + 4, + ), ], }, Case { @@ -337,25 +378,80 @@ mod test { }, ], targets: Targets(vec![ - command::Command::new(runner_type::RunnerType::Make, "test1".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "run1".to_string(), PathBuf::from(""), 4), + command::Command::new( + runner_type::RunnerType::Make, + "test1".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "run1".to_string(), + PathBuf::from(""), + 4, + ), ]), }, expect: vec![ - command::Command::new(runner_type::RunnerType::Make, "test1".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "run1".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "test2".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "run2".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "test2-1".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "run2-1".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "test3".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "run3".to_string(), PathBuf::from(""), 4), + command::Command::new( + runner_type::RunnerType::Make, + "test1".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "run1".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "test2".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "run2".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "test2-1".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "run2-1".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "test3".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "run3".to_string(), + PathBuf::from(""), + 4, + ), ], }, ]; for case in cases { - assert_eq!(case.expect, case.makefile.to_commands(), "\nFailed: 🚨{:?}🚨\n", case.title,) + assert_eq!( + case.expect, + case.makefile.to_commands(), + "\nFailed: 🚨{:?}🚨\n", + case.title, + ) } } @@ -426,22 +522,34 @@ mod test { Case { title: "include one.mk two.mk", line: "include one.mk two.mk", - expect: Some(vec![Path::new("one.mk").to_path_buf(), Path::new("two.mk").to_path_buf()]), + expect: Some(vec![ + Path::new("one.mk").to_path_buf(), + Path::new("two.mk").to_path_buf(), + ]), }, Case { title: "-include", line: "-include one.mk two.mk", - expect: Some(vec![Path::new("one.mk").to_path_buf(), Path::new("two.mk").to_path_buf()]), + expect: Some(vec![ + Path::new("one.mk").to_path_buf(), + Path::new("two.mk").to_path_buf(), + ]), }, Case { title: "sinclude", line: "sinclude hoge.mk fuga.mk", - expect: Some(vec![Path::new("hoge.mk").to_path_buf(), Path::new("fuga.mk").to_path_buf()]), + expect: Some(vec![ + Path::new("hoge.mk").to_path_buf(), + Path::new("fuga.mk").to_path_buf(), + ]), }, Case { title: " include one.mk two.mk", line: " include one.mk two.mk", - expect: Some(vec![Path::new("one.mk").to_path_buf(), Path::new("two.mk").to_path_buf()]), + expect: Some(vec![ + Path::new("one.mk").to_path_buf(), + Path::new("two.mk").to_path_buf(), + ]), }, Case { title: "include one.mk two.mk(tab is not allowed)", @@ -456,7 +564,10 @@ mod test { Case { title: "include comment", line: "include one.mk two.mk # three.mk", - expect: Some(vec![Path::new("one.mk").to_path_buf(), Path::new("two.mk").to_path_buf()]), + expect: Some(vec![ + Path::new("one.mk").to_path_buf(), + Path::new("two.mk").to_path_buf(), + ]), }, Case { title: "# include one.mk two.mk # three.mk", diff --git a/src/model/make/target.rs b/src/model/make/target.rs index 1e5fd2fe..77ebd036 100644 --- a/src/model/make/target.rs +++ b/src/model/make/target.rs @@ -21,8 +21,12 @@ impl Targets { LineType::Normal => { if define_block_depth == 0 { if let Some(t) = line_to_target(line.to_string()) { - let command = - command::Command::new(runner_type::RunnerType::Make, t, path.clone(), i as u32 + 1); + let command = command::Command::new( + runner_type::RunnerType::Make, + t, + path.clone(), + i as u32 + 1, + ); result.push(command); } } @@ -68,9 +72,15 @@ fn get_line_type(line: &str) -> LineType { fn line_to_target(line: String) -> Option { let regex = Regex::new(r"^ *[^.#\s ][^=]*:[^=]*$").unwrap(); - regex - .find(line.as_str()) - .map(|m| m.as_str().to_string().split_once(':').unwrap().0.trim().to_string()) + regex.find(line.as_str()).map(|m| { + m.as_str() + .to_string() + .split_once(':') + .unwrap() + .0 + .trim() + .to_string() + }) } #[cfg(test)] @@ -107,11 +117,36 @@ test: # run test echo: @echo good", expect: Targets(vec![ - command::Command::new(runner_type::RunnerType::Make, "run".to_string(), PathBuf::from(""), 3), - command::Command::new(runner_type::RunnerType::Make, "build".to_string(), PathBuf::from(""), 6), - command::Command::new(runner_type::RunnerType::Make, "check".to_string(), PathBuf::from(""), 9), - command::Command::new(runner_type::RunnerType::Make, "test".to_string(), PathBuf::from(""), 13), - command::Command::new(runner_type::RunnerType::Make, "echo".to_string(), PathBuf::from(""), 16), + command::Command::new( + runner_type::RunnerType::Make, + "run".to_string(), + PathBuf::from(""), + 3, + ), + command::Command::new( + runner_type::RunnerType::Make, + "build".to_string(), + PathBuf::from(""), + 6, + ), + command::Command::new( + runner_type::RunnerType::Make, + "check".to_string(), + PathBuf::from(""), + 9, + ), + command::Command::new( + runner_type::RunnerType::Make, + "test".to_string(), + PathBuf::from(""), + 13, + ), + command::Command::new( + runner_type::RunnerType::Make, + "echo".to_string(), + PathBuf::from(""), + 16, + ), ]), }, Case { @@ -126,8 +161,18 @@ clone: build: @cargo build", expect: Targets(vec![ - command::Command::new(runner_type::RunnerType::Make, "clone".to_string(), PathBuf::from(""), 4), - command::Command::new(runner_type::RunnerType::Make, "build".to_string(), PathBuf::from(""), 7), + command::Command::new( + runner_type::RunnerType::Make, + "clone".to_string(), + PathBuf::from(""), + 4, + ), + command::Command::new( + runner_type::RunnerType::Make, + "build".to_string(), + PathBuf::from(""), + 7, + ), ]), }, Case { @@ -149,8 +194,18 @@ endef my_script: $(file >my_script,$(script-block))\n", expect: Targets(vec![ - command::Command::new(runner_type::RunnerType::Make, "all".to_string(), PathBuf::from(""), 3), - command::Command::new(runner_type::RunnerType::Make, "my_script".to_string(), PathBuf::from(""), 9), + command::Command::new( + runner_type::RunnerType::Make, + "all".to_string(), + PathBuf::from(""), + 3, + ), + command::Command::new( + runner_type::RunnerType::Make, + "my_script".to_string(), + PathBuf::from(""), + 9, + ), ]), }, Case { @@ -231,7 +286,12 @@ endef ]; for case in cases { - assert_eq!(case.expect, get_line_type(case.line), "\nFailed: 🚨{:?}🚨\n", case.title,); + assert_eq!( + case.expect, + get_line_type(case.line), + "\nFailed: 🚨{:?}🚨\n", + case.title, + ); } } diff --git a/src/model/runner_type.rs b/src/model/runner_type.rs index a78613ad..bf290d0c 100644 --- a/src/model/runner_type.rs +++ b/src/model/runner_type.rs @@ -31,8 +31,12 @@ impl RunnerType { match runner { runner::Runner::MakeCommand(_) => RunnerType::Make, runner::Runner::JsPackageManager(js) => match js { - js::JsPackageManager::JsPnpm(_) => RunnerType::JsPackageManager(JsPackageManager::Pnpm), - js::JsPackageManager::JsYarn(_) => RunnerType::JsPackageManager(JsPackageManager::Yarn), + js::JsPackageManager::JsPnpm(_) => { + RunnerType::JsPackageManager(JsPackageManager::Pnpm) + } + js::JsPackageManager::JsYarn(_) => { + RunnerType::JsPackageManager(JsPackageManager::Yarn) + } }, runner::Runner::Just(_) => RunnerType::Just, } @@ -84,8 +88,12 @@ impl Serialize for RunnerType { { match self { RunnerType::Make => serializer.serialize_str("make"), - RunnerType::JsPackageManager(JsPackageManager::Pnpm) => serializer.serialize_str("pnpm"), - RunnerType::JsPackageManager(JsPackageManager::Yarn) => serializer.serialize_str("yarn"), + RunnerType::JsPackageManager(JsPackageManager::Pnpm) => { + serializer.serialize_str("pnpm") + } + RunnerType::JsPackageManager(JsPackageManager::Yarn) => { + serializer.serialize_str("yarn") + } RunnerType::Just => serializer.serialize_str("just"), } } diff --git a/src/usecase/tui/config.rs b/src/usecase/tui/config.rs index 087a2dba..9109a668 100644 --- a/src/usecase/tui/config.rs +++ b/src/usecase/tui/config.rs @@ -8,7 +8,9 @@ impl Config { } pub fn default() -> Self { - Self { focus_history: false } + Self { + focus_history: false, + } } pub fn get_focus_history(&self) -> bool { diff --git a/src/usecase/tui/ui.rs b/src/usecase/tui/ui.rs index 80168724..945c9cb7 100644 --- a/src/usecase/tui/ui.rs +++ b/src/usecase/tui/ui.rs @@ -57,11 +57,15 @@ pub fn ui(f: &mut Frame, model: &mut Model) { const FG_COLOR_SELECTED: ratatui::style::Color = Color::Rgb(161, 220, 156); const FG_COLOR_NOT_SELECTED: ratatui::style::Color = Color::DarkGray; -const BORDER_STYLE_SELECTED: ratatui::widgets::block::BorderType = ratatui::widgets::BorderType::Thick; -const BORDER_STYLE_NOT_SELECTED: ratatui::widgets::block::BorderType = ratatui::widgets::BorderType::Plain; +const BORDER_STYLE_SELECTED: ratatui::widgets::block::BorderType = + ratatui::widgets::BorderType::Thick; +const BORDER_STYLE_NOT_SELECTED: ratatui::widgets::block::BorderType = + ratatui::widgets::BorderType::Plain; const TITLE_STYLE: ratatui::style::Style = Style::new().add_modifier(Modifier::BOLD); -fn color_and_border_style_for_selectable(is_selected: bool) -> (Color, ratatui::widgets::block::BorderType) { +fn color_and_border_style_for_selectable( + is_selected: bool, +) -> (Color, ratatui::widgets::block::BorderType) { if is_selected { (FG_COLOR_SELECTED, BORDER_STYLE_SELECTED) } else { @@ -71,7 +75,8 @@ fn color_and_border_style_for_selectable(is_selected: bool) -> (Color, ratatui:: fn render_preview_block(model: &SelectCommandState, f: &mut Frame, chunk: ratatui::layout::Rect) { let narrow_down_commands = model.narrow_down_commands(); - let selecting_command = narrow_down_commands.get(model.commands_list_state.selected().unwrap_or(0)); + let selecting_command = + narrow_down_commands.get(model.commands_list_state.selected().unwrap_or(0)); let reader = match selecting_command.map(|c| File::open(c.file_path.clone())) { Some(Ok(file)) => Some(BufReader::new(file)), @@ -79,7 +84,8 @@ fn render_preview_block(model: &SelectCommandState, f: &mut Frame, chunk: ratatu }; let command_row_index = selecting_command.map(|c| c.line_number as usize - 1); let row_count = chunk.rows().count() - 2; // NOTE: chunk.rows().count() includes border lines - let start_index_and_end_index = command_row_index.map(|c| determine_rendering_position(row_count, c)); + let start_index_and_end_index = + command_row_index.map(|c| determine_rendering_position(row_count, c)); // NOTE: due to lifetime, source_lines need to be declared outside of `let lines = {/* ... */}` let source_lines: Vec<_> = match (selecting_command, start_index_and_end_index, reader) { (Some(_), Some((start_index, end_index)), Some(reader)) => { @@ -95,7 +101,11 @@ fn render_preview_block(model: &SelectCommandState, f: &mut Frame, chunk: ratatu }; let lines = { - match (selecting_command, start_index_and_end_index, command_row_index) { + match ( + selecting_command, + start_index_and_end_index, + command_row_index, + ) { (Some(_), Some((start_index, _)), Some(command_row_index)) => { let ss = SyntaxSet::load_defaults_newlines(); // HACK: `ml` is specified intentionally because it highlights `Makefile` and `json` files in a good way.(No unnecessary background color) @@ -126,7 +136,10 @@ fn render_preview_block(model: &SelectCommandState, f: &mut Frame, chunk: ratatu .collect(); // add row number - spans.insert(0, Span::styled(format!("{:5} ", start_index + index + 1), Style::default())); + spans.insert( + 0, + Span::styled(format!("{:5} ", start_index + index + 1), Style::default()), + ); lines.push(Line::from(spans)); } @@ -136,14 +149,17 @@ fn render_preview_block(model: &SelectCommandState, f: &mut Frame, chunk: ratatu } }; - let (fg_color_, border_style) = color_and_border_style_for_selectable(model.current_pane.is_main()); + let (fg_color_, border_style) = + color_and_border_style_for_selectable(model.current_pane.is_main()); let block = Block::default() .borders(Borders::ALL) .border_type(border_style) .border_style(Style::default().fg(fg_color_)) .title(" ✨ Preview ") .title_style(TITLE_STYLE); - let preview_widget = Paragraph::new(lines).wrap(Wrap { trim: false }).block(block); + let preview_widget = Paragraph::new(lines) + .wrap(Wrap { trim: false }) + .block(block); f.render_widget(preview_widget, chunk); } @@ -163,9 +179,17 @@ fn determine_rendering_position(row_count: usize, command_row_index: usize) -> ( } } -fn render_commands_block(model: &mut SelectCommandState, f: &mut Frame, chunk: ratatui::layout::Rect) { +fn render_commands_block( + model: &mut SelectCommandState, + f: &mut Frame, + chunk: ratatui::layout::Rect, +) { f.render_stateful_widget( - commands_block(" 📢 Commands ", model.narrow_down_commands(), model.current_pane.is_main()), + commands_block( + " 📢 Commands ", + model.narrow_down_commands(), + model.current_pane.is_main(), + ), chunk, // NOTE: It is against TEA's way to update the model value on the UI side, but it is unavoidable so it is allowed. &mut model.commands_list_state, @@ -173,7 +197,8 @@ fn render_commands_block(model: &mut SelectCommandState, f: &mut Frame, chunk: r } fn render_input_block(model: &mut SelectCommandState, f: &mut Frame, chunk: ratatui::layout::Rect) { - let (fg_color, border_style) = color_and_border_style_for_selectable(model.current_pane.is_main()); + let (fg_color, border_style) = + color_and_border_style_for_selectable(model.current_pane.is_main()); let block = Block::default() .title(" 🔍 Search ") @@ -193,11 +218,19 @@ fn render_input_block(model: &mut SelectCommandState, f: &mut Frame, chunk: rata f.render_widget(&model.search_text_area.0, chunk); } -fn render_notification_block(model: &mut SelectCommandState, f: &mut Frame, chunk: ratatui::layout::Rect) { +fn render_notification_block( + model: &mut SelectCommandState, + f: &mut Frame, + chunk: ratatui::layout::Rect, +) { let text = match &model.latest_version { Some(has_update) => { if format!("v{}", env!("CARGO_PKG_VERSION")) != *has_update { - format!("📦️ A new release is available! v{} → {}.", env!("CARGO_PKG_VERSION"), has_update.as_str()) + format!( + "📦️ A new release is available! v{} → {}.", + env!("CARGO_PKG_VERSION"), + has_update.as_str() + ) } else { "".to_string() } @@ -210,7 +243,9 @@ fn render_notification_block(model: &mut SelectCommandState, f: &mut Frame, chun .padding(ratatui::widgets::Padding::new(1, 0, 1, 1)) .style(Style::new().add_modifier(Modifier::BOLD).fg(Color::Yellow)); - let key_notes_footer = Paragraph::new(notification).wrap(Wrap { trim: true }).block(block); + let key_notes_footer = Paragraph::new(notification) + .wrap(Wrap { trim: true }) + .block(block); f.render_widget(key_notes_footer, chunk); } @@ -227,9 +262,17 @@ fn render_current_version_block(f: &mut Frame, chunk: ratatui::layout::Rect) { f.render_widget(key_notes_footer, chunk); } -fn render_history_block(model: &mut SelectCommandState, f: &mut Frame, chunk: ratatui::layout::Rect) { +fn render_history_block( + model: &mut SelectCommandState, + f: &mut Frame, + chunk: ratatui::layout::Rect, +) { f.render_stateful_widget( - commands_block(" 📚 History ", model.get_history(), model.current_pane.is_history()), + commands_block( + " 📚 History ", + model.get_history(), + model.current_pane.is_history(), + ), chunk, // NOTE: It is against TEA's way to update the model value on the UI side, but it is unavoidable so it is allowed. &mut model.history_list_state, @@ -253,7 +296,11 @@ fn render_hint_block(model: &mut SelectCommandState, f: &mut Frame, chunk: ratat f.render_widget(key_notes_footer, chunk); } -fn commands_block(title: &str, narrowed_down_commands: Vec, is_current: bool) -> List<'_> { +fn commands_block( + title: &str, + narrowed_down_commands: Vec, + is_current: bool, +) -> List<'_> { let (fg_color, border_style) = color_and_border_style_for_selectable(is_current); let list: Vec = narrowed_down_commands