Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ main
====

Improvements:
- [Issue #134]: Add a single-key shortcut `r` to toggle between
absolute-only and relative-only line numbers at runtime. The numeric
count buffer is cleared on toggle for consistency with other
single-key commands. Help text updated accordingly.
- [Issue #143]: `ctrl-z` will now send jless to the background
- `:w[rite] <file>` and `:w[rite]! <file>` can be used to write the
current input to a file
Expand Down
7 changes: 7 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,13 @@ impl App {
Key::End => Some(Action::FocusBottom),
Key::Char('%') => Some(Action::FocusMatchingPair),
Key::Char('m') => Some(Action::ToggleMode),
Key::Char('r') => {
crate::screenwriter::toggle_line_number_mode(
&mut self.screen_writer.show_line_numbers,
&mut self.screen_writer.show_relative_line_numbers,
);
None
}
Key::Char('<') => {
self.screen_writer
.decrease_indentation_level(self.viewer.flatjson.2 as u16);
Expand Down
3 changes: 3 additions & 0 deletions src/jless.help
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@
is from the currently focused line. This makes it easier to use the j
and k commands with specified counts.

Press 'r' at any time to toggle between absolute-only and
relative-only line numbers.

The appearance of line numbers can be configured via command line flags:

-n, --line-numbers Show absolute line numbers.
Expand Down
36 changes: 36 additions & 0 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,39 @@ impl ScreenWriter {
}
}
}

// Toggle between absolute-only and relative-only line numbers.
// If both flags are the same (both true or both false), default to absolute-only.
pub(crate) fn toggle_line_number_mode(
show_absolute_line_numbers: &mut bool,
show_relative_line_numbers: &mut bool,
) {
if *show_absolute_line_numbers == *show_relative_line_numbers {
// If both are the same (both true or both false), normalize to absolute-only.
*show_absolute_line_numbers = true;
*show_relative_line_numbers = false;
} else {
// Otherwise, flip to the other exclusive mode.
*show_absolute_line_numbers = !*show_absolute_line_numbers;
*show_relative_line_numbers = !*show_relative_line_numbers;
}
}

#[cfg(test)]
mod tests {
use super::toggle_line_number_mode;

#[test]
fn toggle_line_number_mode_cases() {
for (start_absolute, start_relative, expected_absolute, expected_relative) in vec![
(true, false, false, true),
(false, true, true, false),
(true, true, true, false),
(false, false, true, false),
] {
let (mut absolute, mut relative) = (start_absolute, start_relative);
toggle_line_number_mode(&mut absolute, &mut relative);
assert_eq!((absolute, relative), (expected_absolute, expected_relative));
}
}
}