Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.8 KB
Raw Blame
use ghostty_vt::{Column, Line};

use super::TerminalInstance;

/// terminalInstance.ts `Constants.MaxCanvasWidth`: the widest texture a fixed width may need.
const MAX_CANVAS_WIDTH: f32 = 4096.;

impl TerminalInstance {
    /// terminalInstance.ts `toggleSizeToContentWidth`: with fixed columns, back to the pane's; else the longest
    /// viewport line's, when wider than the pane.
    pub fn toggle_size_to_content_width(&mut self, char_width: f32) {
        if self.fixed_cols.take().is_some() {
            self.scroll_x = 0.;
            return;
        }
        let max_cols_for_texture = (MAX_CANVAS_WIDTH / char_width).floor() as usize;
        let cols = self.term.columns();
        let proposed = cols.max(self.longest_viewport_wrapped_line_length().min(max_cols_for_texture));
        if proposed > cols {
            self.fixed_cols = Some(proposed as u16);
        }
    }

    /// xtermTerminal.ts `getLongestViewportWrappedLineLength`: each viewport line joined with the rows it wraps
    /// from, less the blanks ending its last row.
    fn longest_viewport_wrapped_line_length(&self) -> usize {
        let cols = self.term.columns();
        let viewport_top = -(self.term.display_offset() as i32);
        let topmost = -(self.term.history_size() as i32);
        let wraps = |line: i32| self.term.wraps(Line(line));
        let mut longest = 0;
        let mut line = self.term.screen_lines() as i32 - 1;
        while line >= viewport_top {
            let row = self.term.row(Line(line));
            let end_spaces = (0..cols).rev().take_while(|&column| matches!(row[Column(column)].c, ' ' | '\0')).count();
            let mut first = line;
            while first > topmost && wraps(first - 1) {
                first -= 1;
            }
            let line_count = (line - first + 1) as usize;
            longest = longest.max(line_count * cols - end_spaces);
            line = first - 1;
        }
        longest
    }
}

#[cfg(test)]
mod tests {
    use ghostty_vt::Config;

    use super::super::{ShellLaunchConfig, TerminalInstance, colors};

    #[test]
    fn content_width_fixes_the_columns_to_the_longest_wrapped_line_and_toggles_back() {
        let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
        let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
        let colors = colors::TerminalColors::new(&crate::theme::load_theme(&[themes], "Dark Modern").unwrap());
        let launch = ShellLaunchConfig { extension_pty: true, cwd: std::env::temp_dir(), ..Default::default() };
        let (mut instance, _) = TerminalInstance::new(1, launch, 10, 4, Config { scrolling_history: 100, ..Config::default() });
        instance.process(b"short\r\n0123456789abcdefghij0123\r\n$ ", &colors);
        instance.toggle_size_to_content_width(8.);
        assert_eq!(instance.fixed_cols, Some(24));
        instance.resize(24, 4);
        assert_eq!(instance.term.columns(), 24);
        instance.scroll_x = 40.;
        instance.toggle_size_to_content_width(8.);
        assert_eq!((instance.fixed_cols, instance.scroll_x), (None, 0.));
    }

    #[test]
    fn content_that_fits_keeps_the_pane_columns() {
        let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
        let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
        let colors = colors::TerminalColors::new(&crate::theme::load_theme(&[themes], "Dark Modern").unwrap());
        let launch = ShellLaunchConfig { extension_pty: true, cwd: std::env::temp_dir(), ..Default::default() };
        let (mut instance, _) = TerminalInstance::new(1, launch, 10, 4, Config { scrolling_history: 100, ..Config::default() });
        instance.process(b"short\r\n$ ", &colors);
        instance.toggle_size_to_content_width(8.);
        assert_eq!(instance.fixed_cols, None);
    }
}