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

@ -15,10 +15,11 @@ use crate::value::dict::Dictionary;
use crate::value::iter::{RowValueIter, TableValueIter};
use crate::value::primitive::Primitive;
use crate::value::range::{Range, RangeInclusion};
use crate::{ColumnPath, PathMember};
use crate::{ColumnPath, PathMember, UnspannedPathMember};
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use indexmap::IndexMap;
use itertools::Itertools;
use nu_errors::ShellError;
use nu_source::{AnchorLocation, HasSpan, Span, Spanned, Tag};
use num_bigint::BigInt;
@ -274,6 +275,35 @@ impl Value {
}
}
pub fn convert_to_string(&self) -> String {
match &self.value {
UntaggedValue::Primitive(Primitive::String(s)) => s.clone(),
UntaggedValue::Primitive(Primitive::Date(dt)) => dt.format("%Y-%b-%d").to_string(),
UntaggedValue::Primitive(Primitive::Boolean(x)) => format!("{}", x),
UntaggedValue::Primitive(Primitive::Decimal(x)) => format!("{}", x),
UntaggedValue::Primitive(Primitive::Int(x)) => format!("{}", x),
UntaggedValue::Primitive(Primitive::Bytes(x)) => format!("{}", x),
UntaggedValue::Primitive(Primitive::Path(x)) => format!("{}", x.display()),
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => {
let joined = path
.iter()
.map(|member| match &member.unspanned {
UnspannedPathMember::String(name) => name.to_string(),
UnspannedPathMember::Int(n) => format!("{}", n),
})
.join(".");
if joined.contains(' ') {
format!("\"{}\"", joined)
} else {
joined
}
}
_ => String::from(""),
}
}
pub fn format(&self, fmt: &str) -> Result<String, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Date(dt)) => Ok(dt.format(fmt).to_string()),