Skip to content

Commit

Permalink
Add toggle line number feature to editor (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
panarch authored Oct 24, 2024
1 parent 940de6b commit 18d036e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/src/state/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl NotebookState {
vec![
"[i] Edit mode",
"[b] Browse note tree",
"[n] Toggle line number",
"[h] Show editor keymap",
"[Esc] Quit",
]
Expand Down
7 changes: 7 additions & 0 deletions tui/src/context/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct NotebookContext {
// editor
pub editor: TextArea<'static>,
pub opened_note: Option<Note>,
pub show_line_number: bool,
}

impl Default for NotebookContext {
Expand All @@ -75,6 +76,7 @@ impl Default for NotebookContext {

editor: TextArea::new(vec!["Welcome to Glues :D".to_owned()]),
opened_note: None,
show_line_number: true,
}
}
}
Expand Down Expand Up @@ -238,6 +240,11 @@ impl NotebookContext {
self.state = ContextState::EditorEditMode;
Action::Dispatch(NotebookEvent::EditNote.into())
}
KeyCode::Char('n') => {
self.show_line_number = !self.show_line_number;

Action::None
}
KeyCode::Char('h') => TuiAction::ShowEditorKeymap.into(),
KeyCode::Esc => TuiAction::Confirm {
message: "Do you want to quit?".to_owned(),
Expand Down
19 changes: 13 additions & 6 deletions tui/src/views/body/notebook/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ pub fn draw(frame: &mut Frame, area: Rect, context: &mut Context) {
),
None => block,
}
.padding(Padding::horizontal(1));
.padding(if context.notebook.show_line_number {
Padding::ZERO
} else {
Padding::left(1)
});

context.notebook.editor.set_block(block);

Expand All @@ -46,11 +50,14 @@ pub fn draw(frame: &mut Frame, area: Rect, context: &mut Context) {
_ => (Style::default(), Style::default()),
};

context.notebook.editor.set_cursor_style(cursor_style);
context
.notebook
.editor
.set_cursor_line_style(cursor_line_style);
let editor = &mut context.notebook.editor;
editor.set_cursor_style(cursor_style);
editor.set_cursor_line_style(cursor_line_style);
if context.notebook.show_line_number {
editor.set_line_number_style(Style::default().dark_gray().dim());
} else {
editor.remove_line_number();
}

frame.render_widget(&context.notebook.editor, area);
}

0 comments on commit 18d036e

Please sign in to comment.