diff --git a/Cargo.lock b/Cargo.lock index d5ca8dccb..42479488b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2777,6 +2777,7 @@ dependencies = [ "percent-encoding", "polars", "powierza-coefficient", + "print-positions", "proptest", "quick-xml 0.27.1", "quickcheck", @@ -3894,6 +3895,15 @@ dependencies = [ "yansi", ] +[[package]] +name = "print-positions" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df593470e3ef502e48cb0cfc9a3a61e5f61e967b78e1ed35a67ac615cfbd208" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -5486,9 +5496,9 @@ dependencies = [ [[package]] name = "unicode-segmentation" -version = "1.10.0" +version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fdbf052a0783de01e944a6ce7a8cb939e295b1e7be835a1112c3b9a7f047a5a" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" [[package]] name = "unicode-width" diff --git a/Cargo.toml b/Cargo.toml index 7703cce92..5a0e52e6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,7 +77,12 @@ signal-hook = { version = "0.3.14", default-features = false } winres = "0.1" [target.'cfg(target_family = "unix")'.dependencies] -nix = { version = "0.25", default-features = false, features = ["signal", "process", "fs", "term"] } +nix = { version = "0.25", default-features = false, features = [ + "signal", + "process", + "fs", + "term", +] } atty = "0.2" [dev-dependencies] diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index c38bd1ac8..950957393 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -98,6 +98,7 @@ url = "2.2.1" uuid = { version = "1.2.2", features = ["v4"] } wax = { version = "0.5.0" } which = { version = "4.4.0", optional = true } +print-positions = "0.6.1" [target.'cfg(windows)'.dependencies] winreg = "0.11.0" diff --git a/crates/nu-command/src/conversions/fill.rs b/crates/nu-command/src/conversions/fill.rs index 6ea2af5cc..adaba33f6 100644 --- a/crates/nu-command/src/conversions/fill.rs +++ b/crates/nu-command/src/conversions/fill.rs @@ -5,7 +5,7 @@ use nu_protocol::{ engine::{Command, EngineState, Stack}, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, }; -use unicode_width::UnicodeWidthStr; +use print_positions::print_positions; #[derive(Clone)] pub struct Fill; @@ -225,7 +225,8 @@ fn fill_string(s: &str, args: &Arguments, span: Span) -> Value { fn pad(s: &str, width: usize, pad_char: &str, alignment: FillAlignment, truncate: bool) -> String { // Attribution: Most of this function was taken from https://github.com/ogham/rust-pad and tweaked. Thank you! // Use width instead of len for graphical display - let cols = UnicodeWidthStr::width(s); + + let cols = print_positions(s).count(); if cols >= width { if truncate { diff --git a/crates/nu-command/tests/commands/fill.rs b/crates/nu-command/tests/commands/fill.rs new file mode 100644 index 000000000..972bb562c --- /dev/null +++ b/crates/nu-command/tests/commands/fill.rs @@ -0,0 +1,33 @@ +use nu_test_support::{nu, pipeline}; + +#[test] +fn string_fill_plain() { + let actual = nu!( + cwd: ".", + pipeline( + r#" + "abc" | fill --alignment center --character "+" --width 5 + "# + ) + ); + + assert_eq!(actual.out, "+abc+"); +} + +#[test] +fn string_fill_fancy() { + let actual = nu!( + cwd: ".", + pipeline( + r#" + $"(ansi red)a(ansi green)\u{65}\u{308}(ansi cyan)c(ansi reset)" + | fill --alignment center --character "+" --width 5 + "# + ) + ); + + assert_eq!( + actual.out, + "+\u{1b}[31ma\u{1b}[32me\u{308}\u{1b}[36mc\u{1b}[0m+" + ); +} diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index 232af3ff2..3e95115df 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -23,6 +23,7 @@ mod every; #[cfg(not(windows))] mod exec; mod export_def; +mod fill; mod find; mod first; mod flatten;