Skip to content
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

Add toggle line number feature to editor #6

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}