ui: save execute cmd history to XDG_DATA_DIR

embed
Manos Pitsidianakis 2019-09-12 16:43:46 +03:00
parent f61a43108c
commit e3cd2d4c67
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 52 additions and 1 deletions

View File

@ -637,7 +637,7 @@ impl StatusBar {
id: ComponentId::new_v4(),
auto_complete: AutoComplete::new(Vec::new()),
cmd_history: Vec::new(),
cmd_history: crate::execute::history::old_cmd_history(),
}
}
fn draw_status_bar(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
@ -966,6 +966,7 @@ impl Component for StatusBar {
&& self.cmd_history.last().map(String::as_str)
!= Some(self.ex_buffer.as_str())
{
crate::execute::history::log_cmd(self.ex_buffer.as_str().to_string());
self.cmd_history.push(self.ex_buffer.as_str().to_string());
}
self.ex_buffer.clear();

View File

@ -26,6 +26,7 @@ pub use melib::thread::{SortField, SortOrder};
use nom::{digit, not_line_ending};
use std;
pub mod actions;
pub mod history;
pub use crate::actions::Action::{self, *};
pub use crate::actions::ComposeAction::{self, *};
pub use crate::actions::ListingAction::{self, *};

View File

@ -0,0 +1,49 @@
/*
* meli - ui crate.
*
* Copyright 2019 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::sync::{Arc, Mutex};
thread_local!(static CMD_HISTORY_FILE: Arc<Mutex<std::fs::File>> = Arc::new(Mutex::new({
let data_dir = xdg::BaseDirectories::with_prefix("meli").unwrap();
OpenOptions::new().append(true) /* writes will append to a file instead of overwriting previous contents */
.create(true) /* a new file will be created if the file does not yet already exist.*/
.read(true)
.open(data_dir.place_data_file("cmd_history").unwrap()).unwrap()
})));
pub fn log_cmd(mut cmd: String) {
CMD_HISTORY_FILE.with(|f| {
cmd.push('\n');
f.lock().unwrap().write_all(cmd.as_bytes()).unwrap();
});
}
pub fn old_cmd_history() -> Vec<String> {
let mut ret = Vec::new();
CMD_HISTORY_FILE.with(|f| {
let mut old_history = String::new();
f.lock().unwrap().read_to_string(&mut old_history).unwrap();
ret.extend(old_history.lines().map(|s| s.to_string()));
});
ret
}