aboutsummaryrefslogtreecommitdiffstats
path: root/src/run/builtin.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/run/builtin.rs')
-rw-r--r--src/run/builtin.rs78
1 files changed, 44 insertions, 34 deletions
diff --git a/src/run/builtin.rs b/src/run/builtin.rs
index 341d98d..33025cb 100644
--- a/src/run/builtin.rs
+++ b/src/run/builtin.rs
@@ -257,6 +257,21 @@ struct HistoryArgs {
// TODO: temporal control, i.e. before & after
}
+fn display_history<T: Iterator<Item = HistoryEntry>>(it: T, stdout: &mut dyn Write) -> Result {
+ let now = crate::date::DateTime::now();
+ for entry in it {
+ let delta = now.relative_to(&entry.time);
+ for _ in 0..crate::date::DateTime::longest_reasonable_delta() - delta.len() {
+ stdout.write_all(b" ")?;
+ }
+ stdout.write_all(delta.as_bytes())?;
+ stdout.write_all(b" ")?;
+ stdout.write_all(&entry.cmd)?;
+ stdout.write_all(b"\n")?;
+ }
+ Ok(())
+}
+
pub struct history;
impl Builtin for history {
fn name(&self) -> &str {
@@ -271,48 +286,43 @@ impl Builtin for history {
stdout: &mut dyn Write,
) -> Result {
let args: HistoryArgs = read_args(args, stdout)?;
- let mut hist = session.lock().unwrap().history.clone();
- let now = crate::date::DateTime::now();
- let mut in_dir = if args.here {
- current_dir()?.as_os_str().as_bytes().to_vec()
+ let path_prefix = if args.here {
+ Some(current_dir()?.as_os_str().as_bytes().to_vec())
} else if let Some(path) = args.at {
- path.as_os_str().as_bytes().to_vec()
+ Some(path.as_os_str().as_bytes().to_vec())
} else {
- Vec::new()
+ None
};
- while let Some(b'/') = in_dir.last() {
- in_dir.pop();
- }
-
- // TODO: local handling (first implement global history)
-
- for entry in hist.iter_mut() {
- if !entry.loc.starts_with(&in_dir) {
- continue;
- }
+ let min_time = None;
+ let max_time = None;
+
+ if args.local {
+ let hist = session.lock().unwrap().history.clone();
+ display_history(
+ crate::history::local_history_filter(
+ hist,
+ min_time,
+ max_time,
+ path_prefix.as_deref(),
+ args.strict,
+ ),
+ stdout,
+ )
+ } else {
+ let Ok(hist) = crate::history::HistoryQueryer::new() else {
+ write!(stdout, "error opening global history file\n")?;
+ return Err(Error::Exit(-1));
+ };
- if args.strict {
- while let Some(b'/') = entry.loc.last() {
- entry.loc.pop();
- }
- if entry.loc.len() != in_dir.len() {
- continue;
- }
- }
+ let Ok(it) = hist.query(min_time, max_time, path_prefix.as_deref(), args.strict) else {
+ write!(stdout, "error querying global history\n")?;
+ return Err(Error::Exit(-1));
+ };
- let delta = now.relative_to(&entry.time);
- for _ in 0..crate::date::DateTime::longest_reasonable_delta() - delta.len() {
- stdout.write_all(b" ")?;
- }
- stdout.write_all(delta.as_bytes())?;
- stdout.write_all(b" ")?;
- stdout.write_all(&entry.cmd)?;
- stdout.write_all(b"\n")?;
+ display_history(it, stdout)
}
-
- Ok(())
}
}