Skip to content

Commit

Permalink
write history
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Nov 23, 2024
1 parent d497772 commit e48477e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 57 deletions.
34 changes: 13 additions & 21 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ impl Histories {
}
}

// fn from(histories: histories::Histories) -> Self {
// let mut result: Vec<History> = vec![];
// for h in histories.histories {
// result.push(History::from(h));
// }
// Self { histories: result }
// }
fn from(histories: histories::Histories) -> Self {
let mut result: Vec<History> = vec![];
for h in histories.histories {
result.push(History::from(h));
}
Self { histories: result }
}

pub fn into(self) -> histories::Histories {
let mut result: Vec<histories::History> = vec![];
Expand Down Expand Up @@ -118,27 +118,19 @@ pub fn parse_history(content: String) -> Result<Histories> {
}

pub fn store_history(
current_working_directory: PathBuf,
history_directory_path: PathBuf,
history_file_name: String,
new_history: histories::History,
new_history: histories::Histories,
) -> Result<()> {
let mut all_histories = Histories::get_history();

for (i, history) in all_histories.clone().histories.iter().enumerate() {
if history.path == current_working_directory {
all_histories.histories[i] = History::from(new_history.clone());
}
}

// TODO: 2. cwdの履歴を更新する

if !history_directory_path.is_dir() {
fs::create_dir_all(history_directory_path.clone())?;
}
// TODO: 3. historiesを保存する
let mut history_file = File::create(history_directory_path.join(history_file_name))?;
history_file.write_all(toml::to_string(&all_histories).unwrap().as_bytes())?;
history_file.write_all(
toml::to_string(&Histories::from(new_history))
.unwrap()
.as_bytes(),
)?;
history_file.flush()?;

Ok(())
Expand Down
52 changes: 29 additions & 23 deletions src/model/histories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,36 @@ impl Histories {
// histories
// }

// pub fn append(&self, path: &PathBuf, executed_target: &str) -> Option<Self> {
// let mut new_histories = self.histories.clone();
//
// new_histories
// .iter()
// .position(|h| h.path == *path)
// .map(|index| {
// let new_history = new_histories[index].append(executed_target.to_string());
// new_histories[index] = new_history;
//
// Self {
// histories: new_histories,
// }
// })
// }
pub fn append(
&self,
current_dir: PathBuf,
histories_before_update: Vec<command::Command>,
command: command::Command,
) -> Self {
let new_history_commands: Vec<HistoryCommand> = [vec![command], histories_before_update]
.concat()
.iter()
.map(|c| HistoryCommand::from(c.clone()))
.collect();
let history = History {
path: current_dir.clone(),
commands: new_history_commands,
};

// pub fn to_tuple(&self) -> Vec<(PathBuf, Vec<String>)> {
// let mut result = Vec::new();
//
// for history in &self.histories {
// result.push((history.path.clone(), history.executed_targets.clone()));
// }
// result
// }
let mut new_histories = self.histories.clone();
match new_histories.iter().position(|h| h.path == history.path) {
Some(index) => {
new_histories[index] = history;
}
None => {
new_histories.insert(0, history);
}
}

Histories {
histories: new_histories,
}
}
}

// TODO(#321): should return Result not Option(returns when it fails to get the home dir)
Expand Down
22 changes: 9 additions & 13 deletions src/usecase/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ fn update(model: &mut Model, message: Option<Message>) {
Some(Message::ExecuteTarget) => {
if let Some(command) = s.get_selected_target() {
// TODO: make this a method of SelectTargetState
s.store_history(&command);
s.store_history(command.clone());
// TODO: s.runners[0]ではなくcommand.runner_type を利用する
let executor: runner::Runner = s.runners[0].clone();

model.transition_to_execute_target_state(executor, command);
Expand Down Expand Up @@ -562,24 +563,19 @@ impl SelectTargetState<'_> {
self.search_text_area.0.input(key_event);
}

fn store_history(&self, command: &command::Command) {
fn store_history(&self, command: command::Command) {
// NOTE: self.get_selected_target should be called before self.append_history.
// Because self.histories_list_state.selected keeps the selected index of the history list
// before update.
if let Some((dir, file_name)) = history_file_path() {
let new_history_commands: Vec<histories::HistoryCommand> =
[vec![command.clone()], self.histories.clone()]
.concat()
.iter()
.map(|c| histories::HistoryCommand::from(c.clone()))
.collect();
let history = histories::History {
path: self.current_dir.clone(),
commands: new_history_commands,
};
let all_histories = toml::Histories::get_history().into().append(
self.current_dir.clone(),
self.histories.clone(),
command,
);

// TODO: handle error
let _ = toml::store_history(self.current_dir.clone(), dir, file_name, history);
let _ = toml::store_history(dir, file_name, all_histories);
};
}

Expand Down

0 comments on commit e48477e

Please sign in to comment.