Skip to content

Commit

Permalink
update and fix the UI (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine authored Apr 19, 2024
1 parent e68d347 commit 92db4f3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 63 deletions.
1 change: 1 addition & 0 deletions examples/config/default.nuon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
show_cell_path: true, # whether or not to show the current cell path above the status bar
show_table_header: true, # whether or not to show the table header in "table" layout
show_hints: true, # whether or not to show the hints with keybindings
layout: "table", # the layout of the data, either "table" or "compact"
margin: 10, # the number of lines to keep between the cursor and the top / bottom
number: false, # show line numbers
Expand Down
7 changes: 7 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub struct Config {
pub margin: usize,
pub number: bool,
pub relativenumber: bool,
pub show_hints: bool,
}

impl Default for Config {
Expand All @@ -162,6 +163,7 @@ impl Default for Config {
margin: 10,
number: false,
relativenumber: false,
show_hints: true,
colors: ColorConfig {
normal: TableRowColorConfig {
name: BgFgColorConfig {
Expand Down Expand Up @@ -297,6 +299,11 @@ impl Config {
config.relativenumber = val
}
}
"show_hints" => {
if let Some(val) = try_bool(value, &["show_hints"])? {
config.show_hints = val
}
}
"colors" => {
let cell = follow_cell_path(value, &["colors"]).unwrap();
let columns = match &cell {
Expand Down
146 changes: 83 additions & 63 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,27 @@ fn render_data(frame: &mut Frame, app: &mut App) {
line_numbers.push(i as usize);
}

let mut lines = vec![ListItem::new(Line::from("")); 2];
let mut lines = if app.config.layout == Layout::Compact && !is_a_table {
vec![]
} else {
vec![ListItem::new(Line::from("")); 2]
};
for i in line_numbers {
lines.push(ListItem::new(Line::from(Span::styled(
format!("{}", i),
normal_line_style,
))));
}

let mut offset = selected - margin_offset;
if app.config.layout == Layout::Table || is_a_table {
offset += 2;
}

frame.render_stateful_widget(
List::new(lines).highlight_style(highlight_line_style),
rect_lines_without_bottom_bar,
&mut ListState::default().with_selected(Some(selected - margin_offset + 2)),
&mut ListState::default().with_selected(Some(offset)),
);
}

Expand Down Expand Up @@ -451,11 +460,17 @@ fn render_data(frame: &mut Frame, app: &mut App) {
.highlight_style(highlight_style)
.highlight_symbol(&config.colors.selected_symbol);

let selected = if app.is_at_bottom() {
None
} else {
Some(selected)
};

frame.render_stateful_widget(
items,
rect_without_bottom_bar,
&mut ListState::default()
.with_selected(Some(selected))
.with_selected(selected)
.with_offset(margin_offset),
)
}
Expand Down Expand Up @@ -646,69 +661,74 @@ fn render_status_bar(frame: &mut Frame, app: &App) {
Mode::Bottom => bg_style.fg(config.colors.status_bar.bottom.foreground),
};

let hints = match app.mode {
Mode::Normal => format!(
"{} to {} | {}{}{}{} to move around | {} to peek | {} to transpose | {} to quit",
repr_key(&config.keybindings.insert),
Mode::Insert,
repr_key(&config.keybindings.navigation.left),
repr_key(&config.keybindings.navigation.down),
repr_key(&config.keybindings.navigation.up),
repr_key(&config.keybindings.navigation.right),
repr_key(&config.keybindings.peek),
repr_key(&config.keybindings.transpose),
repr_key(&config.keybindings.quit),
),
Mode::Waiting(n) => format!(
"{} to quit | will run next motion {} times",
repr_key(&KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
n
),
Mode::Insert => format!(
"{} to quit | {}{}{}{} to move the cursor | {}{} to delete characters | {} to confirm",
repr_key(&KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Left, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Right, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Delete, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
),
Mode::Peeking => format!(
"{} to {} | {} to peek all | {} to peek current view | {} to peek under cursor | {} to peek the cell path",
repr_key(&config.keybindings.normal),
Mode::Normal,
repr_key(&config.keybindings.peeking.all),
repr_key(&config.keybindings.peeking.view),
repr_key(&config.keybindings.peeking.under),
repr_key(&config.keybindings.peeking.cell_path),
),
Mode::Bottom => format!(
"{} to {} | {} to peek | {} to quit",
repr_key(&config.keybindings.navigation.left),
Mode::Normal,
repr_key(&config.keybindings.peek),
repr_key(&config.keybindings.quit),
),
};

let left = Line::from(Span::styled(
format!(" {} ", app.mode),
style.add_modifier(Modifier::REVERSED),
));
let right = Line::from(Span::styled(hints, style));

frame.render_widget(
Paragraph::new(left)
.alignment(Alignment::Left)
.style(bg_style),
bottom_bar_rect,
);
frame.render_widget(
Paragraph::new(right).alignment(Alignment::Right),
Paragraph::new(Line::from(Span::styled(
format!(" {} ", app.mode),
style.add_modifier(Modifier::REVERSED),
)))
.alignment(Alignment::Left)
.style(bg_style),
bottom_bar_rect,
);

if app.config.show_hints || matches!(app.mode, Mode::Waiting(..)) {
let hints = match app.mode {
Mode::Normal => format!(
"{} to {} | {}{}{}{} to move around | {} to peek | {} to transpose | {} to quit",
repr_key(&config.keybindings.insert),
Mode::Insert,
repr_key(&config.keybindings.navigation.left),
repr_key(&config.keybindings.navigation.down),
repr_key(&config.keybindings.navigation.up),
repr_key(&config.keybindings.navigation.right),
repr_key(&config.keybindings.peek),
repr_key(&config.keybindings.transpose),
repr_key(&config.keybindings.quit),
),
Mode::Waiting(n) => {
if app.config.show_hints {
format!(
"{} to quit | will run next motion {} times",
repr_key(&KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
n
)
} else {
format!("{}", n)
}
},
Mode::Insert => format!(
"{} to quit | {}{}{}{} to move the cursor | {}{} to delete characters | {} to confirm",
repr_key(&KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Left, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Right, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Delete, KeyModifiers::NONE)),
repr_key(&KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
),
Mode::Peeking => format!(
"{} to {} | {} to peek all | {} to peek current view | {} to peek under cursor | {} to peek the cell path",
repr_key(&config.keybindings.normal),
Mode::Normal,
repr_key(&config.keybindings.peeking.all),
repr_key(&config.keybindings.peeking.view),
repr_key(&config.keybindings.peeking.under),
repr_key(&config.keybindings.peeking.cell_path),
),
Mode::Bottom => format!(
"{} to {} | {} to peek | {} to quit",
repr_key(&config.keybindings.navigation.left),
Mode::Normal,
repr_key(&config.keybindings.peek),
repr_key(&config.keybindings.quit),
),
};
frame.render_widget(
Paragraph::new(Line::from(Span::styled(hints, style))).alignment(Alignment::Right),
bottom_bar_rect,
);
}
}

// TODO: add proper assert error messages
Expand Down

0 comments on commit 92db4f3

Please sign in to comment.