Skip to content

Commit

Permalink
Add support for max_preview_height setting
Browse files Browse the repository at this point in the history
This allows for more configuration around `show_preview`, specifically in the case where someone has shell commands spanning more than 4 lines in their history and need to differentiate between them with something other than the first few lines.

This change makes the following supporting changes:

- Update the preview renderer to split _lines_ when they go past the preview width, rather than splitting the whole string - this means that the preview is cleanly rendered even in cases where there are newlines
- Update the preview height size calculator to take into account existing newlines
  • Loading branch information
RichardDRJ committed Jul 6, 2023
1 parent eb5e1c2 commit 7c38176
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub struct Settings {
pub inline_height: u16,
pub invert: bool,
pub show_preview: bool,
pub max_preview_height: u16,
pub show_help: bool,
pub exit_mode: ExitMode,
pub word_jump_mode: WordJumpMode,
Expand Down Expand Up @@ -358,6 +359,7 @@ impl Settings {
.set_default("style", "auto")?
.set_default("inline_height", 0)?
.set_default("show_preview", false)?
.set_default("max_preview_height", 4)?
.set_default("show_help", true)?
.set_default("invert", false)?
.set_default("exit_mode", "return-original")?
Expand Down
21 changes: 12 additions & 9 deletions atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ impl State {
.max_by(|h1, h2| h1.command.len().cmp(&h2.command.len()));
longest_command.map_or(0, |v| {
std::cmp::min(
4,
(v.command.len() as u16 + preview_width - 1 - border_size)
/ (preview_width - border_size),
settings.max_preview_height,
v.command
.split('\n')
.map(|line| (line.len() as u16 + preview_width - 1 - border_size) / (preview_width - border_size))
.sum(),
)
}) + border_size * 2
} else if compact {
Expand Down Expand Up @@ -473,12 +475,13 @@ impl State {
} else {
use itertools::Itertools as _;
let s = &results[selected].command;
s.char_indices()
.step_by(preview_width.into())
.map(|(i, _)| i)
.chain(Some(s.len()))
.tuple_windows()
.map(|(a, b)| &s[a..b])
s.split('\n').flat_map(|line|
line.char_indices()
.step_by(preview_width.into())
.map(|(i, _)| i)
.chain(Some(line.len()))
.tuple_windows()
.map(|(a, b)| &line[a..b]))
.join("\n")
};
let preview = if compact {
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ Configure whether or not to show a preview of the selected command.

Useful when the command is longer than the terminal width and is cut off.

### `max_preview_height`

Configure the maximum height of the preview to show.

Useful when you have long scripts in your history that you want to distinguish by more than the first few lines.

Defaults to `4`.

### `show_help`

Configure whether or not to show the help row, which includes the current Atuin version (and whether an update is available), a keymap hint, and the total amount of commands in your history.
Expand Down

0 comments on commit 7c38176

Please sign in to comment.