Added scrollback history setting to terminals
Moorly CI / windows-build (push) Successful in 3m46s
Moorly CI / linux-build (push) Successful in 4m11s

This commit is contained in:
Squid committed 2026-11-17 21:59:13 +01:00
1 parent 2d54d74c69
commit ee01826fb9
4 files changed
+172 -12

No files matched your search

Generated
+12
View File
@@ -2588,10 +2588,12 @@ dependencies = [
"iced_test", "iced_test",
"instance-handoff", "instance-handoff",
"nix", "nix",
"num-format",
"png", "png",
"russh", "russh",
"russh-sftp", "russh-sftp",
"serde", "serde",
"sys-locale",
"tokio", "tokio",
"toml 0.8.23", "toml 0.8.23",
"tray-icon", "tray-icon",
@@ -2734,6 +2736,16 @@ dependencies = [
"rand_core", "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]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.47" version = "0.1.47"
+2
View File
@@ -7,10 +7,12 @@ edition = "2024"
iced = { version = "0.14.0", features = ["tokio", "advanced"] } iced = { version = "0.14.0", features = ["tokio", "advanced"] }
iced_aw = { path = "vendor/iced_aw", default-features = false, features = ["color_picker"] } iced_aw = { path = "vendor/iced_aw", default-features = false, features = ["color_picker"] }
instance-handoff = "=0.1.0-alpha.2" instance-handoff = "=0.1.0-alpha.2"
num-format = "0.4"
png = "0.18" png = "0.18"
russh = "0.65.0" russh = "0.65.0"
russh-sftp = "3.0.1" russh-sftp = "3.0.1"
serde = { version = "1.0.229", features = ["derive"] } serde = { version = "1.0.229", features = ["derive"] }
sys-locale = "0.3"
tokio = { version = "1.54.1", features = ["full"] } tokio = { version = "1.54.1", features = ["full"] }
toml = "0.8" toml = "0.8"
ttf-parser = "0.25" ttf-parser = "0.25"
+132 -5
View File
@@ -1972,6 +1972,8 @@ struct Moorly {
autostart_selected: Option<usize>, autostart_selected: Option<usize>,
fade_settings: bool, fade_settings: bool,
fade_settings_draft: bool, fade_settings_draft: bool,
scrollback_size: usize,
scrollback_size_draft: String,
hide_tab_close: bool, hide_tab_close: bool,
hide_tab_close_draft: bool, hide_tab_close_draft: bool,
show_new_session_button: bool, show_new_session_button: bool,
@@ -2768,6 +2770,9 @@ impl Default for ConfigFile {
struct GeneralConfig { struct GeneralConfig {
#[serde(default = "default_true")] #[serde(default = "default_true")]
fade_settings: bool, 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 /// Closing the main window hides it instead of exiting. Defaults to the
/// old behaviour so upgrades do not strand anyone without a tray host. /// old behaviour so upgrades do not strand anyone without a tray host.
#[serde(default)] #[serde(default)]
@@ -2786,6 +2791,7 @@ impl Default for GeneralConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
fade_settings: true, fade_settings: true,
scrollback_size: default_scrollback_size(),
close_to_tray: false, close_to_tray: false,
minimize_to_tray: false, minimize_to_tray: false,
tray_icon_style: icons::TrayIconStyle::default(), tray_icon_style: icons::TrayIconStyle::default(),
@@ -3131,6 +3137,10 @@ fn default_true() -> bool {
true true
} }
fn default_scrollback_size() -> usize {
terminal::DEFAULT_SCROLLBACK_SIZE
}
fn default_theme() -> String { fn default_theme() -> String {
theme::Preference::default().as_str().to_owned() 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.show_start_tab = state.show_start_tab_draft;
state.autostart_profiles = state.autostart_profiles_draft.clone(); state.autostart_profiles = state.autostart_profiles_draft.clone();
state.fade_settings = state.fade_settings_draft; 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.close_to_tray = state.close_to_tray_draft;
state.minimize_to_tray = state.minimize_to_tray_draft; state.minimize_to_tray = state.minimize_to_tray_draft;
state.tray_icon_style = state.tray_icon_style_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_profiles_draft = state.autostart_profiles.clone();
state.autostart_selected = None; state.autostart_selected = None;
state.fade_settings_draft = state.fade_settings; 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.close_to_tray_draft = state.close_to_tray;
state.minimize_to_tray_draft = state.minimize_to_tray; state.minimize_to_tray_draft = state.minimize_to_tray;
state.tray_icon_style_draft = state.tray_icon_style; state.tray_icon_style_draft = state.tray_icon_style;
@@ -5009,6 +5024,7 @@ fn config_file(state: &Moorly) -> ConfigFile {
ConfigFile { ConfigFile {
general: GeneralConfig { general: GeneralConfig {
fade_settings: state.fade_settings, fade_settings: state.fade_settings,
scrollback_size: state.scrollback_size,
close_to_tray: state.close_to_tray, close_to_tray: state.close_to_tray,
minimize_to_tray: state.minimize_to_tray, minimize_to_tray: state.minimize_to_tray,
tray_icon_style: state.tray_icon_style, 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 show_start_tab = config.startup.show_start_tab;
let autostart_profiles = config.startup.autostart_profiles.clone(); let autostart_profiles = config.startup.autostart_profiles.clone();
let fade_settings = config.general.fade_settings; 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 close_to_tray = config.general.close_to_tray;
let minimize_to_tray = config.general.minimize_to_tray; let minimize_to_tray = config.general.minimize_to_tray;
let tray_icon_style = config.general.tray_icon_style; let tray_icon_style = config.general.tray_icon_style;
@@ -5183,6 +5203,8 @@ impl Default for Moorly {
autostart_selected: None, autostart_selected: None,
fade_settings, fade_settings,
fade_settings_draft: fade_settings, fade_settings_draft: fade_settings,
scrollback_size,
scrollback_size_draft: format_number(scrollback_size),
hide_tab_close, hide_tab_close,
hide_tab_close_draft: hide_tab_close, hide_tab_close_draft: hide_tab_close,
show_new_session_button, show_new_session_button,
@@ -5311,6 +5333,7 @@ enum Message {
MoveAutostartBottom(usize), MoveAutostartBottom(usize),
RemoveAutostartProfile(usize), RemoveAutostartProfile(usize),
SetFadeSettings(bool), SetFadeSettings(bool),
SetScrollbackSize(String),
SetCloseToTray(bool), SetCloseToTray(bool),
SetMinimizeToTray(bool), SetMinimizeToTray(bool),
SetTrayIconStyle(icons::TrayIconStyle), SetTrayIconStyle(icons::TrayIconStyle),
@@ -6200,6 +6223,9 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
} }
} }
Message::SetFadeSettings(value) => state.fade_settings_draft = value, 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::SetCloseToTray(value) => state.close_to_tray_draft = value,
Message::SetMinimizeToTray(value) => state.minimize_to_tray_draft = value, Message::SetMinimizeToTray(value) => state.minimize_to_tray_draft = value,
Message::SetTrayIconStyle(style) => { Message::SetTrayIconStyle(style) => {
@@ -8142,9 +8168,12 @@ fn tab_view(
tooltip( tooltip(
content, content,
container(text(title).size(11)).padding(4), container(text(title).size(11))
.padding([4, 7])
.style(theme::menu),
tooltip::Position::Bottom, tooltip::Position::Bottom,
) )
.gap(-4.0)
.into() .into()
} }
@@ -9031,7 +9060,9 @@ fn profile_action_button<'a>(
tooltip( tooltip(
button, button,
container(text(tip).size(11)).padding(4), container(text(tip).size(11))
.padding([4, 7])
.style(theme::menu),
tooltip::Position::Top, tooltip::Position::Top,
) )
.into() .into()
@@ -10269,7 +10300,9 @@ fn profile_folder_row<'a>(
let profile_row: Element<'_, Message> = if let Some(reason) = unavailable { let profile_row: Element<'_, Message> = if let Some(reason) = unavailable {
tooltip( tooltip(
profile_row, profile_row,
container(text(reason).size(11)).padding(4), container(text(reason).size(11))
.padding([4, 7])
.style(theme::menu),
tooltip::Position::FollowCursor, tooltip::Position::FollowCursor,
) )
.into() .into()
@@ -10425,6 +10458,40 @@ fn command_palette_row(
.into() .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<usize> {
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> { fn general_panel(state: &Moorly) -> Element<'_, Message> {
let mut panel = column![ let mut panel = column![
row![ row![
@@ -10495,6 +10562,34 @@ fn general_panel(state: &Moorly) -> Element<'_, Message> {
.spacing(8), .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 { let login_action: Element<'_, Message> = if state.autostart_at_login {
button("Remove autostart") button("Remove autostart")
.on_press(Message::DisableAutostartAtLogin) .on_press(Message::DisableAutostartAtLogin)
@@ -11105,6 +11200,7 @@ fn settings_dirty(state: &Moorly) -> bool {
state.show_start_tab_draft != state.show_start_tab state.show_start_tab_draft != state.show_start_tab
|| state.autostart_profiles_draft != state.autostart_profiles || state.autostart_profiles_draft != state.autostart_profiles
|| state.fade_settings_draft != state.fade_settings || 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.hide_tab_close_draft != state.hide_tab_close
|| state.show_new_session_button_draft != state.show_new_session_button || state.show_new_session_button_draft != state.show_new_session_button
|| state.show_command_palette_description_draft != state.show_command_palette_description || 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)); .unwrap_or(Size::new(900.0, 600.0));
let (rows, cols) = terminal_grid(state, size, false); 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, Ok(session) => session,
Err(error) => { Err(error) => {
state.notifications.add( state.notifications.add(
@@ -11755,7 +11857,13 @@ fn restart_terminal(state: &mut Moorly, id: u32) {
return; 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) => { Ok(session) => {
if let Some(terminal) = state.terminal_sessions.get_mut(&id) { if let Some(terminal) = state.terminal_sessions.get_mut(&id) {
terminal.session = session; terminal.session = session;
@@ -12788,6 +12896,8 @@ mod tests {
autostart_selected: None, autostart_selected: None,
fade_settings: true, fade_settings: true,
fade_settings_draft: 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: true,
hide_tab_close_draft: true, hide_tab_close_draft: true,
show_new_session_button: true, show_new_session_button: true,
@@ -13341,6 +13451,23 @@ mod tests {
assert!(state.main_window_hidden); 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] #[test]
fn hiding_tab_close_waits_for_save_or_undo() { fn hiding_tab_close_waits_for_save_or_undo() {
let mut state = state(); let mut state = state();
+26 -7
View File
@@ -91,18 +91,23 @@ pub fn grid_size(size: iced::Size, font_size: f32) -> (u16, u16) {
(rows, cols) (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. /// Parsed terminal screen. This has no knowledge of where the bytes came from.
pub struct Emulator { pub struct Emulator {
parser: vt100::Parser, parser: vt100::Parser,
} }
impl Emulator { 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 rows = std::num::NonZeroU16::new(rows.max(1)).unwrap();
let cols = std::num::NonZeroU16::new(cols.max(1)).unwrap(); let cols = std::num::NonZeroU16::new(cols.max(1)).unwrap();
Self { Self {
parser: vt100::Parser::new(rows, cols, 2000), parser: vt100::Parser::new(rows, cols, scrollback),
} }
} }
@@ -247,19 +252,26 @@ pub struct Session {
impl Session { impl Session {
pub fn spawn(start_dir: &Path, rows: u16, cols: u16) -> Result<Self, String> { pub fn spawn(start_dir: &Path, rows: u16, cols: u16) -> Result<Self, String> {
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, start_dir: &Path,
rows: u16, rows: u16,
cols: u16, cols: u16,
shell: Shell, shell: Shell,
scrollback: usize,
) -> Result<Self, String> { ) -> Result<Self, String> {
let pty = pty::Pty::spawn_with(start_dir, rows, cols, shell)?; let pty = pty::Pty::spawn_with(start_dir, rows, cols, shell)?;
Ok(Self { Ok(Self {
emulator: Emulator::new(rows, cols), emulator: Emulator::new(rows, cols, scrollback),
pty, pty,
}) })
} }
@@ -1161,7 +1173,7 @@ mod tests {
fn session() -> Session { fn session() -> Session {
let pty = pty::Pty::spawn_for_test(Path::new("/"), 24, 80).unwrap(); let pty = pty::Pty::spawn_for_test(Path::new("/"), 24, 80).unwrap();
Session { Session {
emulator: Emulator::new(24, 80), emulator: Emulator::new(24, 80, DEFAULT_SCROLLBACK_SIZE),
pty, pty,
} }
} }
@@ -1377,6 +1389,13 @@ mod tests {
assert!(resized, "the shell did not observe the new window size"); 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] #[test]
fn shell_runs_in_the_start_directory() { fn shell_runs_in_the_start_directory() {
let directory = let directory =
@@ -1386,7 +1405,7 @@ mod tests {
let mut session = { let mut session = {
let pty = pty::Pty::spawn_for_test(&directory, 24, 80).unwrap(); let pty = pty::Pty::spawn_for_test(&directory, 24, 80).unwrap();
Session { Session {
emulator: Emulator::new(24, 80), emulator: Emulator::new(24, 80, DEFAULT_SCROLLBACK_SIZE),
pty, pty,
} }
}; };