From 1ac8e26279a4b16a7e2455e10c32acdf10575899 Mon Sep 17 00:00:00 2001 From: kyu08 <49891479+kyu08@users.noreply.github.com> Date: Tue, 19 Nov 2024 00:27:27 +0900 Subject: [PATCH] wip --- src/model/histories.rs | 7 ------ src/usecase/repeat.rs | 16 ++++++------ src/usecase/tui/app.rs | 55 +++++++++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/model/histories.rs b/src/model/histories.rs index c5d699c..3b65b12 100644 --- a/src/model/histories.rs +++ b/src/model/histories.rs @@ -53,13 +53,6 @@ impl Histories { // } // result // } - - pub fn get_latest_command(&self, path: &PathBuf) -> Option<&HistoryCommand> { - self.histories - .iter() - .find(|h| h.path == *path) - .map(|h| h.executed_commands.first())? - } } // TODO(#321): should return Result not Option(returns when it fails to get the home dir) diff --git a/src/usecase/repeat.rs b/src/usecase/repeat.rs index 89e95d8..6ae7bc2 100644 --- a/src/usecase/repeat.rs +++ b/src/usecase/repeat.rs @@ -23,15 +23,13 @@ impl Usecase for Repeat { match Model::new(config::Config::default()) { Err(e) => Err(e), Ok(model) => match model.app_state { - AppState::SelectTarget(state) => { - match ( - state.runners.first(), // TODO: firstではなく最後に実行されたcommandのrunnerを使うべき - state.histories.get_latest_command(&state.current_dir), - ) { - (Some(r), Some(h)) => r.execute(h), - (_, _) => Err(anyhow!("fzf-make has not been executed in this path yet.")), - } - } + AppState::SelectTarget(state) => match state.get_latest_command() { + Some(c) => match state.get_runner(&c.runner_type) { + Some(runner) => runner.execute(c), + None => Err(anyhow!("runner not found.")), + }, + None => Err(anyhow!("fzf-make has not been executed in this path yet.")), + }, _ => Err(anyhow!("Invalid state")), }, } diff --git a/src/usecase/tui/app.rs b/src/usecase/tui/app.rs index 12e51c4..596ed2d 100644 --- a/src/usecase/tui/app.rs +++ b/src/usecase/tui/app.rs @@ -610,8 +610,29 @@ impl SelectTargetState<'_> { self.search_text_area.0.lines().join("") } + pub fn get_latest_command(&self) -> Option<&command::Command> { + Some(self.histories.first()?) + } + + pub fn get_runner(&self, runner_type: &runner_type::RunnerType) -> Option { + for runner in &self.runners { + match (runner_type, runner) { + (runner_type::RunnerType::Make, runner::Runner::MakeCommand(_)) => { + return Some(runner.clone()); + } + (runner_type::RunnerType::Pnpm, runner::Runner::PnpmCommand(_)) => { + return Some(runner.clone()); + } + _ => continue, + } + } + None + } + #[cfg(test)] fn init_histories(history_commands: Vec) -> Histories { + use std::path::Path; + let mut commands: Vec = Vec::new(); for h in history_commands { @@ -639,20 +660,26 @@ impl SelectTargetState<'_> { runners: vec![runner::Runner::MakeCommand(Make::new_for_test())], search_text_area: TextArea_(TextArea::default()), targets_list_state: ListState::with_selected(ListState::default(), Some(0)), - histories: SelectTargetState::init_histories(vec![ - histories::HistoryCommand { - runner_type: runner_type::RunnerType::Make, - name: "history0".to_string(), - }, - histories::HistoryCommand { - runner_type: runner_type::RunnerType::Make, - name: "history1".to_string(), - }, - histories::HistoryCommand { - runner_type: runner_type::RunnerType::Make, - name: "history2".to_string(), - }, - ]), + // histories: SelectTargetState::init_histories(vec![ + // histories::HistoryCommand { + // runner_type: runner_type::RunnerType::Make, + // name: "history0".to_string(), + // }, + // histories::HistoryCommand { + // runner_type: runner_type::RunnerType::Make, + // name: "history1".to_string(), + // }, + // histories::HistoryCommand { + // runner_type: runner_type::RunnerType::Make, + // name: "history2".to_string(), + // }, + // ]), + histories: vec![command::Command { + runner_type: runner_type::RunnerType::Make, + name: "history0".to_string(), + file_name: todo!(), + line_number: todo!(), + }], histories_list_state: ListState::with_selected(ListState::default(), Some(0)), } }