From 58d73d4c233beccc4ca84f9829bfdabfa0535937 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 7 Oct 2021 10:32:39 -0500 Subject: [PATCH] moved `grid` to it's own crate named `nu-term-grid` --- Cargo.lock | 10 ++++++- Cargo.toml | 1 + crates/nu-command/Cargo.toml | 2 +- crates/nu-command/src/viewers/griddle.rs | 2 +- crates/nu-command/src/viewers/mod.rs | 2 -- crates/nu-term-grid/.gitignore | 22 ++++++++++++++ crates/nu-term-grid/Cargo.toml | 15 ++++++++++ .../src/viewers => nu-term-grid/src}/grid.rs | 2 +- crates/nu-term-grid/src/lib.rs | 3 ++ crates/nu-term-grid/src/main.rs | 30 +++++++++++++++++++ 10 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 crates/nu-term-grid/.gitignore create mode 100644 crates/nu-term-grid/Cargo.toml rename crates/{nu-command/src/viewers => nu-term-grid/src}/grid.rs (99%) create mode 100644 crates/nu-term-grid/src/lib.rs create mode 100644 crates/nu-term-grid/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 324510e4a..a17184eea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,6 +294,7 @@ dependencies = [ "nu-path", "nu-protocol", "nu-table", + "nu-term-grid", "pretty_assertions", "reedline", "tempfile", @@ -523,10 +524,10 @@ dependencies = [ "nu-path", "nu-protocol", "nu-table", + "nu-term-grid", "sysinfo", "terminal_size", "thiserror", - "unicode-width", ] [[package]] @@ -589,6 +590,13 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "nu-term-grid" +version = "0.36.0" +dependencies = [ + "unicode-width", +] + [[package]] name = "num-integer" version = "0.1.44" diff --git a/Cargo.toml b/Cargo.toml index 826e3d542..5f43cb117 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ nu-parser = { path="./crates/nu-parser" } nu-path = { path="./crates/nu-path" } nu-protocol = { path = "./crates/nu-protocol" } nu-table = { path = "./crates/nu-table" } +nu-term-grid = { path = "./crates/nu-term-grid" } miette = "3.0.0" # mimalloc = { version = "*", default-features = false } diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index ba09d1e3b..4d0f13c78 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -11,11 +11,11 @@ nu-json = { path = "../nu-json" } nu-path = { path = "../nu-path" } nu-protocol = { path = "../nu-protocol" } nu-table = { path = "../nu-table" } +nu-term-grid = { path = "../nu-term-grid" } # Potential dependencies for extras glob = "0.3.0" thiserror = "1.0.29" sysinfo = "0.20.4" chrono = { version="0.4.19", features=["serde"] } -unicode-width = "0.1.9" terminal_size = "0.1.17" diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index 3a9d7433c..c94781efe 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -1,10 +1,10 @@ -use super::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions}; use nu_engine::CallExt; use nu_protocol::{ ast::{Call, PathMember}, engine::{Command, EvaluationContext}, Signature, Span, SyntaxShape, Value, }; +use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions}; use terminal_size::{Height, Width}; //{Alignment, Cell, Direction, Filling, Grid, GridOptions}; pub struct Griddle; diff --git a/crates/nu-command/src/viewers/mod.rs b/crates/nu-command/src/viewers/mod.rs index a2f76b56c..3ff09eff3 100644 --- a/crates/nu-command/src/viewers/mod.rs +++ b/crates/nu-command/src/viewers/mod.rs @@ -1,7 +1,5 @@ -pub mod grid; mod griddle; mod table; -pub use grid::Grid; pub use griddle::Griddle; pub use table::Table; diff --git a/crates/nu-term-grid/.gitignore b/crates/nu-term-grid/.gitignore new file mode 100644 index 000000000..4c234e523 --- /dev/null +++ b/crates/nu-term-grid/.gitignore @@ -0,0 +1,22 @@ +/target +/scratch +**/*.rs.bk +history.txt +tests/fixtures/nuplayground +crates/*/target + +# Debian/Ubuntu +debian/.debhelper/ +debian/debhelper-build-stamp +debian/files +debian/nu.substvars +debian/nu/ + +# macOS junk +.DS_Store + +# JetBrains' IDE items +.idea/* + +# VSCode's IDE items +.vscode/* diff --git a/crates/nu-term-grid/Cargo.toml b/crates/nu-term-grid/Cargo.toml new file mode 100644 index 000000000..183de8ecc --- /dev/null +++ b/crates/nu-term-grid/Cargo.toml @@ -0,0 +1,15 @@ +[package] +authors = ["The Nu Project Contributors"] +description = "Nushell grid printing" +edition = "2018" +license = "MIT" +name = "nu-term-grid" +version = "0.36.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[[bin]] +name = "grid" +path = "src/main.rs" + +[dependencies] +unicode-width = "0.1.9" diff --git a/crates/nu-command/src/viewers/grid.rs b/crates/nu-term-grid/src/grid.rs similarity index 99% rename from crates/nu-command/src/viewers/grid.rs rename to crates/nu-term-grid/src/grid.rs index 8856bdb8a..f15b2fe37 100644 --- a/crates/nu-command/src/viewers/grid.rs +++ b/crates/nu-term-grid/src/grid.rs @@ -13,7 +13,7 @@ //! needed. For example: //! //! ```rust -//! use nu_command::grid::{Grid, GridOptions, Direction, Filling, Cell}; +//! use nu_term_grid::grid::{Grid, GridOptions, Direction, Filling, Cell}; //! //! let mut grid = Grid::new(GridOptions { //! filling: Filling::Spaces(1), diff --git a/crates/nu-term-grid/src/lib.rs b/crates/nu-term-grid/src/lib.rs new file mode 100644 index 000000000..79b146593 --- /dev/null +++ b/crates/nu-term-grid/src/lib.rs @@ -0,0 +1,3 @@ +pub mod grid; + +pub use grid::Grid; diff --git a/crates/nu-term-grid/src/main.rs b/crates/nu-term-grid/src/main.rs new file mode 100644 index 000000000..a9ef42679 --- /dev/null +++ b/crates/nu-term-grid/src/main.rs @@ -0,0 +1,30 @@ +use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions}; + +// This produces: +// +// 1 | 128 | 16384 | 2097152 | 268435456 | 34359738368 | 4398046511104 +// 2 | 256 | 32768 | 4194304 | 536870912 | 68719476736 | 8796093022208 +// 4 | 512 | 65536 | 8388608 | 1073741824 | 137438953472 | 17592186044416 +// 8 | 1024 | 131072 | 16777216 | 2147483648 | 274877906944 | 35184372088832 +// 16 | 2048 | 262144 | 33554432 | 4294967296 | 549755813888 | 70368744177664 +// 32 | 4096 | 524288 | 67108864 | 8589934592 | 1099511627776 | 140737488355328 +// 64 | 8192 | 1048576 | 134217728 | 17179869184 | 2199023255552 | + +fn main() { + let mut grid = Grid::new(GridOptions { + direction: Direction::TopToBottom, + filling: Filling::Text(" | ".into()), + }); + + for i in 0..48 { + let mut cell = Cell::from(format!("{}", 2_isize.pow(i))); + cell.alignment = Alignment::Right; + grid.add(cell) + } + + if let Some(grid_display) = grid.fit_into_width(80) { + println!("{}", grid_display); + } else { + println!("Couldn't fit grid into 80 columns!"); + } +}