Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Nov 18, 2024
1 parent b463c4c commit 1ac8e26
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
7 changes: 0 additions & 7 deletions src/model/histories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions src/usecase/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
},
}
Expand Down
55 changes: 41 additions & 14 deletions src/usecase/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<runner::Runner> {
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::HistoryCommand>) -> Histories {
use std::path::Path;

let mut commands: Vec<histories::HistoryCommand> = Vec::new();

for h in history_commands {
Expand Down Expand Up @@ -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)),
}
}
Expand Down

0 comments on commit 1ac8e26

Please sign in to comment.