summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2019-09-12 16:43:46 +0300
committerManos Pitsidianakis <el13635@mail.ntua.gr>2019-09-15 13:21:15 +0300
commite3cd2d4c678a4f067344cab7f819c45c1957d30c (patch)
tree9a68f3d336e53b26471fee6f874970856e89e043
parentf61a43108c060c8271fa68bb4358f0872bb36f77 (diff)
ui: save execute cmd history to XDG_DATA_DIR
-rw-r--r--ui/src/components/utilities.rs3
-rw-r--r--ui/src/execute.rs1
-rw-r--r--ui/src/execute/history.rs49
3 files changed, 52 insertions, 1 deletions
diff --git a/ui/src/components/utilities.rs b/ui/src/components/utilities.rs
index d1eb7409..14834b8b 100644
--- a/ui/src/components/utilities.rs
+++ b/ui/src/components/utilities.rs
@@ -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();
diff --git a/ui/src/execute.rs b/ui/src/execute.rs
index 1db34cf9..248d22a3 100644
--- a/ui/src/execute.rs
+++ b/ui/src/execute.rs
@@ -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, *};
diff --git a/ui/src/execute/history.rs b/ui/src/execute/history.rs
new file mode 100644
index 00000000..87907d85
--- /dev/null
+++ b/ui/src/execute/history.rs
@@ -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
+}