From 5dd22c6c8958ff726c11607abd53cf7151f0c919 Mon Sep 17 00:00:00 2001
From: ClementTsang <cjhtsang@uwaterloo.ca>
Date: Sun, 9 Feb 2020 21:34:44 -0500
Subject: [PATCH] Minor refactoring.

---
 src/canvas.rs                                 | 107 +-----------------
 src/canvas/canvas_colours.rs                  |   4 +-
 .../{drawing_utils.rs => colour_utils.rs}     |   0
 src/canvas/drawing_utils.rs                   | 104 +++++++++++++++++
 4 files changed, 110 insertions(+), 105 deletions(-)
 rename src/canvas/canvas_colours/{drawing_utils.rs => colour_utils.rs} (100%)
 create mode 100644 src/canvas/drawing_utils.rs

diff --git a/src/canvas.rs b/src/canvas.rs
index c8cd0d0a..246ce415 100644
--- a/src/canvas.rs
+++ b/src/canvas.rs
@@ -17,6 +17,10 @@ use tui::{
 
 mod canvas_colours;
 use canvas_colours::*;
+
+mod drawing_utils;
+use drawing_utils::*;
+
 // Headers
 const CPU_LEGEND_HEADER: [&str; 2] = ["CPU", "Use%"];
 const DISK_HEADERS: [&str; 7] = ["Disk", "Mount", "Used", "Free", "Total", "R/s", "W/s"];
@@ -1149,106 +1153,3 @@ impl Painter {
 			.render(f, draw_loc);
 	}
 }
-
-/// A somewhat jury-rigged solution to simulate a variable intrinsic layout for
-/// table widths.  Note that this will do one main pass to try to properly
-/// allocate widths.  This will thus potentially cut off latter elements
-/// (return size of 0) if it is too small (threshold), but will try its best.
-///
-/// `width thresholds` and `desired_widths_ratio` should be the same length.
-/// Otherwise bad things happen.
-fn get_variable_intrinsic_widths(
-	total_width: u16, desired_widths_ratio: &[f64], width_thresholds: &[usize],
-) -> (Vec<u16>, usize) {
-	let num_widths = desired_widths_ratio.len();
-	let mut resulting_widths: Vec<u16> = vec![0; num_widths];
-	let mut last_index = 0;
-
-	let mut remaining_width = (total_width - (num_widths as u16 - 1)) as i32; // Required for spaces...
-	let desired_widths = desired_widths_ratio
-		.iter()
-		.map(|&desired_width_ratio| (desired_width_ratio * total_width as f64) as i32)
-		.collect::<Vec<_>>();
-
-	for (itx, desired_width) in desired_widths.into_iter().enumerate() {
-		resulting_widths[itx] = if desired_width < width_thresholds[itx] as i32 {
-			// Try to take threshold, else, 0
-			if remaining_width < width_thresholds[itx] as i32 {
-				0
-			} else {
-				remaining_width -= width_thresholds[itx] as i32;
-				width_thresholds[itx] as u16
-			}
-		} else {
-			// Take as large as possible
-			if remaining_width < desired_width {
-				// Check the biggest chunk possible
-				if remaining_width < width_thresholds[itx] as i32 {
-					0
-				} else {
-					let temp_width = remaining_width;
-					remaining_width = 0;
-					temp_width as u16
-				}
-			} else {
-				remaining_width -= desired_width;
-				desired_width as u16
-			}
-		};
-
-		if resulting_widths[itx] == 0 {
-			break;
-		} else {
-			last_index += 1;
-		}
-	}
-
-	// Simple redistribution tactic - if there's any space left, split it evenly amongst all members
-	if last_index < num_widths {
-		let for_all_widths = (remaining_width / last_index as i32) as u16;
-		let mut remainder = remaining_width % last_index as i32;
-
-		for resulting_width in &mut resulting_widths {
-			*resulting_width += for_all_widths;
-			if remainder > 0 {
-				*resulting_width += 1;
-				remainder -= 1;
-			}
-		}
-	}
-
-	(resulting_widths, last_index)
-}
-
-fn get_start_position(
-	num_rows: u64, scroll_direction: &app::ScrollDirection, previously_scrolled_position: &mut u64,
-	currently_selected_position: u64,
-) -> u64 {
-	match scroll_direction {
-		app::ScrollDirection::DOWN => {
-			if currently_selected_position < *previously_scrolled_position + num_rows {
-				// If, using previous_scrolled_position, we can see the element
-				// (so within that and + num_rows) just reuse the current previously scrolled position
-				*previously_scrolled_position
-			} else if currently_selected_position >= num_rows {
-				// Else if the current position past the last element visible in the list, omit
-				// until we can see that element
-				*previously_scrolled_position = currently_selected_position - num_rows;
-				currently_selected_position - num_rows
-			} else {
-				// Else, if it is not past the last element visible, do not omit anything
-				0
-			}
-		}
-		app::ScrollDirection::UP => {
-			if currently_selected_position <= *previously_scrolled_position {
-				// If it's past the first element, then show from that element downwards
-				*previously_scrolled_position = currently_selected_position;
-				currently_selected_position
-			} else {
-				// Else, don't change what our start position is from whatever it is set to!
-				*previously_scrolled_position
-			}
-		}
-	}
-}
diff --git a/src/canvas/canvas_colours.rs b/src/canvas/canvas_colours.rs
index 322f6d0a..27a4c4f4 100644
--- a/src/canvas/canvas_colours.rs
+++ b/src/canvas/canvas_colours.rs
@@ -1,5 +1,5 @@
-mod drawing_utils;
-use drawing_utils::*;
+mod colour_utils;
+use colour_utils::*;
 use tui::style::{Color, Style};
 
 use crate::{constants::*, utils::error};
