Skip to content

Commit 77403ca

Browse files
authored
Merge pull request #697 from carlfriedrich/make-width-configurable-for-snippet-column
Make width configurable for snippet column
2 parents 528178e + 6b43872 commit 77403ca

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/config/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ impl Config {
128128
self.yaml.style.comment.width_percentage
129129
}
130130

131+
pub fn snippet_width_percentage(&self) -> u16 {
132+
self.yaml.style.snippet.width_percentage
133+
}
134+
131135
pub fn tag_min_width(&self) -> u16 {
132136
self.yaml.style.tag.min_width
133137
}
@@ -136,6 +140,10 @@ impl Config {
136140
self.yaml.style.comment.min_width
137141
}
138142

143+
pub fn snippet_min_width(&self) -> u16 {
144+
self.yaml.style.snippet.min_width
145+
}
146+
139147
#[cfg(feature = "disable-command-execution")]
140148
fn print(&self) -> bool {
141149
true

src/ui.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::terminal;
33
pub use crate::terminal::style::style;
44
use std::cmp::max;
55

6-
pub fn get_widths() -> (usize, usize) {
6+
pub fn get_widths() -> (usize, usize, usize) {
77
let width = terminal::width();
88
let tag_width_percentage = max(
99
CONFIG.tag_min_width(),
@@ -13,8 +13,13 @@ pub fn get_widths() -> (usize, usize) {
1313
CONFIG.comment_min_width(),
1414
width * CONFIG.comment_width_percentage() / 100,
1515
);
16+
let snippet_width_percentage = max(
17+
CONFIG.snippet_min_width(),
18+
width * CONFIG.snippet_width_percentage() / 100,
19+
);
1620
(
1721
usize::from(tag_width_percentage),
1822
usize::from(comment_width_percentage),
23+
usize::from(snippet_width_percentage),
1924
)
2025
}

src/writer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub const DELIMITER: &str = r" ⠀";
1111
lazy_static! {
1212
pub static ref NEWLINE_REGEX: Regex = Regex::new(r"\\\s+").expect("Invalid regex");
1313
pub static ref VAR_REGEX: Regex = Regex::new(r"\\?<(\w[\w\d\-_]*)>").expect("Invalid regex");
14-
pub static ref COLUMN_WIDTHS: (usize, usize) = ui::get_widths();
14+
pub static ref COLUMN_WIDTHS: (usize, usize, usize) = ui::get_widths();
1515
}
1616

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

3939
pub fn write(item: &Item) -> String {
40-
let (tag_width_percentage, comment_width_percentage) = *COLUMN_WIDTHS;
40+
let (tag_width_percentage, comment_width_percentage, snippet_width_percentage) = *COLUMN_WIDTHS;
4141
format!(
4242
"{tags_short}{delimiter}{comment_short}{delimiter}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}{file_index}{delimiter}\n",
4343
tags_short = ui::style(limit_str(&item.tags, tag_width_percentage)).with(CONFIG.tag_color()),
4444
comment_short = ui::style(limit_str(&item.comment, comment_width_percentage)).with(CONFIG.comment_color()),
45-
snippet_short = ui::style(fix_newlines(&item.snippet)).with(CONFIG.snippet_color()),
45+
snippet_short = ui::style(limit_str(&fix_newlines(&item.snippet), snippet_width_percentage)).with(CONFIG.snippet_color()),
4646
tags = item.tags,
4747
comment = item.comment,
4848
delimiter = DELIMITER,

0 commit comments

Comments
 (0)