Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Calculate cell size
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Apr 21, 2023
1 parent 57b4b10 commit 926757f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
49 changes: 39 additions & 10 deletions pulldown-cmark-mdcat/src/terminal/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct TerminalSize {
pub rows: u16,
/// The size in pixels, if available.
pub pixels: Option<PixelSize>,
/// The size of once cell, if available.
pub cell: Option<PixelSize>,
}

impl Default for TerminalSize {
Expand All @@ -66,6 +68,7 @@ impl Default for TerminalSize {
columns: 80,
rows: 24,
pixels: None,
cell: None,
}
}
}
Expand Down Expand Up @@ -121,19 +124,24 @@ mod implementation {
);
None
} else {
let pixels = if winsize.ws_xpixel != 0 && winsize.ws_ypixel != 0 {
Some(PixelSize {
let mut terminal_size = TerminalSize {
columns: winsize.ws_col,
rows: winsize.ws_row,
pixels: None,
cell: None,
};
if winsize.ws_xpixel != 0 && winsize.ws_ypixel != 0 {
let pixels = PixelSize {
x: winsize.ws_xpixel as u32,
y: winsize.ws_ypixel as u32,
})
} else {
None
};
terminal_size.pixels = Some(pixels);
terminal_size.cell = Some(PixelSize {
x: pixels.x / terminal_size.columns as u32,
y: pixels.y / terminal_size.rows as u32,
});
};
Some(TerminalSize {
columns: winsize.ws_col,
rows: winsize.ws_row,
pixels,
})
Some(terminal_size)
}
}
}
Expand All @@ -149,6 +157,7 @@ mod implementation {
rows,
columns,
pixels: None,
cell: None,
})
}
}
Expand All @@ -170,6 +179,7 @@ impl TerminalSize {
columns,
rows,
pixels: None,
cell: None,
}),
_ => None,
}
Expand All @@ -193,4 +203,23 @@ impl TerminalSize {
pub fn detect() -> Option<Self> {
Self::from_terminal().or_else(Self::from_env)
}

/// Shrink the terminal size to the given amount of maximum columns.
///
/// Also shrinks the pixel size accordingly.
pub fn with_max_columns(&self, max_columns: u16) -> Self {
let pixels = match (self.pixels, self.cell) {
(Some(pixels), Some(cell)) => Some(PixelSize {
x: cell.x * max_columns as u32,
y: pixels.y,
}),
_ => None,
};
Self {
columns: max_columns,
rows: self.rows,
pixels,
cell: self.cell,
}
}
}
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ fn main() {
// Enable Ansi color processing on Windows; no-op on other platforms.
concolor_query::windows::enable_ansi_colors();

let size = TerminalSize::detect().unwrap_or_default();
let columns = args.columns.unwrap_or(size.columns);
let terminal_size = TerminalSize::detect().unwrap_or_default();
let terminal_size = if let Some(max_columns) = args.columns {
terminal_size.with_max_columns(max_columns)
} else {
terminal_size
};

let exit_code = match Output::new(args.paginate()) {
Ok(mut output) => {
let settings = Settings {
terminal_capabilities: terminal.capabilities(),
terminal_size: TerminalSize { columns, ..size },
terminal_size,
syntax_set: &SyntaxSet::load_defaults_newlines(),
theme: Theme::default(),
};
Expand Down

0 comments on commit 926757f

Please sign in to comment.