Skip to content

Commit

Permalink
Implement History Builtin for #273 (#324)
Browse files Browse the repository at this point in the history
* Initial Commit for #273

* History run (without running)

* Initial implementation of history search

* Added running using prompt_content_queue

* Add message for out of bounds index

* Simplify run code
  • Loading branch information
rithikasilva authored Jan 17, 2024
1 parent 7970032 commit 28e6e5d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions crates/shrs_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ derive_builder = "0.12"
dirs = "5"
anymap = "1.0.0-beta.2"
multimap = "0.9"
skim = "0.10.4"

shrs_job = { path = "../shrs_job", version = "^0.0.3" }
shrs_utils = { path = "../shrs_utils", version = "^0.0.3" }
Expand Down
51 changes: 39 additions & 12 deletions crates/shrs_core/src/builtin/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use clap::{Parser, Subcommand};
use super::BuiltinCmd;
use crate::{
prelude::CmdOutput,
shell::{Context, Runtime, Shell},
shell::{Context, Runtime, Shell}, prompt_content_queue::PromptContent,
};

use skim::prelude::*;
use std::io::Cursor;

#[derive(Parser)]
struct Cli {
#[command(subcommand)]
Expand Down Expand Up @@ -39,20 +42,44 @@ impl BuiltinCmd for HistoryBuiltin {

match &cli.command {
None => {
// let history = ctx.history.all();
// for (i, h) in history.iter().enumerate() {
// print!("{} {}", i, h);
// }
// stdout().flush()?;
},
for i in (0.._ctx.history.len()).rev() {
_ctx.out.print(format!("{}: {}\n", i, _ctx.history.get(i).unwrap()))?;
}
},
Some(Commands::Clear) => {
// ctx.history.clear();
_ctx.history.clear();
},
Some(Commands::Run { .. }) => {
todo!()
Some(Commands::Run { index }) => {
if let Some(cmd) = _ctx.history.get(*index as usize) {
_ctx.prompt_content_queue.push(PromptContent::new(cmd.to_string(), true))
} else {
_ctx.out.print(format!("Please specificy an index from {} to {} inclusive", 0, _ctx.history.len() - 1))?;
}
},
Some(Commands::Search { .. }) => {
todo!()
Some(Commands::Search { query }) => {
// We expect Skim to succeed
let options = SkimOptionsBuilder::default()
.height(Some("100%"))
.nosort(true)
.query(Some(query))
.build()
.unwrap();

let mut input = String::new();
for i in 0.._ctx.history.len() {
input = format!("{}{}\n", input, _ctx.history.get(i).unwrap());
}
let item_reader = SkimItemReader::default();
let items = item_reader.of_bufread(Cursor::new(input));

let selected_items = Skim::run_with(&options, Some(items))
.map(|out| out.selected_items)
.unwrap_or_else(|| Vec::new());

for item in selected_items.iter() {
_ctx.prompt_content_queue.push(PromptContent::new(item.output().to_string(), true));
break;
}
},
}

Expand Down
12 changes: 8 additions & 4 deletions crates/shrs_core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ impl History for DefaultHistory {
type HistoryItem = String;

fn add(&mut self, item: Self::HistoryItem) {
self.hist.insert(0, item);
if !item.starts_with("history run") {
self.hist.insert(0, item);
}
}

fn clear(&mut self) {
Expand Down Expand Up @@ -125,9 +127,11 @@ impl History for FileBackedHistory {
type HistoryItem = String;

fn add(&mut self, item: Self::HistoryItem) {
self.hist.insert(0, item);
// TODO consider how often we want to flush
self.flush().unwrap();
if !item.starts_with("history run") {
self.hist.insert(0, item);
// TODO consider how often we want to flush
self.flush().unwrap();
}
}

fn clear(&mut self) {
Expand Down

0 comments on commit 28e6e5d

Please sign in to comment.