Tweak auto-rotate for single row output (#1861)

* added helper to convert data to strings
added ability to auto-rotate single row output
if row will be greater than terminal width

* Added pivot_to_fit config value

* Added ColumnPath to convert_to_string helper

* Figured out I had to run `cargo fmt --all -- --check`

Co-authored-by: Darren Schroeder <fdncred@hotmail.com>
This commit is contained in:
Darren Schroeder
2020-05-21 11:30:58 -05:00
committed by GitHub
parent 97b9c078b1
commit 5de30d0ae5
4 changed files with 45 additions and 2 deletions

View File

@ -84,6 +84,10 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
Some(val) => val.is_true(),
_ => false,
};
let pivot_to_fit = match config::config(Tag::unknown())?.get("pivot_to_fit") {
Some(val) => val.is_true(),
_ => false,
};
Ok(OutputStream::new(async_stream! {
let (mut input_stream, context) = RunnableContextWithoutInput::convert(context);
@ -221,7 +225,14 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
yield Err(e);
}
Value { value: UntaggedValue::Row(row), ..} if !no_auto_pivot => {
Value { value: UntaggedValue::Row(row), ..} if !no_auto_pivot
|| (pivot_to_fit && // Or if the row character count + number of headers * 2 (for padding) > terminal width
(row.entries.iter().map(|(k,v)| v.convert_to_string())
.collect::<Vec<_>>().iter()
.fold(0, |acc, len| acc + len.len())
+
(row.entries.iter().map(|(k,_)| k.chars()).count() * 2))
> textwrap::termwidth()) => {
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
use prettytable::{color, Attr, Cell, Row, Table};
use crate::data::value::{format_leaf, style_leaf};