-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vi mode indicators #369
base: master
Are you sure you want to change the base?
Vi mode indicators #369
Changes from 4 commits
5b369f3
9c0ebb7
2234f78
783d1c9
a73f517
5950b7f
c0b8c7a
23e05df
3beb69a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,12 @@ use std::rc::Rc; | |
use unicode_segmentation::UnicodeSegmentation; | ||
use unicode_width::UnicodeWidthChar; | ||
|
||
use super::{Context, Helper, Result}; | ||
use super::{Context, EditMode, Helper, Result}; | ||
use crate::config::Config; | ||
use crate::highlight::Highlighter; | ||
use crate::history::Direction; | ||
use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word}; | ||
use crate::keymap::{InputState, Invoke, Refresher}; | ||
use crate::keymap::{InputMode, InputState, Invoke, Refresher}; | ||
use crate::layout::{Layout, Position}; | ||
use crate::line_buffer::{LineBuffer, WordAction, MAX_LINE}; | ||
use crate::tty::{Renderer, Term, Terminal}; | ||
|
@@ -33,6 +34,7 @@ pub struct State<'out, 'prompt, H: Helper> { | |
pub ctx: Context<'out>, // Give access to history for `hinter` | ||
pub hint: Option<String>, // last hint displayed | ||
highlight_char: bool, // `true` if a char has been highlighted | ||
input_mode: Option<InputMode>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may avoid to duplicate input_mode if we move |
||
} | ||
|
||
enum Info<'m> { | ||
|
@@ -47,6 +49,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { | |
prompt: &'prompt str, | ||
helper: Option<&'out H>, | ||
ctx: Context<'out>, | ||
config: &'prompt Config, | ||
) -> State<'out, 'prompt, H> { | ||
let prompt_size = out.calculate_position(prompt, Position::default()); | ||
State { | ||
|
@@ -62,6 +65,10 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { | |
ctx, | ||
hint: None, | ||
highlight_char: false, | ||
input_mode: match &config.edit_mode() { | ||
EditMode::Vi => Some(InputMode::Insert), | ||
EditMode::Emacs => None, | ||
}, | ||
} | ||
} | ||
|
||
|
@@ -164,6 +171,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { | |
&self.layout, | ||
&new_layout, | ||
highlighter, | ||
self.input_mode, | ||
)?; | ||
self.layout = new_layout; | ||
|
||
|
@@ -229,6 +237,19 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { | |
Ok(true) | ||
} | ||
} | ||
|
||
//fn calculate_prompt_size(&mut self) -> Position { | ||
// let prompt = self.prompt; | ||
// self.calculate_custom_prompt_size(prompt) | ||
//} | ||
|
||
//fn calculate_custom_prompt_size(&mut self, prompt: &str) -> Position { | ||
// let indicator_size = self.out.calculate_position( | ||
// self.input_mode_indicator().unwrap_or(""), | ||
// Position::default(), | ||
// ); | ||
// self.out.calculate_position(prompt, indicator_size) | ||
//} | ||
} | ||
|
||
impl<'out, 'prompt, H: Helper> Invoke for State<'out, 'prompt, H> { | ||
|
@@ -261,10 +282,25 @@ impl<'out, 'prompt, H: Helper> Refresher for State<'out, 'prompt, H> { | |
|
||
fn doing_insert(&mut self) { | ||
self.changes.borrow_mut().begin(); | ||
if let Some(mode) = &mut self.input_mode { | ||
*mode = InputMode::Insert; | ||
self.refresh_line().unwrap(); | ||
} | ||
} | ||
|
||
fn doing_replace(&mut self) { | ||
if let Some(mode) = &mut self.input_mode { | ||
*mode = InputMode::Replace; | ||
self.refresh_line().unwrap(); | ||
} | ||
} | ||
|
||
fn done_inserting(&mut self) { | ||
self.changes.borrow_mut().end(); | ||
if let Some(mode) = &mut self.input_mode { | ||
*mode = InputMode::Command; | ||
self.refresh_line().unwrap(); | ||
} | ||
} | ||
|
||
fn last_insert(&self) -> Option<String> { | ||
|
@@ -675,6 +711,7 @@ pub fn init_state<'out, H: Helper>( | |
ctx: Context::new(history), | ||
hint: Some("hint".to_owned()), | ||
highlight_char: false, | ||
input_mode: None, | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,7 +319,7 @@ mod tests { | |
#[test] | ||
fn add() { | ||
let config = Config::builder().history_ignore_space(true).build(); | ||
let mut history = History::with_config(config); | ||
let mut history = History::with_config(config.clone()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be reverted. |
||
assert_eq!(config.max_history_size(), history.max_len); | ||
assert!(history.add("line1")); | ||
assert!(history.add("line2")); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ use crate::edit::State; | |
use crate::highlight::Highlighter; | ||
use crate::hint::Hinter; | ||
use crate::history::{Direction, History}; | ||
pub use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word}; | ||
pub use crate::keymap::{Anchor, At, CharSearch, Cmd, InputMode, Movement, RepeatCount, Word}; | ||
use crate::keymap::{InputState, Refresher}; | ||
pub use crate::keys::KeyPress; | ||
use crate::kill_ring::{KillRing, Mode}; | ||
|
@@ -424,7 +424,7 @@ fn readline_edit<H: Helper>( | |
|
||
editor.reset_kill_ring(); // TODO recreate a new kill ring vs Arc<Mutex<KillRing>> | ||
let ctx = Context::new(&editor.history); | ||
let mut s = State::new(&mut stdout, prompt, helper, ctx); | ||
let mut s = State::new(&mut stdout, prompt, helper, ctx, &editor.config); | ||
let mut input_state = InputState::new(&editor.config, Arc::clone(&editor.custom_bindings)); | ||
|
||
s.line.set_delete_listener(editor.kill_ring.clone()); | ||
|
@@ -784,7 +784,7 @@ impl<H: Helper> Editor<H> { | |
); | ||
Self { | ||
term, | ||
history: History::with_config(config), | ||
history: History::with_config(config.clone()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be reverted. |
||
helper: None, | ||
kill_ring: Arc::new(Mutex::new(KillRing::new(60))), | ||
config, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//! This module implements and describes common TTY methods & traits | ||
use crate::config::{BellStyle, ColorMode, Config, OutputStreamType}; | ||
use crate::highlight::Highlighter; | ||
use crate::keymap::InputMode; | ||
use crate::keys::KeyPress; | ||
use crate::layout::{Layout, Position}; | ||
use crate::line_buffer::LineBuffer; | ||
|
@@ -39,6 +40,7 @@ pub trait Renderer { | |
old_layout: &Layout, | ||
new_layout: &Layout, | ||
highlighter: Option<&dyn Highlighter>, | ||
vi_mode: Option<InputMode>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can factorize code by passing a |
||
) -> Result<()>; | ||
|
||
/// Compute layout for rendering prompt + line + some info (either hint, | ||
|
@@ -118,8 +120,17 @@ impl<'a, R: Renderer + ?Sized> Renderer for &'a mut R { | |
old_layout: &Layout, | ||
new_layout: &Layout, | ||
highlighter: Option<&dyn Highlighter>, | ||
vi_mode: Option<InputMode>, | ||
) -> Result<()> { | ||
(**self).refresh_line(prompt, line, hint, old_layout, new_layout, highlighter) | ||
(**self).refresh_line( | ||
prompt, | ||
line, | ||
hint, | ||
old_layout, | ||
new_layout, | ||
highlighter, | ||
vi_mode, | ||
) | ||
} | ||
|
||
fn calculate_position(&self, s: &str, orig: Position) -> Position { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be reverted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 missed that.