diff --git a/src/canvas/canvas_colours/drawing_utils.rs b/src/canvas/canvas_colours/colour_utils.rs
similarity index 100%
rename from src/canvas/canvas_colours/drawing_utils.rs
rename to src/canvas/canvas_colours/colour_utils.rs
diff --git a/src/canvas/drawing_utils.rs b/src/canvas/drawing_utils.rs
new file mode 100644
index 00000000..2d53d064
--- /dev/null
+++ b/src/canvas/drawing_utils.rs
@@ -0,0 +1,104 @@
+use crate::app;
+
+/// A somewhat jury-rigged solution to simulate a variable intrinsic layout for
+/// table widths.  Note that this will do one main pass to try to properly
+/// allocate widths.  This will thus potentially cut off latter elements
+/// (return size of 0) if it is too small (threshold), but will try its best.
+///
+/// `width thresholds` and `desired_widths_ratio` should be the same length.
+/// Otherwise bad things happen.
+pub fn get_variable_intrinsic_widths(
+	total_width: u16, desired_widths_ratio: &[f64], width_thresholds: &[usize],
+) -> (Vec<u16>, usize) {
+	let num_widths = desired_widths_ratio.len();
+	let mut resulting_widths: Vec<u16> = vec![0; num_widths];
+	let mut last_index = 0;
+
+	let mut remaining_width = (total_width - (num_widths as u16 - 1)) as i32; // Required for spaces...
+	let desired_widths = desired_widths_ratio
+		.iter()
+		.map(|&desired_width_ratio| (desired_width_ratio * total_width as f64) as i32)
+		.collect::<Vec<_>>();
+
+	for (itx, desired_width) in desired_widths.into_iter().enumerate() {
+		resulting_widths[itx] = if desired_width < width_thresholds[itx] as i32 {
+			// Try to take threshold, else, 0
+			if remaining_width < width_thresholds[itx] as i32 {
+				0
+			} else {
+				remaining_width -= width_thresholds[itx] as i32;
+				width_thresholds[itx] as u16
+			}
+		} else {
+			// Take as large as possible
+			if remaining_width < desired_width {
+				// Check the biggest chunk possible
+				if remaining_width < width_thresholds[itx] as i32 {
+					0
+				} else {
+					let temp_width = remaining_width;
+					remaining_width = 0;
+					temp_width as u16
+				}
+			} else {
+				remaining_width -= desired_width;
+				desired_width as u16
+			}
+		};
+
+		if resulting_widths[itx] == 0 {
+			break;
+		} else {
+			last_index += 1;
+		}
+	}
+
+	// Simple redistribution tactic - if there's any space left, split it evenly amongst all members
+	if last_index < num_widths {
+		let for_all_widths = (remaining_width / last_index as i32) as u16;
+		let mut remainder = remaining_width % last_index as i32;
+
+		for resulting_width in &mut resulting_widths {
+			*resulting_width += for_all_widths;
+			if remainder > 0 {
+				*resulting_width += 1;
+				remainder -= 1;
+			}
+		}
+	}
+
+	(resulting_widths, last_index)
+}
+
+pub fn get_start_position(
+	num_rows: u64, scroll_direction: &app::ScrollDirection, previously_scrolled_position: &mut u64,
+	currently_selected_position: u64,
+) -> u64 {
+	match scroll_direction {
+		app::ScrollDirection::DOWN => {
+			if currently_selected_position < *previously_scrolled_position + num_rows {
+				// If, using previous_scrolled_position, we can see the element
+				// (so within that and + num_rows) just reuse the current previously scrolled position
+				*previously_scrolled_position
+			} else if currently_selected_position >= num_rows {
+				// Else if the current position past the last element visible in the list, omit
+				// until we can see that element
+				*previously_scrolled_position = currently_selected_position - num_rows;
+				currently_selected_position - num_rows
+			} else {
+				// Else, if it is not past the last element visible, do not omit anything
+				0
+			}
+		}
+		app::ScrollDirection::UP => {
+			if currently_selected_position <= *previously_scrolled_position {
+				// If it's past the first element, then show from that element downwards
+				*previously_scrolled_position = currently_selected_position;
+				currently_selected_position
+			} else {
+				// Else, don't change what our start position is from whatever it is set to!
+				*previously_scrolled_position
+			}
+		}
+	}
+}