summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2020-04-23 16:03:07 -0400
committerGitHub <noreply@github.com>2020-04-23 16:03:07 -0400
commit99fe0a1844b063ee34eafb7cab00bdadc3ae8006 (patch)
treeb0867728de940b269353951fe364fa739a56e3b8 /src
parent481275f61d4cc8e0f82dbd9b41bfc9384c5e061c (diff)
parent9fe6e7d4d200c8a6004ead9cac8eccda1a1e3b09 (diff)
Merge pull request #126 from ClementTsang/update_tests
Update arg test; add config tests
Diffstat (limited to 'src')
-rw-r--r--src/app/layout_manager.rs4
-rw-r--r--src/canvas/canvas_colours.rs3
-rw-r--r--src/canvas/canvas_colours/colour_utils.rs57
-rw-r--r--src/main.rs14
-rw-r--r--src/options.rs13
-rw-r--r--src/utils/error.rs10
6 files changed, 64 insertions, 37 deletions
diff --git a/src/app/layout_manager.rs b/src/app/layout_manager.rs
index 8c6815b0..60a241e2 100644
--- a/src/app/layout_manager.rs
+++ b/src/app/layout_manager.rs
@@ -929,11 +929,11 @@ impl std::str::FromStr for BottomWidgetType {
"net" | "network" => Ok(BottomWidgetType::Net),
"proc" | "process" | "processes" => Ok(BottomWidgetType::Proc),
"temp" | "temperature" => Ok(BottomWidgetType::Temp),
- "disk" => Ok(BottomWidgetType::Disk),
+ "disk" => Ok(BottomWidgetType::Disk),
"empty" => Ok(BottomWidgetType::Empty),
"battery" | "batt" => Ok(BottomWidgetType::Battery),
_ => Err(BottomError::ConfigError(format!(
- "Invalid widget type: {}",
+ "invalid widget type: {}",
s
))),
}
diff --git a/src/canvas/canvas_colours.rs b/src/canvas/canvas_colours.rs
index c38b487d..1e1fe236 100644
--- a/src/canvas/canvas_colours.rs
+++ b/src/canvas/canvas_colours.rs
@@ -166,7 +166,8 @@ impl CanvasColours {
pub fn set_battery_colours(&mut self, colours: &[String]) -> error::Result<()> {
if colours.is_empty() {
Err(error::BottomError::ConfigError(
- "Battery colour list must have at least one colour!".to_string(),
+ "invalid colour config: battery colour list must have at least one colour!"
+ .to_string(),
))
} else {
let generated_colours: Result<Vec<_>, _> = colours
diff --git a/src/canvas/canvas_colours/colour_utils.rs b/src/canvas/canvas_colours/colour_utils.rs
index 0d1af937..595989cc 100644
--- a/src/canvas/canvas_colours/colour_utils.rs
+++ b/src/canvas/canvas_colours/colour_utils.rs
@@ -96,17 +96,34 @@ pub fn gen_n_styles(num_to_gen: i32) -> Vec<Style> {
}
pub fn convert_hex_to_color(hex: &str) -> error::Result<Color> {
+ fn hex_err(hex: &str) -> error::Result<u8> {
+ Err(
+ error::BottomError::ConfigError(format!(
+ "invalid color hex: error when parsing hex value {}. It must be a valid 7 character hex string of the (ie: \"#112233\")."
+ , hex))
+ )
+ }
+
fn convert_hex_to_rgb(hex: &str) -> error::Result<(u8, u8, u8)> {
- if hex.len() == 7 && &hex[0..1] == "#" {
- let r = u8::from_str_radix(&hex[1..3], 16)?;
- let g = u8::from_str_radix(&hex[3..5], 16)?;
- let b = u8::from_str_radix(&hex[5..7], 16)?;
+ let hex_components: Vec<char> = hex.chars().collect();
+
+ if hex_components.len() == 7 {
+ let mut r_string = hex_components[1].to_string();
+ r_string.push(hex_components[2]);
+ let mut g_string = hex_components[3].to_string();
+ g_string.push(hex_components[4]);
+ let mut b_string = hex_components[5].to_string();
+ b_string.push(hex_components[6]);
+
+ let r = u8::from_str_radix(&r_string, 16).or_else(|_err| hex_err(hex))?;
+ let g = u8::from_str_radix(&g_string, 16).or_else(|_err| hex_err(hex))?;
+ let b = u8::from_str_radix(&b_string, 16).or_else(|_err| hex_err(hex))?;
return Ok((r, g, b));
}
- Err(error::BottomError::GenericError(format!(
- "Colour hex {} is not of valid length. It must be a 7 character string of the form \"#112233\".",
+ Err(error::BottomError::ConfigError(format!(
+ "invalid color hex: value {} is not of valid length. It must be a 7 character string of the form \"#112233\".",
hex
)))
}
@@ -125,8 +142,8 @@ pub fn get_style_from_config(input_val: &str) -> error::Result<Style> {
get_style_from_color_name(input_val)
}
} else {
- Err(error::BottomError::GenericError(format!(
- "Colour input {} is not valid.",
+ Err(error::BottomError::ConfigError(format!(
+ "invalid color: value {} is not valid.",
input_val
)))
}
@@ -142,8 +159,8 @@ pub fn get_colour_from_config(input_val: &str) -> error::Result<Color> {
convert_name_to_color(input_val)
}
} else {
- Err(error::BottomError::GenericError(format!(
- "Colour input {} is not valid.",
+ Err(error::BottomError::ConfigError(format!(
+ "invalid color: value {} is not valid.",
input_val
)))
}
@@ -154,10 +171,18 @@ pub fn get_style_from_hex(hex: &str) -> error::Result<Style> {
}
fn convert_rgb_to_color(rgb_str: &str) -> error::Result<Color> {
- let rgb_list = rgb_str.split(',');
+ let rgb_list = rgb_str.split(',').collect::<Vec<&str>>();
+ if rgb_list.len() != 3 {
+ return Err(error::BottomError::ConfigError(format!(
+ "invalid RGB color: value {} is not of valid length. It must be a comma separated value with 3 integers from 0 to 255 (ie: \"255, 0, 155\").",
+ rgb_str
+ )));
+ }
+
let rgb = rgb_list
+ .iter()
.filter_map(|val| {
- if let Ok(res) = val.to_string().trim().parse::<u8>() {
+ if let Ok(res) = (*(*val)).to_string().trim().parse::<u8>() {
Some(res)
} else {
None
@@ -167,8 +192,8 @@ fn convert_rgb_to_color(rgb_str: &str) -> error::Result<Color> {
if rgb.len() == 3 {
Ok(Color::Rgb(rgb[0], rgb[1], rgb[2]))
} else {
- Err(error::BottomError::GenericError(format!(
- "RGB colour {} is not of valid length. It must be a comma separated value with 3 integers from 0 to 255, like \"255, 0, 155\".",
+ Err(error::BottomError::ConfigError(format!(
+ "invalid RGB color: value {} contained invalid RGB values. It must be a comma separated value with 3 integers from 0 to 255 (ie: \"255, 0, 155\").",
rgb_str
)))
}
@@ -184,8 +209,8 @@ fn convert_name_to_color(color_name: &str) -> error::Result<Color> {
return Ok(*color);
}
- Err(error::BottomError::GenericError(format!(
- "Color {} is not a supported config colour. bottom supports the following named colours as strings: \
+ Err(error::BottomError::ConfigError(format!(
+ "invalid named color: value {} is not a supported named colour. The following are supported strings: \
Reset, Black, Red, Green, Yellow, Blue, Magenta, Cyan, Gray, DarkGray, LightRed, LightGreen, \
LightYellow, LightBlue, LightMagenta, LightCyan, White",
color_name
diff --git a/src/main.rs b/src/main.rs
index 42df73ff..675e8cdf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -108,6 +108,12 @@ fn main() -> error::Result<()> {
// Create "app" struct, which will control most of the program and store settings/state
let mut app = build_app(&matches, &config, &widget_layout, default_widget_id)?;
+ // Create painter and set colours.
+ let mut painter = canvas::Painter::init(widget_layout, app.app_config_fields.table_gap);
+ generate_config_colours(&config, &mut painter)?;
+ painter.colours.generate_remaining_cpu_colours();
+ painter.complete_painter_init();
+
// Set up up tui and crossterm
let mut stdout_val = stdout();
execute!(stdout_val, EnterAlternateScreen, EnableMouseCapture)?;
@@ -145,14 +151,6 @@ fn main() -> error::Result<()> {
app.used_widgets.clone(),
);
- let mut painter = canvas::Painter::init(widget_layout, app.app_config_fields.table_gap);
- if let Err(config_check) = generate_config_colours(&config, &mut painter) {
- cleanup_terminal(&mut terminal)?;
- return Err(config_check);
- }
- painter.colours.generate_remaining_cpu_colours();
- painter.complete_painter_init();
-
let mut first_run = true;
loop {
if let Ok(recv) = rx.recv_timeout(Duration::from_millis(TICK_RATE_IN_MILLISECONDS)) {
diff --git a/src/options.rs b/src/options.rs
index f2fa00e3..5b52d17a 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -41,7 +41,7 @@ pub struct ConfigFlags {
pub default_widget_type: Option<String>,
pub default_widget_count: Option<u64>,
pub use_old_network_legend: Option<bool>,
- pub hide_table_gap : Option<bool>,
+ pub hide_table_gap: Option<bool>,
//disabled_cpu_cores: Option<Vec<u64>>, // TODO: [FEATURE] Enable disabling cores in config/flags
}
@@ -219,7 +219,11 @@ pub fn build_app(
hide_time: get_hide_time(matches, config),
autohide_time,
use_old_network_legend: get_use_old_network_legend(matches, config),
- table_gap: if get_hide_table_gap(matches, config){0}else{1},
+ table_gap: if get_hide_table_gap(matches, config) {
+ 0
+ } else {
+ 1
+ },
};
let used_widgets = UsedWidgets {
@@ -286,7 +290,7 @@ pub fn get_widget_layout(
ret_bottom_layout
} else {
return Err(error::BottomError::ConfigError(
- "Invalid layout - please have at least one widget.".to_string(),
+ "invalid layout config: please have at least one widget.".to_string(),
));
}
} else {
@@ -342,7 +346,7 @@ fn get_temperature(
"kelvin" | "k" => Ok(data_harvester::temperature::TemperatureType::Kelvin),
"celsius" | "c" => Ok(data_harvester::temperature::TemperatureType::Celsius),
_ => Err(BottomError::ConfigError(
- "Invalid temperature type. Please have the value be of the form \
+ "invalid temperature type: please have the value be of the form \
<kelvin|k|celsius|c|fahrenheit|f>"
.to_string(),
)),
@@ -633,4 +637,3 @@ pub fn get_hide_table_gap(matches: &clap::ArgMatches<'static>, config: &Config)
}
false
}
-
diff --git a/src/utils/error.rs b/src/utils/error.rs
index e8675f89..41b3d065 100644
--- a/src/utils/error.rs
+++ b/src/utils/error.rs
@@ -28,24 +28,24 @@ impl std::fmt::Display for BottomError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
BottomError::InvalidIO(ref message) => {
- write!(f, "Encountered an IO exception: {}", message)
+ write!(f, "encountered an IO exception: {}", message)
}
BottomError::InvalidArg(ref message) => write!(f, "Invalid argument: {}", message),
BottomError::InvalidHeim(ref message) => write!(
f,
- "Invalid error during data collection due to Heim: {}",
+ "invalid error during data collection due to heim: {}",
message
),
BottomError::CrosstermError(ref message) => {
- write!(f, "Invalid error due to Crossterm: {}", message)
+ write!(f, "invalid error due to Crossterm: {}", message)
}
BottomError::GenericError(ref message) => write!(f, "{}", message),
BottomError::FernError(ref message) => write!(f, "Invalid fern error: {}", message),
BottomError::ConfigError(ref message) => {
- write!(f, "Invalid config file error: {}", message)
+ write!(f, "invalid config file error: {}", message)
}
BottomError::ConversionError(ref message) => {
- write!(f, "Unable to convert: {}", message)
+ write!(f, "unable to convert: {}", message)
}
}
}