mirror of
https://github.com/atuinsh/atuin.git
synced 2025-06-20 18:07:57 +02:00
feat(ui): When in vim-normal mode apply an alternative highlighting to the selected line (#1574)
This makes it much more obvious whether you're in normal or insert mode.
This commit is contained in:
parent
d21de3cd25
commit
230bf2d1c6
@ -16,6 +16,8 @@ pub struct HistoryList<'a> {
|
|||||||
history: &'a [History],
|
history: &'a [History],
|
||||||
block: Option<Block<'a>>,
|
block: Option<Block<'a>>,
|
||||||
inverted: bool,
|
inverted: bool,
|
||||||
|
/// Apply an alternative highlighting to the selected row
|
||||||
|
alternate_highlight: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@ -65,6 +67,7 @@ impl<'a> StatefulWidget for HistoryList<'a> {
|
|||||||
y: 0,
|
y: 0,
|
||||||
state,
|
state,
|
||||||
inverted: self.inverted,
|
inverted: self.inverted,
|
||||||
|
alternate_highlight: self.alternate_highlight,
|
||||||
};
|
};
|
||||||
|
|
||||||
for item in self.history.iter().skip(state.offset).take(end - start) {
|
for item in self.history.iter().skip(state.offset).take(end - start) {
|
||||||
@ -81,11 +84,12 @@ impl<'a> StatefulWidget for HistoryList<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> HistoryList<'a> {
|
impl<'a> HistoryList<'a> {
|
||||||
pub fn new(history: &'a [History], inverted: bool) -> Self {
|
pub fn new(history: &'a [History], inverted: bool, alternate_highlight: bool) -> Self {
|
||||||
Self {
|
Self {
|
||||||
history,
|
history,
|
||||||
block: None,
|
block: None,
|
||||||
inverted,
|
inverted,
|
||||||
|
alternate_highlight,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,11 +120,14 @@ struct DrawState<'a> {
|
|||||||
y: u16,
|
y: u16,
|
||||||
state: &'a ListState,
|
state: &'a ListState,
|
||||||
inverted: bool,
|
inverted: bool,
|
||||||
|
alternate_highlight: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// longest line prefix I could come up with
|
// longest line prefix I could come up with
|
||||||
#[allow(clippy::cast_possible_truncation)] // we know that this is <65536 length
|
#[allow(clippy::cast_possible_truncation)] // we know that this is <65536 length
|
||||||
pub const PREFIX_LENGTH: u16 = " > 123ms 59s ago".len() as u16;
|
pub const PREFIX_LENGTH: u16 = " > 123ms 59s ago".len() as u16;
|
||||||
|
static SPACES: &str = " ";
|
||||||
|
static _ASSERT: () = assert!(SPACES.len() == PREFIX_LENGTH as usize);
|
||||||
|
|
||||||
impl DrawState<'_> {
|
impl DrawState<'_> {
|
||||||
fn index(&mut self) {
|
fn index(&mut self) {
|
||||||
@ -157,7 +164,10 @@ impl DrawState<'_> {
|
|||||||
let time = format_duration(since.try_into().unwrap_or_default());
|
let time = format_duration(since.try_into().unwrap_or_default());
|
||||||
|
|
||||||
// pad the time a little bit before we write. this aligns things nicely
|
// pad the time a little bit before we write. this aligns things nicely
|
||||||
self.x = PREFIX_LENGTH - 4 - time.len() as u16;
|
self.draw(
|
||||||
|
&SPACES[..((PREFIX_LENGTH - self.x) as usize - 4 - time.len())],
|
||||||
|
Style::default(),
|
||||||
|
);
|
||||||
|
|
||||||
self.draw(&time, style);
|
self.draw(&time, style);
|
||||||
self.draw(" ago", style);
|
self.draw(" ago", style);
|
||||||
@ -165,12 +175,14 @@ impl DrawState<'_> {
|
|||||||
|
|
||||||
fn command(&mut self, h: &History) {
|
fn command(&mut self, h: &History) {
|
||||||
let mut style = Style::default();
|
let mut style = Style::default();
|
||||||
if self.y as usize + self.state.offset == self.state.selected {
|
if !self.alternate_highlight && (self.y as usize + self.state.offset == self.state.selected)
|
||||||
|
{
|
||||||
|
// if not applying alternative highlighting to the whole row, color the command
|
||||||
style = style.fg(Color::Red).add_modifier(Modifier::BOLD);
|
style = style.fg(Color::Red).add_modifier(Modifier::BOLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
for section in h.command.escape_control().split_ascii_whitespace() {
|
for section in h.command.escape_control().split_ascii_whitespace() {
|
||||||
self.x += 1;
|
self.draw(" ", style);
|
||||||
if self.x > self.list_area.width {
|
if self.x > self.list_area.width {
|
||||||
// Avoid attempting to draw a command section beyond the width
|
// Avoid attempting to draw a command section beyond the width
|
||||||
// of the list
|
// of the list
|
||||||
@ -180,7 +192,7 @@ impl DrawState<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&mut self, s: &str, style: Style) {
|
fn draw(&mut self, s: &str, mut style: Style) {
|
||||||
let cx = self.list_area.left() + self.x;
|
let cx = self.list_area.left() + self.x;
|
||||||
|
|
||||||
let cy = if self.inverted {
|
let cy = if self.inverted {
|
||||||
@ -189,6 +201,11 @@ impl DrawState<'_> {
|
|||||||
self.list_area.bottom() - self.y - 1
|
self.list_area.bottom() - self.y - 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if self.alternate_highlight && (self.y as usize + self.state.offset == self.state.selected)
|
||||||
|
{
|
||||||
|
style = style.add_modifier(Modifier::REVERSED);
|
||||||
|
}
|
||||||
|
|
||||||
let w = (self.list_area.width - self.x) as usize;
|
let w = (self.list_area.width - self.x) as usize;
|
||||||
self.x += self.buf.set_stringn(cx, cy, s, w, style).0 - cx;
|
self.x += self.buf.set_stringn(cx, cy, s, w, style).0 - cx;
|
||||||
}
|
}
|
||||||
|
@ -550,7 +550,7 @@ impl State {
|
|||||||
|
|
||||||
match self.tab_index {
|
match self.tab_index {
|
||||||
0 => {
|
0 => {
|
||||||
let results_list = Self::build_results_list(style, results);
|
let results_list = Self::build_results_list(style, results, self.keymap_mode);
|
||||||
f.render_stateful_widget(results_list, results_list_chunk, &mut self.results_state);
|
f.render_stateful_widget(results_list, results_list_chunk, &mut self.results_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -652,8 +652,13 @@ impl State {
|
|||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_results_list(style: StyleState, results: &[History]) -> HistoryList {
|
fn build_results_list(
|
||||||
let results_list = HistoryList::new(results, style.invert);
|
style: StyleState,
|
||||||
|
results: &[History],
|
||||||
|
keymap_mode: KeymapMode,
|
||||||
|
) -> HistoryList<'_> {
|
||||||
|
let results_list =
|
||||||
|
HistoryList::new(results, style.invert, keymap_mode == KeymapMode::VimNormal);
|
||||||
|
|
||||||
if style.compact {
|
if style.compact {
|
||||||
results_list
|
results_list
|
||||||
|
Loading…
x
Reference in New Issue
Block a user