From ee01826fb95d4950db1314f0c19e3079ed049fd0 Mon Sep 17 00:00:00 2001 From: squid Date: Tue, 17 Nov 2026 21:59:13 +0100 Subject: [PATCH] Added scrollback history setting to terminals --- Cargo.lock | 12 +++++ Cargo.toml | 2 + src/main.rs | 137 ++++++++++++++++++++++++++++++++++++++++++++++-- src/terminal.rs | 33 +++++++++--- 4 files changed, 172 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0fda557..404c6c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2588,10 +2588,12 @@ dependencies = [ "iced_test", "instance-handoff", "nix", + "num-format", "png", "russh", "russh-sftp", "serde", + "sys-locale", "tokio", "toml 0.8.23", "tray-icon", @@ -2734,6 +2736,16 @@ dependencies = [ "rand_core", ] +[[package]] +name = "num-format" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a652d9771a63711fd3c3deb670acfbe5c30a4072e664d7a3bf5a9e1056ac72c3" +dependencies = [ + "arrayvec", + "itoa", +] + [[package]] name = "num-integer" version = "0.1.47" diff --git a/Cargo.toml b/Cargo.toml index 16bd83f..d906dfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,12 @@ edition = "2024" iced = { version = "0.14.0", features = ["tokio", "advanced"] } iced_aw = { path = "vendor/iced_aw", default-features = false, features = ["color_picker"] } instance-handoff = "=0.1.0-alpha.2" +num-format = "0.4" png = "0.18" russh = "0.65.0" russh-sftp = "3.0.1" serde = { version = "1.0.229", features = ["derive"] } +sys-locale = "0.3" tokio = { version = "1.54.1", features = ["full"] } toml = "0.8" ttf-parser = "0.25" diff --git a/src/main.rs b/src/main.rs index 02ac029..7e13e75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1972,6 +1972,8 @@ struct Moorly { autostart_selected: Option, fade_settings: bool, fade_settings_draft: bool, + scrollback_size: usize, + scrollback_size_draft: String, hide_tab_close: bool, hide_tab_close_draft: bool, show_new_session_button: bool, @@ -2768,6 +2770,9 @@ impl Default for ConfigFile { struct GeneralConfig { #[serde(default = "default_true")] fade_settings: bool, + /// Lines of scrollback kept by terminal sessions. + #[serde(default = "default_scrollback_size")] + scrollback_size: usize, /// Closing the main window hides it instead of exiting. Defaults to the /// old behaviour so upgrades do not strand anyone without a tray host. #[serde(default)] @@ -2786,6 +2791,7 @@ impl Default for GeneralConfig { fn default() -> Self { Self { fade_settings: true, + scrollback_size: default_scrollback_size(), close_to_tray: false, minimize_to_tray: false, tray_icon_style: icons::TrayIconStyle::default(), @@ -3131,6 +3137,10 @@ fn default_true() -> bool { true } +fn default_scrollback_size() -> usize { + terminal::DEFAULT_SCROLLBACK_SIZE +} + fn default_theme() -> String { theme::Preference::default().as_str().to_owned() } @@ -4911,6 +4921,10 @@ fn apply_settings(state: &mut Moorly) { state.show_start_tab = state.show_start_tab_draft; state.autostart_profiles = state.autostart_profiles_draft.clone(); state.fade_settings = state.fade_settings_draft; + state.scrollback_size = parse_scrollback(&state.scrollback_size_draft) + .unwrap_or(state.scrollback_size) + .min(terminal::MAX_SCROLLBACK_SIZE); + state.scrollback_size_draft = format_number(state.scrollback_size); state.close_to_tray = state.close_to_tray_draft; state.minimize_to_tray = state.minimize_to_tray_draft; state.tray_icon_style = state.tray_icon_style_draft; @@ -4955,6 +4969,7 @@ fn undo_settings(state: &mut Moorly) { state.autostart_profiles_draft = state.autostart_profiles.clone(); state.autostart_selected = None; state.fade_settings_draft = state.fade_settings; + state.scrollback_size_draft = format_number(state.scrollback_size); state.close_to_tray_draft = state.close_to_tray; state.minimize_to_tray_draft = state.minimize_to_tray; state.tray_icon_style_draft = state.tray_icon_style; @@ -5009,6 +5024,7 @@ fn config_file(state: &Moorly) -> ConfigFile { ConfigFile { general: GeneralConfig { fade_settings: state.fade_settings, + scrollback_size: state.scrollback_size, close_to_tray: state.close_to_tray, minimize_to_tray: state.minimize_to_tray, tray_icon_style: state.tray_icon_style, @@ -5113,6 +5129,10 @@ impl Default for Moorly { let show_start_tab = config.startup.show_start_tab; let autostart_profiles = config.startup.autostart_profiles.clone(); let fade_settings = config.general.fade_settings; + let scrollback_size = config + .general + .scrollback_size + .min(terminal::MAX_SCROLLBACK_SIZE); let close_to_tray = config.general.close_to_tray; let minimize_to_tray = config.general.minimize_to_tray; let tray_icon_style = config.general.tray_icon_style; @@ -5183,6 +5203,8 @@ impl Default for Moorly { autostart_selected: None, fade_settings, fade_settings_draft: fade_settings, + scrollback_size, + scrollback_size_draft: format_number(scrollback_size), hide_tab_close, hide_tab_close_draft: hide_tab_close, show_new_session_button, @@ -5311,6 +5333,7 @@ enum Message { MoveAutostartBottom(usize), RemoveAutostartProfile(usize), SetFadeSettings(bool), + SetScrollbackSize(String), SetCloseToTray(bool), SetMinimizeToTray(bool), SetTrayIconStyle(icons::TrayIconStyle), @@ -6200,6 +6223,9 @@ fn update(state: &mut Moorly, message: Message) -> Task { } } Message::SetFadeSettings(value) => state.fade_settings_draft = value, + Message::SetScrollbackSize(value) => { + state.scrollback_size_draft = sanitize_scrollback_input(&value); + } Message::SetCloseToTray(value) => state.close_to_tray_draft = value, Message::SetMinimizeToTray(value) => state.minimize_to_tray_draft = value, Message::SetTrayIconStyle(style) => { @@ -8142,9 +8168,12 @@ fn tab_view( tooltip( content, - container(text(title).size(11)).padding(4), + container(text(title).size(11)) + .padding([4, 7]) + .style(theme::menu), tooltip::Position::Bottom, ) + .gap(-4.0) .into() } @@ -9031,7 +9060,9 @@ fn profile_action_button<'a>( tooltip( button, - container(text(tip).size(11)).padding(4), + container(text(tip).size(11)) + .padding([4, 7]) + .style(theme::menu), tooltip::Position::Top, ) .into() @@ -10269,7 +10300,9 @@ fn profile_folder_row<'a>( let profile_row: Element<'_, Message> = if let Some(reason) = unavailable { tooltip( profile_row, - container(text(reason).size(11)).padding(4), + container(text(reason).size(11)) + .padding([4, 7]) + .style(theme::menu), tooltip::Position::FollowCursor, ) .into() @@ -10425,6 +10458,40 @@ fn command_palette_row( .into() } +/// Formats a number with the grouping separator of the user's locale. +fn format_number(value: usize) -> String { + use num_format::ToFormattedString; + + sys_locale::get_locale() + .and_then(|name| { + num_format::Locale::from_name(name.as_str()) + .or_else(|_| { + num_format::Locale::from_name(name.split('-').next().unwrap_or_default()) + }) + .ok() + }) + .map(|locale| value.to_formatted_string(&locale)) + .unwrap_or_else(|| value.to_string()) +} + +/// Keeps only digits and renders the result with locale grouping. +fn sanitize_scrollback_input(value: &str) -> String { + match parse_scrollback(value) { + Some(value) => format_number(value.min(terminal::MAX_SCROLLBACK_SIZE)), + None => String::new(), + } +} + +/// Reads a scrollback size from text that may contain locale separators. +fn parse_scrollback(value: &str) -> Option { + let digits: String = value.chars().filter(char::is_ascii_digit).collect(); + + (!digits.is_empty()) + .then(|| digits.parse().ok()) + .flatten() + .map(|value: usize| value.min(terminal::MAX_SCROLLBACK_SIZE)) +} + fn general_panel(state: &Moorly) -> Element<'_, Message> { let mut panel = column![ row![ @@ -10495,6 +10562,34 @@ fn general_panel(state: &Moorly) -> Element<'_, Message> { .spacing(8), ); + panel = panel.push( + column![ + styled_text(&state.typography.h2, "Terminal"), + row![ + column![ + styled_text(&state.typography.h3, "Scrollback size"), + styled_text( + &state.typography.caption, + format!( + "Lines of history kept for new Terminal and PowerShell sessions. Use 0 to disable; maximum is {}.", + format_number(terminal::MAX_SCROLLBACK_SIZE) + ), + ), + ] + .spacing(4) + .width(Length::Fill), + text_input("Scrollback size", &state.scrollback_size_draft) + .on_input(Message::SetScrollbackSize) + .padding(6) + .style(theme::filter_field) + .width(130), + ] + .spacing(12) + .align_y(Alignment::Center), + ] + .spacing(8), + ); + let login_action: Element<'_, Message> = if state.autostart_at_login { button("Remove autostart") .on_press(Message::DisableAutostartAtLogin) @@ -11105,6 +11200,7 @@ fn settings_dirty(state: &Moorly) -> bool { state.show_start_tab_draft != state.show_start_tab || state.autostart_profiles_draft != state.autostart_profiles || state.fade_settings_draft != state.fade_settings + || parse_scrollback(&state.scrollback_size_draft) != Some(state.scrollback_size) || state.hide_tab_close_draft != state.hide_tab_close || state.show_new_session_button_draft != state.show_new_session_button || state.show_command_palette_description_draft != state.show_command_palette_description @@ -11544,7 +11640,13 @@ fn start_terminal_profile( .unwrap_or(Size::new(900.0, 600.0)); let (rows, cols) = terminal_grid(state, size, false); - let session = match terminal::Session::spawn_with(&start_dir, rows, cols, shell) { + let session = match terminal::Session::spawn_with_scrollback( + &start_dir, + rows, + cols, + shell, + state.scrollback_size, + ) { Ok(session) => session, Err(error) => { state.notifications.add( @@ -11755,7 +11857,13 @@ fn restart_terminal(state: &mut Moorly, id: u32) { return; } - match terminal::Session::spawn_with(&start_dir, rows, cols, shell) { + match terminal::Session::spawn_with_scrollback( + &start_dir, + rows, + cols, + shell, + state.scrollback_size, + ) { Ok(session) => { if let Some(terminal) = state.terminal_sessions.get_mut(&id) { terminal.session = session; @@ -12788,6 +12896,8 @@ mod tests { autostart_selected: None, fade_settings: true, fade_settings_draft: true, + scrollback_size: super::terminal::DEFAULT_SCROLLBACK_SIZE, + scrollback_size_draft: super::format_number(super::terminal::DEFAULT_SCROLLBACK_SIZE), hide_tab_close: true, hide_tab_close_draft: true, show_new_session_button: true, @@ -13341,6 +13451,23 @@ mod tests { assert!(state.main_window_hidden); } + #[test] + fn scrollback_size_parses_grouped_input_and_clamps() { + let mut state = state(); + + let _ = update(&mut state, Message::SetScrollbackSize("12,500".to_owned())); + assert_eq!(state.scrollback_size_draft, super::format_number(12_500)); + super::apply_settings(&mut state); + assert_eq!(state.scrollback_size, 12_500); + + let _ = update( + &mut state, + Message::SetScrollbackSize("9,999,999,999".to_owned()), + ); + super::apply_settings(&mut state); + assert_eq!(state.scrollback_size, super::terminal::MAX_SCROLLBACK_SIZE); + } + #[test] fn hiding_tab_close_waits_for_save_or_undo() { let mut state = state(); diff --git a/src/terminal.rs b/src/terminal.rs index e213153..95a94e7 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -91,18 +91,23 @@ pub fn grid_size(size: iced::Size, font_size: f32) -> (u16, u16) { (rows, cols) } +/// Default number of scrollback lines kept for each terminal session. +pub const DEFAULT_SCROLLBACK_SIZE: usize = 10_000; +/// Upper bound for the configurable scrollback size. +pub const MAX_SCROLLBACK_SIZE: usize = 1_000_000; + /// Parsed terminal screen. This has no knowledge of where the bytes came from. pub struct Emulator { parser: vt100::Parser, } impl Emulator { - pub fn new(rows: u16, cols: u16) -> Self { + pub fn new(rows: u16, cols: u16, scrollback: usize) -> Self { let rows = std::num::NonZeroU16::new(rows.max(1)).unwrap(); let cols = std::num::NonZeroU16::new(cols.max(1)).unwrap(); Self { - parser: vt100::Parser::new(rows, cols, 2000), + parser: vt100::Parser::new(rows, cols, scrollback), } } @@ -247,19 +252,26 @@ pub struct Session { impl Session { pub fn spawn(start_dir: &Path, rows: u16, cols: u16) -> Result { - Self::spawn_with(start_dir, rows, cols, Shell::Default) + Self::spawn_with_scrollback( + start_dir, + rows, + cols, + Shell::Default, + DEFAULT_SCROLLBACK_SIZE, + ) } - pub fn spawn_with( + pub fn spawn_with_scrollback( start_dir: &Path, rows: u16, cols: u16, shell: Shell, + scrollback: usize, ) -> Result { let pty = pty::Pty::spawn_with(start_dir, rows, cols, shell)?; Ok(Self { - emulator: Emulator::new(rows, cols), + emulator: Emulator::new(rows, cols, scrollback), pty, }) } @@ -1161,7 +1173,7 @@ mod tests { fn session() -> Session { let pty = pty::Pty::spawn_for_test(Path::new("/"), 24, 80).unwrap(); Session { - emulator: Emulator::new(24, 80), + emulator: Emulator::new(24, 80, DEFAULT_SCROLLBACK_SIZE), pty, } } @@ -1377,6 +1389,13 @@ mod tests { assert!(resized, "the shell did not observe the new window size"); } + #[test] + fn emulator_uses_the_configured_scrollback() { + let emulator = Emulator::new(24, 80, 12_345); + + assert_eq!(emulator.screen().scrollback_capacity(), 12_345); + } + #[test] fn shell_runs_in_the_start_directory() { let directory = @@ -1386,7 +1405,7 @@ mod tests { let mut session = { let pty = pty::Pty::spawn_for_test(&directory, 24, 80).unwrap(); Session { - emulator: Emulator::new(24, 80), + emulator: Emulator::new(24, 80, DEFAULT_SCROLLBACK_SIZE), pty, } };