You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.7 KiB

  1. /*
  2. * meli
  3. *
  4. * Copyright 2019 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. use std::fs::OpenOptions;
  22. use std::io::{Read, Write};
  23. use std::sync::{Arc, Mutex};
  24. thread_local!(static CMD_HISTORY_FILE: Arc<Mutex<std::fs::File>> = Arc::new(Mutex::new({
  25. let data_dir = xdg::BaseDirectories::with_prefix("meli").unwrap();
  26. OpenOptions::new().append(true) /* writes will append to a file instead of overwriting previous contents */
  27. .create(true) /* a new file will be created if the file does not yet already exist.*/
  28. .read(true)
  29. .open(data_dir.place_data_file("cmd_history").unwrap()).unwrap()
  30. })));
  31. pub fn log_cmd(mut cmd: String) {
  32. CMD_HISTORY_FILE.with(|f| {
  33. cmd.push('\n');
  34. f.lock().unwrap().write_all(cmd.as_bytes()).unwrap();
  35. });
  36. }
  37. pub fn old_cmd_history() -> Vec<String> {
  38. let mut ret = Vec::new();
  39. CMD_HISTORY_FILE.with(|f| {
  40. let mut old_history = String::new();
  41. f.lock().unwrap().read_to_string(&mut old_history).unwrap();
  42. ret.extend(old_history.lines().map(|s| s.to_string()));
  43. });
  44. ret
  45. }