aboutsummaryrefslogtreecommitdiff
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
parentea95516537b9ef2c9badd01abcf72f20066f9c55 (diff)
Fix help on smaller terminal
COLUMNS not working, using term_size crate for a more portable solution.
-rw-r--r--Cargo.lock11
-rw-r--r--Cargo.toml1
-rw-r--r--src/error/mod.rs38
3 files changed, 31 insertions, 19 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 8b0baa1..de51705 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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"
374checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 375checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
375 376
376[[package]] 377[[package]]
378name = "term_size"
379version = "0.3.2"
380source = "registry+https://github.com/rust-lang/crates.io-index"
381checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9"
382dependencies = [
383 "libc",
384 "winapi",
385]
386
387[[package]]
377name = "textwrap" 388name = "textwrap"
378version = "0.11.0" 389version = "0.11.0"
379source = "registry+https://github.com/rust-lang/crates.io-index" 390source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index ae37294..4527862 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,3 +19,4 @@ lazy_static = "1.3.0"
19directories = "2.0.1" 19directories = "2.0.1"
20regex = "1.1.7" 20regex = "1.1.7"
21num = "0.2" 21num = "0.2"
22term_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.
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;