Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Option to show a custom window title #215

Merged
merged 4 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add optional window title
Optional window title can now be provided with --window-title. Total image
width takes into account title width, when greater than lines.
  • Loading branch information
mrcoalp committed Nov 6, 2022
commit 2dc325e3fbc1cf42ee7be6f97c5b12359f430b4f
6 changes: 5 additions & 1 deletion src/bin/silicon/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ pub struct Config {
#[structopt(long)]
pub no_window_controls: bool,

/// Show an optional window title
#[structopt(long, value_name = "WINDOW_TITLE")]
pub window_title: Option<String>,

/// Hide the line number.
#[structopt(long)]
pub no_line_number: bool,
Expand Down Expand Up @@ -269,10 +273,10 @@ impl Config {
let formatter = ImageFormatterBuilder::new()
.line_pad(self.line_pad)
.window_controls(!self.no_window_controls)
.window_title(self.window_title.clone())
.line_number(!self.no_line_number)
.font(self.font.clone().unwrap_or_default())
.round_corner(!self.no_round_corner)
.window_controls(!self.no_window_controls)
.shadow_adder(self.get_shadow_adder()?)
.tab_width(self.tab_width)
.highlight_lines(self.highlight_lines.clone().unwrap_or_default())
Expand Down
47 changes: 39 additions & 8 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct ImageFormatter {
tab_width: u8,
/// Line Offset
line_offset: u32,
/// Window title
window_title: Option<String>,
}

#[derive(Default)]
Expand All @@ -52,6 +54,8 @@ pub struct ImageFormatterBuilder<S> {
highlight_lines: Vec<u32>,
/// Whether show the window controls
window_controls: bool,
/// Window title
window_title: Option<String>,
/// Whether round the corner of the image
round_corner: bool,
/// Shadow adder,
Expand All @@ -69,6 +73,7 @@ impl<S: AsRef<str> + Default> ImageFormatterBuilder<S> {
line_pad: 2,
line_number: true,
window_controls: true,
window_title: None,
round_corner: true,
tab_width: 4,
..Default::default()
Expand Down Expand Up @@ -105,6 +110,12 @@ impl<S: AsRef<str> + Default> ImageFormatterBuilder<S> {
self
}

/// Window title
pub fn window_title(mut self, title: Option<String>) -> Self {
self.window_title = title;
self
}

/// Whether round the corner
pub fn round_corner(mut self, b: bool) -> Self {
self.round_corner = b;
Expand Down Expand Up @@ -151,6 +162,7 @@ impl<S: AsRef<str> + Default> ImageFormatterBuilder<S> {
code_pad_top,
font,
line_offset: self.line_offset,
window_title: self.window_title,
})
}
}
Expand All @@ -161,7 +173,7 @@ struct Drawable {
/// max number of line of the picture
max_lineno: u32,
/// arguments for draw_text_mut
drawables: Vec<(u32, u32, Color, FontStyle, String)>,
drawables: Vec<(u32, u32, Option<Color>, FontStyle, String)>,
}

impl ImageFormatter {
Expand Down Expand Up @@ -214,7 +226,7 @@ impl ImageFormatter {
drawables.push((
width,
height,
style.foreground,
Some(style.foreground),
style.font_style.into(),
text.to_owned(),
));
Expand All @@ -226,6 +238,27 @@ impl ImageFormatter {
max_lineno = i as u32;
}

if self.window_title.is_some() {
// TODO: Move all these hardcoded values
let ctrl_offset = 15 + 120;
let ctrl_center = 15 + 20;
let title_padding = 5;

let title = self.window_title.as_ref().unwrap();
let title_width = self.font.get_text_len(&title);

drawables.push((
ctrl_offset + title_padding,
ctrl_center - self.font.get_font_height() / 2,
None,
FontStyle::BOLD,
title.to_string(),
));

let title_bar_width = ctrl_offset + title_padding * 2 + title_width;
max_width = max_width.max(title_bar_width);
}

Drawable {
max_width,
max_lineno,
Expand Down Expand Up @@ -288,10 +321,8 @@ impl ImageFormatter {
let foreground = theme.settings.foreground.unwrap();
let background = theme.settings.background.unwrap();

let foreground = foreground.to_rgba();
let background = background.to_rgba();

let mut image = DynamicImage::ImageRgba8(RgbaImage::from_pixel(size.0, size.1, background));
let mut image =
DynamicImage::ImageRgba8(RgbaImage::from_pixel(size.0, size.1, background.to_rgba()));

if !self.highlight_lines.is_empty() {
let highlight_lines = self
Expand All @@ -302,11 +333,11 @@ impl ImageFormatter {
self.highlight_lines(&mut image, highlight_lines);
}
if self.line_number {
self.draw_line_number(&mut image, drawables.max_lineno, foreground);
self.draw_line_number(&mut image, drawables.max_lineno, foreground.to_rgba());
}

for (x, y, color, style, text) in drawables.drawables {
let color = color.to_rgba();
let color = color.unwrap_or(foreground).to_rgba();
self.font
.draw_text_mut(&mut image, color, x, y, style, &text);
}
Expand Down