diff options
author | ath3 <45574139+ath3@users.noreply.github.com> | 2019-07-03 19:35:30 +0000 |
---|---|---|
committer | rabite0 <rabite@posteo.de> | 2019-07-03 21:35:30 +0200 |
commit | 6198b4e421d87ac20b77424ef537d8b6e730f9e4 (patch) | |
tree | a6f2f6e20b253dac7637c6acd316746c33f72865 /src | |
parent | fae8706f7ab7c1141c7ae4737b12891cb23213fd (diff) |
Customizable column ratios (#58)
* Customizable column ratios
* Allow ratios sum to be different from 99
* Moved the ratio logic to HBox, column ratios are not based on 0-99 anymore, improved parsing from config file
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 19 | ||||
-rw-r--r-- | src/file_browser.rs | 3 | ||||
-rw-r--r-- | src/hbox.rs | 25 |
3 files changed, 40 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs index e969f1a..3c53a78 100644 --- a/src/config.rs +++ b/src/config.rs @@ -77,7 +77,8 @@ pub struct Config { pub icons: bool, pub media_autoplay: bool, pub media_mute: bool, - pub media_previewer: String + pub media_previewer: String, + pub ratios: Vec::<usize> } @@ -97,7 +98,8 @@ impl Config { icons: false, media_autoplay: false, media_mute: false, - media_previewer: "hunter-media".to_string() + media_previewer: "hunter-media".to_string(), + ratios: vec![20,30,49] } } @@ -133,6 +135,19 @@ impl Config { Ok(("media_previewer", cmd)) => { let cmd = cmd.to_string(); config.select_cmd = cmd; + }, + Ok(("ratios", ratios)) => { + let ratios_str = ratios.to_string(); + if ratios_str.chars().all(|x| x.is_digit(10) || x.is_whitespace() + || x == ':' || x == ',' ) { + let ratios: Vec<usize> = ratios_str.split([',', ':'].as_ref()) + .map(|r| r.trim().parse::<usize>().unwrap()).collect(); + let ratios_sum: usize = ratios.iter().sum(); + if ratios.len() == 3 && ratios_sum > 0 && ratios.iter() + .filter(|&r| *r > u16::max_value() as usize).next() == None { + config.ratios = ratios; + } + } } _ => { HError::config_error::<Config>(line.to_string()).log(); } } diff --git a/src/file_browser.rs b/src/file_browser.rs index 019b77f..b77109b 100644 --- a/src/file_browser.rs +++ b/src/file_browser.rs @@ -234,6 +234,7 @@ impl Tabbable for TabView<FileBrowser> { }).log(); tab.preview_widget_mut().map(|w| w.config_loaded()).ok(); + tab.columns.set_ratios(self.core.config().ratios); } Ok(()) } @@ -255,7 +256,7 @@ impl FileBrowser { let mut core_p = core.clone(); let mut columns = HBox::new(core); - columns.set_ratios(vec![20,30,49]); + columns.set_ratios(core.config().ratios); let list_coords = columns.calculate_coordinates()?; core_l.coordinates = list_coords[0].clone(); diff --git a/src/hbox.rs b/src/hbox.rs index edc8e4e..8ad23c6 100644 --- a/src/hbox.rs +++ b/src/hbox.rs @@ -96,17 +96,34 @@ impl<T> HBox<T> where T: Widget + PartialEq { let box_ysize = box_coords.ysize(); let box_top = box_coords.top().y(); - let ratios = match &self.ratios { + let mut ratios = match &self.ratios { Some(ratios) => ratios.clone(), None => self.calculate_equal_ratios()? }; + let mut ratios_sum: usize = ratios.iter().sum(); + + ratios = ratios.iter().map(|&r| + (r as f64 * box_xsize as f64 / ratios_sum as f64).round() as usize).collect(); + + for r in &mut ratios { + if *r < 10 { *r = 10 } + } + + ratios_sum = ratios.iter().sum(); + + while ratios_sum + ratios.len() > box_xsize as usize { + let ratios_max = ratios.iter() + .position(|&r| r == *ratios.iter().max().unwrap()).unwrap(); + ratios[ratios_max] = ratios[ratios_max] - 1; + ratios_sum -= 1; + } + let coords = ratios.iter().fold(Vec::<Coordinates>::new(), |mut coords, ratio| { - let ratio = *ratio as u16; let len = coords.len(); - let gap = if len == 0 { 0 } else { 1 }; + let gap = if len == ratios.len() { 0 } else { 1 }; - let widget_xsize = box_xsize * ratio / 100; + let widget_xsize = *ratio as u16; let widget_xpos = if len == 0 { box_coords.top().x() } else { |