From 4d5f1f6023e7ca68110af166adb3594196fd36c9 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 24 Nov 2019 13:56:19 +1300 Subject: [PATCH] Revert some of the recent styled string changes --- src/commands/debug.rs | 13 ++--- src/commands/what.rs | 4 +- src/data/base.rs | 13 +++-- src/data/base/shape.rs | 13 +++-- src/format/table.rs | 125 +++++++++++++++++++---------------------- 5 files changed, 78 insertions(+), 90 deletions(-) diff --git a/src/commands/debug.rs b/src/commands/debug.rs index f9639b7263..08bd8259be 100644 --- a/src/commands/debug.rs +++ b/src/commands/debug.rs @@ -30,13 +30,10 @@ impl WholeStreamCommand for Debug { fn debug_value( _args: DebugArgs, - RunnableContext { mut input, .. }: RunnableContext, + RunnableContext { input, .. }: RunnableContext, ) -> Result { - let stream = async_stream! { - while let Some(row) = input.values.next().await { - yield ReturnSuccess::debug_value(row.clone()) - } - }; - - Ok(stream) + Ok(input + .values + .map(|v| ReturnSuccess::value(Value::string(format!("{:?}", v)).tagged_unknown())) + .to_output_stream()) } diff --git a/src/commands/what.rs b/src/commands/what.rs index c6d2ebfb52..6440a6c209 100644 --- a/src/commands/what.rs +++ b/src/commands/what.rs @@ -34,14 +34,14 @@ impl WholeStreamCommand for What { pub fn what( WhatArgs {}: WhatArgs, - RunnableContext { input, host, .. }: RunnableContext, + RunnableContext { input, .. }: RunnableContext, ) -> Result { let stream = async_stream! { let values = input.values; pin_mut!(values); while let Some(row) = values.next().await { - let name = row.format_type(host.clone().lock().unwrap().width()); + let name = row.format_leaf().pretty_debug().plain_string(100000); yield ReturnSuccess::value(Value::string(name).tagged(Tag::unknown_anchor(row.tag.span))); } }; diff --git a/src/data/base.rs b/src/data/base.rs index 0579e7017e..57b920bc44 100644 --- a/src/data/base.rs +++ b/src/data/base.rs @@ -3,7 +3,7 @@ mod property_get; pub(crate) mod shape; use crate::context::CommandRegistry; -use crate::data::base::shape::{Column, InlineShape, TypeShape}; +use crate::data::base::shape::{InlineShape, TypeShape}; use crate::data::TaggedDictBuilder; use crate::errors::ShellError; use crate::evaluate::{evaluate_baseline_expr, Scope}; @@ -439,6 +439,7 @@ impl Value { } } + #[allow(unused)] pub(crate) fn format_type(&self, width: usize) -> String { TypeShape::from_value(self).colored_string(width) } @@ -447,11 +448,11 @@ impl Value { InlineShape::from_value(self).format().pretty_debug() } - pub(crate) fn format_for_column(&self, column: impl Into) -> DebugDocBuilder { - InlineShape::from_value(self) - .format_for_column(column) - .pretty_debug() - } + // pub(crate) fn format_for_column(&self, column: impl Into) -> DebugDocBuilder { + // InlineShape::from_value(self) + // .format_for_column(column) + // .pretty_debug() + // } pub(crate) fn style_leaf(&self) -> &'static str { match self { diff --git a/src/data/base/shape.rs b/src/data/base/shape.rs index 751b141f45..fb651cf69a 100644 --- a/src/data/base/shape.rs +++ b/src/data/base/shape.rs @@ -22,6 +22,7 @@ use std::path::PathBuf; It also serves as the primary vehicle for pretty-printing. */ +#[allow(unused)] #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum TypeShape { Nothing, @@ -281,12 +282,12 @@ impl InlineShape { } } - pub fn format_for_column(self, column: impl Into) -> FormatInlineShape { - FormatInlineShape { - shape: self, - column: Some(column.into()), - } - } + // pub fn format_for_column(self, column: impl Into) -> FormatInlineShape { + // FormatInlineShape { + // shape: self, + // column: Some(column.into()), + // } + // } pub fn format(self) -> FormatInlineShape { FormatInlineShape { diff --git a/src/format/table.rs b/src/format/table.rs index 3c0ade3c5a..2d337260c8 100644 --- a/src/format/table.rs +++ b/src/format/table.rs @@ -1,7 +1,7 @@ use crate::data::Value; use crate::format::RenderView; use crate::prelude::*; -use crate::traits::{DebugDocBuilder, DebugDocBuilder as b, PrettyDebug}; +use crate::traits::PrettyDebug; use derive_new::new; use textwrap::fill; @@ -58,33 +58,56 @@ impl TableView { let mut entries = vec![]; for (idx, value) in values.iter().enumerate() { - let mut row: Vec<(DebugDocBuilder, &'static str)> = match value { - Tagged { - item: Value::Row(..), - .. - } => headers - .iter() - .enumerate() - .map(|(i, d)| { - let data = value.get_data(d); - return ( - data.borrow().format_for_column(&headers[i]), - data.borrow().style_leaf(), - ); - }) - .collect(), - x => vec![(x.format_leaf(), x.style_leaf())], - }; + let mut row: Vec<(String, &'static str)> = headers + .iter() + .map(|d| { + if d == "" { + match value { + Tagged { + item: Value::Row(..), + .. + } => ( + Value::nothing() + .format_leaf() + .pretty_debug() + .plain_string(100000), + Value::nothing().style_leaf(), + ), + _ => ( + value.format_leaf().pretty_debug().plain_string(100000), + value.style_leaf(), + ), + } + } else { + match value { + Tagged { + item: Value::Row(..), + .. + } => { + let data = value.get_data(d); + ( + data.borrow() + .format_leaf() + .pretty_debug() + .plain_string(100000), + data.borrow().style_leaf(), + ) + } + _ => ( + Value::nothing() + .format_leaf() + .pretty_debug() + .plain_string(100000), + Value::nothing().style_leaf(), + ), + } + } + }) + .collect(); if values.len() > 1 { // Indices are black, bold, right-aligned: - row.insert( - 0, - ( - b::primitive(format!("{}", (starting_idx + idx).to_string())), - "Fdbr", - ), - ); + row.insert(0, (format!("{}", (starting_idx + idx).to_string()), "Fdbr")); } entries.push(row); @@ -96,13 +119,10 @@ impl TableView { headers.insert(0, format!("#")); } - // Different platforms want different amounts of buffer, not sure why - let termwidth = std::cmp::max(textwrap::termwidth(), 20); - for head in 0..headers.len() { let mut current_col_max = 0; for row in 0..values.len() { - let value_length = entries[row][head].0.plain_string(termwidth).chars().count(); + let value_length = entries[row][head].0.chars().count(); if value_length > current_col_max { current_col_max = value_length; } @@ -114,6 +134,9 @@ impl TableView { )); } + // Different platforms want different amounts of buffer, not sure why + let termwidth = std::cmp::max(textwrap::termwidth(), 20); + // Make sure we have enough space for the columns we have let max_num_of_columns = termwidth / 10; @@ -126,7 +149,7 @@ impl TableView { headers.push("...".to_string()); for row in 0..entries.len() { - entries[row].push((b::description("..."), "c")); // ellipsis is centred + entries[row].push(("...".to_string(), "c")); // ellipsis is centred } } @@ -198,54 +221,20 @@ impl TableView { 99999 }; - let mut out_entries: Vec> = Vec::with_capacity(entries.len()); - - for row in entries.iter() { - let mut out_row = Vec::with_capacity(row.len()); - - for _ in row { - out_row.push((String::new(), "Fdbr")); - } - - out_entries.push(out_row); - } - // Wrap cells as needed for head in 0..headers.len() { if max_per_column[head] > max_naive_column_width { - if true_width(&headers[head]) > max_column_width { - headers[head] = fill(&headers[head], max_column_width); - } - - for (i, row) in entries.iter().enumerate() { - let column = &row[head].0; - let column = column.plain_string(max_column_width); - - out_entries[i][head] = (column, row[head].1); - } - } else { - for (i, row) in entries.iter().enumerate() { - let column = &row[head].0; - let column = column.plain_string(max_column_width); - - out_entries[i][head] = (column, row[head].1); + headers[head] = fill(&headers[head], max_column_width); + for row in 0..entries.len() { + entries[row][head].0 = fill(&entries[row][head].0, max_column_width); } } } - Some(TableView { - headers, - entries: out_entries, - }) + Some(TableView { headers, entries }) } } -fn true_width(string: &str) -> usize { - let stripped = console::strip_ansi_codes(string); - - stripped.lines().map(|line| line.len()).max().unwrap_or(0) -} - impl RenderView for TableView { fn render_view(&self, host: &mut dyn Host) -> Result<(), ShellError> { if self.entries.len() == 0 {