Skip to content

Commit

Permalink
Nithin/remove history item (#419)
Browse files Browse the repository at this point in the history
* removing history item

* a
  • Loading branch information
nithinmuthukumar authored Apr 1, 2024
1 parent 4d7c344 commit 741f2f7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 74 deletions.
30 changes: 12 additions & 18 deletions crates/shrs_core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ use thiserror::Error;

/// Trait to implement for shell history
pub trait History {
type HistoryItem;

/// Insert item into shell history
fn add(&mut self, cmd: Self::HistoryItem);
fn add(&mut self, cmd: String);
/// Remove all history entries
fn clear(&mut self);
// fn iter(&self) -> impl Iterator<Item = Self::HistoryItem>;
// fn iter(&self) -> impl Iterator<Item = String>;
/// Query for a history entry
fn search(&self, query: &str) -> Option<&Self::HistoryItem>;
fn search(&self, query: &str) -> Option<&String>;
/// Get number of history entries
fn len(&self) -> usize;
/// Get a history entry by index
fn get(&self, i: usize) -> Option<&Self::HistoryItem>;
fn get(&self, i: usize) -> Option<&String>;

/// Check if the history is empty
fn is_empty(&self) -> bool {
Expand All @@ -38,9 +36,7 @@ pub struct DefaultHistory {
}

impl History for DefaultHistory {
type HistoryItem = String;

fn add(&mut self, item: Self::HistoryItem) {
fn add(&mut self, item: String) {
if !item.starts_with("history run") {
self.hist.insert(0, item);
}
Expand All @@ -50,11 +46,11 @@ impl History for DefaultHistory {
self.hist.clear();
}

// fn iter(&self) -> impl Iterator<Item = Self::HistoryItem> {
// fn iter(&self) -> impl Iterator<Item = String> {
// todo!()
// }

fn search(&self, _query: &str) -> Option<&Self::HistoryItem> {
fn search(&self, _query: &str) -> Option<&String> {
todo!()
}

Expand All @@ -63,7 +59,7 @@ impl History for DefaultHistory {
}

/// Get index starts at most recent (index zero is previous command)
fn get(&self, i: usize) -> Option<&Self::HistoryItem> {
fn get(&self, i: usize) -> Option<&String> {
self.hist.get(i)
}
}
Expand Down Expand Up @@ -124,9 +120,7 @@ impl FileBackedHistory {
}

impl History for FileBackedHistory {
type HistoryItem = String;

fn add(&mut self, item: Self::HistoryItem) {
fn add(&mut self, item: String) {
if !item.starts_with("history run") {
self.hist.insert(0, item);
// TODO consider how often we want to flush
Expand All @@ -139,19 +133,19 @@ impl History for FileBackedHistory {
self.flush().unwrap();
}

// fn iter(&self) -> impl Iterator<Item = Self::HistoryItem> {
// fn iter(&self) -> impl Iterator<Item = String> {
// todo!()
// }

fn search(&self, _query: &str) -> Option<&Self::HistoryItem> {
fn search(&self, _query: &str) -> Option<&String> {
todo!()
}

fn len(&self) -> usize {
self.hist.len()
}

fn get(&self, i: usize) -> Option<&Self::HistoryItem> {
fn get(&self, i: usize) -> Option<&String> {
self.hist.get(i)
}
}
Expand Down
17 changes: 6 additions & 11 deletions crates/shrs_core/src/keybinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ use std::collections::HashMap;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use thiserror::Error;

use crate::{shell::{Context, Runtime, Shell}, prelude::LineStateBundle};
use crate::{
prelude::LineStateBundle,
shell::{Context, Runtime, Shell},
};

pub type BindingFn = dyn Fn(&mut LineStateBundle);

/// Implement this trait to define your own keybinding system
pub trait Keybinding {
/// Return true indicates that event was handled
fn handle_key_event(
&self,
line: &mut LineStateBundle,
key_event: KeyEvent,
) -> bool;
fn handle_key_event(&self, line: &mut LineStateBundle, key_event: KeyEvent) -> bool;
fn get_info(&self) -> &HashMap<String, String>;
}

Expand Down Expand Up @@ -126,11 +125,7 @@ impl DefaultKeybinding {
}

impl Keybinding for DefaultKeybinding {
fn handle_key_event(
&self,
line: &mut LineStateBundle,
key_event: KeyEvent,
) -> bool {
fn handle_key_event(&self, line: &mut LineStateBundle, key_event: KeyEvent) -> bool {
let mut event_handled = false;
for (binding, binding_fn) in self.bindings.iter() {
if (key_event.code, key_event.modifiers) == *binding {
Expand Down
106 changes: 68 additions & 38 deletions crates/shrs_core/src/readline/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl LineState {
mode: LineMode::Insert,
lines: String::new(),
}
}
}

pub fn mode(&self) -> LineMode {
self.mode
Expand All @@ -126,17 +126,17 @@ pub struct LineStateBundle<'a> {
pub sh: &'a Shell,
pub ctx: &'a mut Context,
pub rt: &'a mut Runtime,
pub line: &'a mut LineState
pub line: &'a mut LineState,
}

impl<'a> LineStateBundle<'a> {
pub fn new(sh: &'a Shell, ctx: &'a mut Context, rt: &'a mut Runtime, line: &'a mut LineState) -> Self {
LineStateBundle {
sh,
ctx,
rt,
line,
}
pub fn new(
sh: &'a Shell,
ctx: &'a mut Context,
rt: &'a mut Runtime,
line: &'a mut LineState,
) -> Self {
LineStateBundle { sh, ctx, rt, line }
}
}

Expand Down Expand Up @@ -228,7 +228,10 @@ impl Line {
self.painter.init().unwrap();
if let Some(c) = state.ctx.prompt_content_queue.pop() {
auto_run = c.auto_run;
state.line.cb.insert(Location::Cursor(), c.content.as_str())?;
state
.line
.cb
.insert(Location::Cursor(), c.content.as_str())?;
}

loop {
Expand Down Expand Up @@ -270,10 +273,7 @@ impl Line {
let event = read()?;

if let Event::Key(key_event) = event {
if state.sh.keybinding.handle_key_event(
state,
key_event,
) {
if state.sh.keybinding.handle_key_event(state, key_event) {
break;
}
}
Expand Down Expand Up @@ -363,7 +363,11 @@ impl Line {
}

//Keys that are universal regardless of mode, ex. Enter, Ctrl-c
fn handle_standard_keys(&mut self, state: &mut LineStateBundle, event: Event) -> anyhow::Result<bool> {
fn handle_standard_keys(
&mut self,
state: &mut LineStateBundle,
event: Event,
) -> anyhow::Result<bool> {
match event {
Event::Resize(a, b) => {
self.painter.set_term_size(a, b);
Expand Down Expand Up @@ -400,7 +404,11 @@ impl Line {
self.buffer_history.clear();
self.painter.newline()?;

if state.sh.lang.needs_line_check(state.line.get_full_command()) {
if state
.sh
.lang
.needs_line_check(state.line.get_full_command())
{
state.line.lines += state.line.cb.as_str().into_owned().as_str();
state.line.lines += "\n";
state.line.cb.clear();
Expand Down Expand Up @@ -434,7 +442,11 @@ impl Line {
Ok(false)
}

fn handle_insert_keys(&mut self, state: &mut LineStateBundle, event: Event) -> anyhow::Result<()> {
fn handle_insert_keys(
&mut self,
state: &mut LineStateBundle,
event: Event,
) -> anyhow::Result<()> {
match event {
Event::Key(KeyEvent {
code: KeyCode::Tab,
Expand Down Expand Up @@ -546,7 +558,10 @@ impl Line {
..
}) => {
if !state.line.cb.is_empty() && state.line.cb.cursor() != 0 {
state.line.cb.delete(Location::Before(), Location::Cursor())?;
state
.line
.cb
.delete(Location::Before(), Location::Cursor())?;
}
},
Event::Key(KeyEvent {
Expand Down Expand Up @@ -587,7 +602,11 @@ impl Line {
Ok(())
}

fn handle_normal_keys(&mut self, state: &mut LineStateBundle, event: Event) -> anyhow::Result<()> {
fn handle_normal_keys(
&mut self,
state: &mut LineStateBundle,
event: Event,
) -> anyhow::Result<()> {
// TODO write better system toString support key combinations
match event {
Event::Key(KeyEvent {
Expand Down Expand Up @@ -633,7 +652,9 @@ impl Line {
let mut tempbuf = tempfile::NamedTempFile::new().unwrap();

// write contexts of line to file
tempbuf.write_all(state.line.cb.as_str().as_bytes()).unwrap();
tempbuf
.write_all(state.line.cb.as_str().as_bytes())
.unwrap();

// TODO should use shrs_job for this?
// TODO configure the command used
Expand Down Expand Up @@ -677,7 +698,9 @@ impl Line {
// recalculate the current completions
fn populate_completions(&mut self, state: &mut LineStateBundle) -> anyhow::Result<()> {
// TODO IFS
let args = state.line.cb
let args = state
.line
.cb
.slice(..state.line.cb.cursor())
.as_str()
.unwrap()
Expand Down Expand Up @@ -712,17 +735,25 @@ impl Line {
// no-op
},
ReplaceMethod::Replace => {
state.line.cb
state
.line
.cb
.move_cursor(Location::Rel(-(state.line.current_word.len() as isize)))?;
let cur_word_len = unicode_width::UnicodeWidthStr::width(state.line.current_word.as_str());
state.line.cb
let cur_word_len =
unicode_width::UnicodeWidthStr::width(state.line.current_word.as_str());
state
.line
.cb
.delete(Location::Cursor(), Location::Rel(cur_word_len as isize))?;
state.line.current_word.clear();
},
}

// then replace with the completion word
state.line.cb.insert(Location::Cursor(), &completion.accept())?;
state
.line
.cb
.insert(Location::Cursor(), &completion.accept())?;

Ok(())
}
Expand Down Expand Up @@ -751,7 +782,10 @@ impl Line {
// restore saved line
HistoryInd::Prompt => {
state.line.cb.clear();
state.line.cb.insert(Location::Cursor(), &state.line.saved_line)?;
state
.line
.cb
.insert(Location::Cursor(), &state.line.saved_line)?;
},
// fill prompt with history element
HistoryInd::Line(i) => {
Expand All @@ -773,12 +807,10 @@ impl Line {
let hook_ctx = LineModeSwitchCtx {
line_mode: LineMode::Normal,
};
state.sh.hooks.run::<LineModeSwitchCtx>(
state.sh,
state.ctx,
state.rt,
hook_ctx,
)?;
state
.sh
.hooks
.run::<LineModeSwitchCtx>(state.sh, state.ctx, state.rt, hook_ctx)?;
Ok(())
}

Expand All @@ -792,12 +824,10 @@ impl Line {
let hook_ctx = LineModeSwitchCtx {
line_mode: LineMode::Insert,
};
state.sh.hooks.run::<LineModeSwitchCtx>(
state.sh,
state.ctx,
state.rt,
hook_ctx,
)?;
state
.sh
.hooks
.run::<LineModeSwitchCtx>(state.sh, state.ctx, state.rt, hook_ctx)?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/shrs_core/src/readline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub use buffer_history::{BufferHistory, DefaultBufferHistory};
pub use cursor::CursorStyle;
pub use highlight::{DefaultHighlighter, Highlighter, SyntaxHighlighter, SyntaxTheme};
pub use hooks::*;
pub use line::{Line, LineBuilder, LineBuilderError, LineStateBundle, LineMode, Readline};
pub use line::{Line, LineBuilder, LineBuilderError, LineMode, LineStateBundle, Readline};
pub use menu::{DefaultMenu, Menu};
pub use prompt::{DefaultPrompt, Prompt, *};
pub use vi::*;
Loading

0 comments on commit 741f2f7

Please sign in to comment.