Skip to content

Commit

Permalink
Merge pull request #697 from carlfriedrich/make-width-configurable-fo…
Browse files Browse the repository at this point in the history
…r-snippet-column

Make width configurable for snippet column
  • Loading branch information
denisidoro authored Mar 6, 2022
2 parents 528178e + 6b43872 commit 77403ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ impl Config {
self.yaml.style.comment.width_percentage
}

pub fn snippet_width_percentage(&self) -> u16 {
self.yaml.style.snippet.width_percentage
}

pub fn tag_min_width(&self) -> u16 {
self.yaml.style.tag.min_width
}
Expand All @@ -136,6 +140,10 @@ impl Config {
self.yaml.style.comment.min_width
}

pub fn snippet_min_width(&self) -> u16 {
self.yaml.style.snippet.min_width
}

#[cfg(feature = "disable-command-execution")]
fn print(&self) -> bool {
true
Expand Down
7 changes: 6 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::terminal;
pub use crate::terminal::style::style;
use std::cmp::max;

pub fn get_widths() -> (usize, usize) {
pub fn get_widths() -> (usize, usize, usize) {
let width = terminal::width();
let tag_width_percentage = max(
CONFIG.tag_min_width(),
Expand All @@ -13,8 +13,13 @@ pub fn get_widths() -> (usize, usize) {
CONFIG.comment_min_width(),
width * CONFIG.comment_width_percentage() / 100,
);
let snippet_width_percentage = max(
CONFIG.snippet_min_width(),
width * CONFIG.snippet_width_percentage() / 100,
);
(
usize::from(tag_width_percentage),
usize::from(comment_width_percentage),
usize::from(snippet_width_percentage),
)
}
6 changes: 3 additions & 3 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const DELIMITER: &str = r" ⠀";
lazy_static! {
pub static ref NEWLINE_REGEX: Regex = Regex::new(r"\\\s+").expect("Invalid regex");
pub static ref VAR_REGEX: Regex = Regex::new(r"\\?<(\w[\w\d\-_]*)>").expect("Invalid regex");
pub static ref COLUMN_WIDTHS: (usize, usize) = ui::get_widths();
pub static ref COLUMN_WIDTHS: (usize, usize, usize) = ui::get_widths();
}

pub fn with_new_lines(txt: String) -> String {
Expand All @@ -37,12 +37,12 @@ fn limit_str(text: &str, length: usize) -> String {
}

pub fn write(item: &Item) -> String {
let (tag_width_percentage, comment_width_percentage) = *COLUMN_WIDTHS;
let (tag_width_percentage, comment_width_percentage, snippet_width_percentage) = *COLUMN_WIDTHS;
format!(
"{tags_short}{delimiter}{comment_short}{delimiter}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}{file_index}{delimiter}\n",
tags_short = ui::style(limit_str(&item.tags, tag_width_percentage)).with(CONFIG.tag_color()),
comment_short = ui::style(limit_str(&item.comment, comment_width_percentage)).with(CONFIG.comment_color()),
snippet_short = ui::style(fix_newlines(&item.snippet)).with(CONFIG.snippet_color()),
snippet_short = ui::style(limit_str(&fix_newlines(&item.snippet), snippet_width_percentage)).with(CONFIG.snippet_color()),
tags = item.tags,
comment = item.comment,
delimiter = DELIMITER,
Expand Down

0 comments on commit 77403ca

Please sign in to comment.