Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions plugins/shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ crate-type = [ "cdylib" ]
[dependencies]
abi_stable = "0.11.1"
anyrun-plugin = { path = "../../anyrun-plugin" }
dirs = "6.0.0"
fuzzy-matcher = "0.3.7"
indexmap = { features = [ "serde" ], version = "2.12.1" }
ron = "0.8.0"
serde = { features = [ "derive" ], version = "1.0.228" }
serde_json = "1.0.149"
8 changes: 7 additions & 1 deletion plugins/shell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ Config(
prefix: ":sh",
// Override the shell used to launch the command
shell: None,
max_entries: 10,
// to enable history:
// Some((
// capacity: 100,
// )),
history: None,
)
```
```
99 changes: 99 additions & 0 deletions plugins/shell/src/history.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::fs::File;
use std::io::{BufReader, BufWriter, Seek, SeekFrom, Write};

use indexmap::IndexSet;
use serde::{Deserialize, Serialize};

use crate::HistoryConfig;

#[derive(Serialize, Deserialize, Default)]
struct PersistedHistory<T> {
elements: T,
}
type PersistedHistoryOwned = PersistedHistory<IndexSet<HistoryItem>>;
type PersistedHistoryBorrowed<'a> = PersistedHistory<&'a IndexSet<HistoryItem>>;

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
pub struct HistoryItem {
pub command: String,
}

impl HistoryItem {
fn new(command: String) -> Self {
Self { command }
}
}

#[derive(Debug)]
pub struct History {
store: File,
elements: IndexSet<HistoryItem>,
pub cap: usize,
}

impl History {
pub fn new(history_config: &HistoryConfig) -> Result<History, std::io::Error> {
let maybe_history_path =
dirs::state_dir().map(|s| s.join("anyrun").join("shell").join("history.json"));

if let Some(history_path) = maybe_history_path {
if let Some(dir) = history_path.parent() {
std::fs::create_dir_all(dir)?;
}

let file = File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(&history_path)?;

let reader = BufReader::new(&file);
let persisted_history: PersistedHistoryOwned = match serde_json::from_reader(reader) {
Ok(val) => val,
Err(e) if e.is_eof() => PersistedHistory::default(),
Err(e) => return Err(e.into()),
};

Ok(History {
store: file,
elements: persisted_history.elements,
cap: history_config.capacity,
})
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"failed to get the user state directory",
))
}
}

pub fn push(&mut self, value: String) -> Result<(), std::io::Error> {
// insert_before ensures new usages of existing commands bubble up to the top of the history, simple `insert` does not
self.elements
.insert_before(self.elements.len(), HistoryItem::new(value));

if self.elements.len() > self.cap {
let remove_count = self.elements.len().saturating_sub(self.cap);
self.elements.drain(0..remove_count);
}

self.store.set_len(0)?;
self.store.seek(SeekFrom::Start(0))?;

let mut writer = BufWriter::new(&self.store);
serde_json::to_writer(
&mut writer,
&PersistedHistoryBorrowed {
elements: &self.elements,
},
)?;
writer.flush()?;

Ok(())
}

pub fn elements(&self) -> impl Iterator<Item = &HistoryItem> {
self.elements.iter()
}
}
Loading
Loading