diff options
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/error/mod.rs | 38 |
3 files changed, 31 insertions, 19 deletions
@@ -158,6 +158,7 @@ dependencies = [ | |||
158 | "radix_fmt", | 158 | "radix_fmt", |
159 | "regex", | 159 | "regex", |
160 | "rustyline", | 160 | "rustyline", |
161 | "term_size", | ||
161 | ] | 162 | ] |
162 | 163 | ||
163 | [[package]] | 164 | [[package]] |
@@ -374,6 +375,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
374 | checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" | 375 | checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" |
375 | 376 | ||
376 | [[package]] | 377 | [[package]] |
378 | name = "term_size" | ||
379 | version = "0.3.2" | ||
380 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
381 | checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9" | ||
382 | dependencies = [ | ||
383 | "libc", | ||
384 | "winapi", | ||
385 | ] | ||
386 | |||
387 | [[package]] | ||
377 | name = "textwrap" | 388 | name = "textwrap" |
378 | version = "0.11.0" | 389 | version = "0.11.0" |
379 | source = "registry+https://github.com/rust-lang/crates.io-index" | 390 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -19,3 +19,4 @@ lazy_static = "1.3.0" | |||
19 | directories = "2.0.1" | 19 | directories = "2.0.1" |
20 | regex = "1.1.7" | 20 | regex = "1.1.7" |
21 | num = "0.2" | 21 | num = "0.2" |
22 | term_size = "0.3" | ||
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. |
46 | fn blocks(mut iter: impl Iterator<Item = &'static str> + ExactSizeIterator) -> String { | 53 | fn 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; |