Right-align var_name if terminal under 80 chars

This commit is contained in:
Richard Feldman 2022-10-31 06:14:43 -04:00
parent ea67b729c3
commit 2b89f53e76
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
3 changed files with 106 additions and 36 deletions

127
Cargo.lock generated
View file

@ -129,7 +129,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi",
"libc", "libc",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -509,7 +509,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fdf5e01086b6be750428ba4a40619f847eb2e95756eee84b18e06e5f0b50342" checksum = "9fdf5e01086b6be750428ba4a40619f847eb2e95756eee84b18e06e5f0b50342"
dependencies = [ dependencies = [
"lazy-bytes-cast", "lazy-bytes-cast",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -520,7 +520,7 @@ checksum = "2f3e1238132dc01f081e1cbb9dace14e5ef4c3a51ee244bd982275fb514605db"
dependencies = [ dependencies = [
"error-code", "error-code",
"str-buf", "str-buf",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -572,7 +572,7 @@ checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [ dependencies = [
"atty", "atty",
"lazy_static", "lazy_static",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -606,7 +606,7 @@ dependencies = [
"libc", "libc",
"once_cell", "once_cell",
"terminal_size", "terminal_size",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -801,7 +801,7 @@ dependencies = [
"stdweb 0.1.3", "stdweb 0.1.3",
"thiserror", "thiserror",
"web-sys", "web-sys",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -1040,7 +1040,7 @@ checksum = "2daefd788d1e96e0a9d66dee4b828b883509bc3ea9ce30665f04c3246372690c"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"libloading", "libloading",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -1145,7 +1145,7 @@ checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [ dependencies = [
"libc", "libc",
"redox_users", "redox_users",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -1156,7 +1156,7 @@ checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [ dependencies = [
"libc", "libc",
"redox_users", "redox_users",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -1313,7 +1313,7 @@ checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [ dependencies = [
"errno-dragonfly", "errno-dragonfly",
"libc", "libc",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -1913,6 +1913,16 @@ dependencies = [
"rayon", "rayon",
] ]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]
[[package]] [[package]]
name = "khronos-egl" name = "khronos-egl"
version = "4.1.0" version = "4.1.0"
@ -1971,7 +1981,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -2393,7 +2403,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [ dependencies = [
"overload", "overload",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -2456,6 +2466,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
[[package]] [[package]]
name = "objc" name = "objc"
version = "0.2.7" version = "0.2.7"
@ -2585,7 +2601,7 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66" checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66"
dependencies = [ dependencies = [
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -2632,7 +2648,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eebde548fbbf1ea81a99b128872779c437752fb99f217c45245e1a61dcd9edcd" checksum = "eebde548fbbf1ea81a99b128872779c437752fb99f217c45245e1a61dcd9edcd"
dependencies = [ dependencies = [
"libc", "libc",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -2691,7 +2707,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -3130,6 +3146,15 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "redox_termios"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
dependencies = [
"redox_syscall",
]
[[package]] [[package]]
name = "redox_users" name = "redox_users"
version = "0.4.3" version = "0.4.3"
@ -3187,7 +3212,7 @@ dependencies = [
"bitflags", "bitflags",
"libc", "libc",
"mach", "mach",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -3196,7 +3221,7 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [ dependencies = [
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -3209,7 +3234,7 @@ dependencies = [
"log", "log",
"num_cpus", "num_cpus",
"rayon", "rayon",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -3312,7 +3337,7 @@ dependencies = [
"roc_unify", "roc_unify",
"snafu", "snafu",
"ven_graph", "ven_graph",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -3918,6 +3943,7 @@ dependencies = [
"rustyline", "rustyline",
"rustyline-derive", "rustyline-derive",
"target-lexicon", "target-lexicon",
"termsize",
"unicode-segmentation", "unicode-segmentation",
] ]
@ -4233,7 +4259,7 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
"utf8parse", "utf8parse",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -4530,7 +4556,7 @@ checksum = "31ef6ee280cdefba6d2d0b4b78a84a1c1a3f3a4cec98c2d4231c8bc225de0f25"
dependencies = [ dependencies = [
"libc", "libc",
"mach", "mach",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -4778,7 +4804,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"remove_dir_all 0.5.3", "remove_dir_all 0.5.3",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -4797,7 +4823,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df"
dependencies = [ dependencies = [
"libc", "libc",
"winapi", "winapi 0.3.9",
]
[[package]]
name = "termion"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e"
dependencies = [
"libc",
"numtoa",
"redox_syscall",
"redox_termios",
]
[[package]]
name = "termsize"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e86d824a8e90f342ad3ef4bd51ef7119a9b681b0cc9f8ee7b2852f02ccd2517"
dependencies = [
"atty",
"kernel32-sys",
"libc",
"termion",
"winapi 0.2.8",
] ]
[[package]] [[package]]
@ -4957,7 +5008,7 @@ dependencies = [
"stdweb 0.4.20", "stdweb 0.4.20",
"time-macros 0.1.1", "time-macros 0.1.1",
"version_check", "version_check",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -5248,7 +5299,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [ dependencies = [
"same-file", "same-file",
"winapi", "winapi 0.3.9",
"winapi-util", "winapi-util",
] ]
@ -5381,7 +5432,7 @@ dependencies = [
"wasmer-types", "wasmer-types",
"wasmer-vm", "wasmer-vm",
"wat", "wat",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -5532,7 +5583,7 @@ dependencies = [
"wasmer-engine-universal-artifact", "wasmer-engine-universal-artifact",
"wasmer-types", "wasmer-types",
"wasmer-vm", "wasmer-vm",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -5615,7 +5666,7 @@ dependencies = [
"thiserror", "thiserror",
"wasmer-artifact", "wasmer-artifact",
"wasmer-types", "wasmer-types",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -5634,7 +5685,7 @@ dependencies = [
"wasmer", "wasmer",
"wasmer-vfs", "wasmer-vfs",
"wasmer-wasi-types", "wasmer-wasi-types",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -5837,7 +5888,7 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
"wgpu-types", "wgpu-types",
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -5872,6 +5923,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -5882,6 +5939,12 @@ dependencies = [
"winapi-x86_64-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu",
] ]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
[[package]] [[package]]
name = "winapi-i686-pc-windows-gnu" name = "winapi-i686-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
@ -5894,7 +5957,7 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [ dependencies = [
"winapi", "winapi 0.3.9",
] ]
[[package]] [[package]]
@ -6018,7 +6081,7 @@ dependencies = [
"wayland-client", "wayland-client",
"wayland-protocols", "wayland-protocols",
"web-sys", "web-sys",
"winapi", "winapi 0.3.9",
"x11-dl", "x11-dl",
] ]

View file

@ -23,6 +23,7 @@ rustyline = {git = "https://github.com/roc-lang/rustyline", rev = "e74333c"}
rustyline-derive = {git = "https://github.com/roc-lang/rustyline", rev = "e74333c"} rustyline-derive = {git = "https://github.com/roc-lang/rustyline", rev = "e74333c"}
target-lexicon = "0.12.2" target-lexicon = "0.12.2"
unicode-segmentation = "1.10.0" unicode-segmentation = "1.10.0"
termsize = "0.1.6"
roc_build = {path = "../compiler/build"} roc_build = {path = "../compiler/build"}
roc_builtins = {path = "../compiler/builtins"} roc_builtins = {path = "../compiler/builtins"}

View file

@ -17,6 +17,7 @@ use rustyline::validate::{self, ValidationContext, ValidationResult, Validator};
use rustyline_derive::{Completer, Helper, Hinter}; use rustyline_derive::{Completer, Helper, Hinter};
use std::borrow::Cow; use std::borrow::Cow;
use target_lexicon::Triple; use target_lexicon::Triple;
use termsize::Size;
pub const PROMPT: &str = concatcp!("\n", BLUE, "»", END_COL, " "); pub const PROMPT: &str = concatcp!("\n", BLUE, "»", END_COL, " ");
pub const CONT_PROMPT: &str = concatcp!(BLUE, "", END_COL, " "); pub const CONT_PROMPT: &str = concatcp!(BLUE, "", END_COL, " ");
@ -556,7 +557,12 @@ fn format_output(
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
const VAR_NAME_PREFIX: &str = " # "; // e.g. in " # val1" const VAR_NAME_PREFIX: &str = " # "; // e.g. in " # val1"
const VAR_NAME_COLUMN: usize = 80; // Right-align the var_name at this column const VAR_NAME_COLUMN_MAX: u16 = 80; // Right-align the var_name at this column
let var_name_column = match termsize::get() {
Some(Size { cols, rows: _ }) => cols.min(VAR_NAME_COLUMN_MAX) as usize,
None => VAR_NAME_COLUMN_MAX as usize,
};
let expr_with_type = format!("{expr}{EXPR_TYPE_SEPARATOR}{expr_type}"); let expr_with_type = format!("{expr}{EXPR_TYPE_SEPARATOR}{expr_type}");
@ -569,11 +575,11 @@ fn format_output(
.count(); .count();
let var_name_len = let var_name_len =
var_name.graphemes(true).count() + VAR_NAME_PREFIX.graphemes(true).count(); var_name.graphemes(true).count() + VAR_NAME_PREFIX.graphemes(true).count();
let spaces_needed = if last_line_len + var_name_len > VAR_NAME_COLUMN { let spaces_needed = if last_line_len + var_name_len > var_name_column {
buf.push('\n'); buf.push('\n');
VAR_NAME_COLUMN - var_name_len var_name_column - var_name_len
} else { } else {
VAR_NAME_COLUMN - last_line_len - var_name_len var_name_column - last_line_len - var_name_len
}; };
for _ in 0..spaces_needed { for _ in 0..spaces_needed {