aboutsummaryrefslogtreecommitdiff
path: root/src/error
diff options
context:
space:
mode:
authorIvan Tham <[email protected]>2020-11-02 17:45:45 +0000
committerIvan Tham <[email protected]>2020-11-02 17:45:45 +0000
commit8ff40835ccb622c076e889ed48e5cef6480977a1 (patch)
tree63f3d86d38bcd07afe8327fb449a519bd12bbf9a /src/error
parentea95516537b9ef2c9badd01abcf72f20066f9c55 (diff)
Fix help on smaller terminal
COLUMNS not working, using term_size crate for a more portable solution.
Diffstat (limited to 'src/error')
-rw-r--r--src/error/mod.rs38
1 files changed, 19 insertions, 19 deletions
diff --git a/src/error/mod.rs b/src/error/mod.rs
index 307ca31..e05d4e3 100644
--- a/src/error/mod.rs
+++ b/src/error/mod.rs
@@ -30,30 +30,30 @@ pub fn handler(e: CalcError) -> String {
30 }, 30 },
31 CalcError::Syntax(details) => format!("Syntax Error: {}", details), 31 CalcError::Syntax(details) => format!("Syntax Error: {}", details),
32 CalcError::Parser(details) => format!("Parser Error: {}", details), 32 CalcError::Parser(details) => format!("Parser Error: {}", details),
33 CalcError::Help => format!( 33 CalcError::Help => {
34 "Constants\n{}\nFunctions\n{}\nOperators\n{}\n", 34 // calculate max width but ideally this should be calculated once
35 blocks(lex::CONSTANTS.keys().cloned()), 35 let mut max_width = 79; // capped at 79
36 blocks(lex::FUNCTIONS.keys().cloned()), 36 if let Some((w, _)) = term_size::dimensions() {
37 { 37 if w < max_width {
38 let l: Vec<_> = lex::OPERATORS.keys().map(|c| c.to_string()).collect(); 38 max_width = w;
39 l.join(" ") 39 }
40 } 40 }
41 ), 41 let operators: Vec<_> = lex::OPERATORS.keys().map(|c| c.to_string()).collect();
42 format!(
43 "Constants\n{}\nFunctions\n{}\nOperators\n{}\n",
44 blocks(max_width, lex::CONSTANTS.keys().cloned()),
45 blocks(max_width, lex::FUNCTIONS.keys().cloned()),
46 operators.join(" ")
47 )
48 }
42 } 49 }
43} 50}
44 51
45/// Convert iterator into strings of chunks of 8 right padded with space. 52/// Convert iterator into strings of chunks of 8 right padded with space.
46fn blocks(mut iter: impl Iterator<Item = &'static str> + ExactSizeIterator) -> String { 53fn blocks(
47 // calculate max width but ideally this should be calculated once 54 max_width: usize,
48 let mut max_width = 79; // capped at 79 55 mut iter: impl Iterator<Item = &'static str> + ExactSizeIterator,
49 if let Ok(s) = std::env::var("COLUMNS") { 56) -> String {
50 if let Ok(n) = s.parse() {
51 if n < max_width {
52 max_width = n;
53 }
54 }
55 }
56
57 // multiply by eight since we are formatting it into chunks of 8 57 // multiply by eight since we are formatting it into chunks of 8
58 let items_per_line = max_width / 8; 58 let items_per_line = max_width / 8;
59 let full_bytes = (iter.len() - iter.len() % items_per_line) * 8; 59 let full_bytes = (iter.len() - iter.len() % items_per_line) * 8;