2021-12-02 05:59:10 +00:00
|
|
|
use lscolors::{LsColors, Style};
|
2021-12-16 12:17:29 +00:00
|
|
|
use nu_color_config::{get_color_config, style_primitive};
|
2022-01-01 01:39:58 +00:00
|
|
|
use nu_engine::column::get_columns;
|
2021-12-23 21:41:29 +00:00
|
|
|
use nu_engine::{env_to_string, CallExt};
|
2021-09-29 18:25:05 +00:00
|
|
|
use nu_protocol::ast::{Call, PathMember};
|
2021-10-25 16:58:58 +00:00
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2021-11-17 04:22:37 +00:00
|
|
|
use nu_protocol::{
|
2022-02-21 13:52:50 +00:00
|
|
|
Category, Config, DataSource, Example, IntoPipelineData, ListStream, PipelineData,
|
|
|
|
PipelineMetadata, RawStream, ShellError, Signature, Span, SyntaxShape, Value,
|
2021-11-17 04:22:37 +00:00
|
|
|
};
|
2021-11-20 13:12:35 +00:00
|
|
|
use nu_table::{StyledString, TextStyle, Theme};
|
2021-11-09 06:14:00 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Arc;
|
2021-12-03 06:15:23 +00:00
|
|
|
use std::time::Instant;
|
2021-10-08 13:14:32 +00:00
|
|
|
use terminal_size::{Height, Width};
|
2021-09-29 18:25:05 +00:00
|
|
|
|
2021-12-03 06:15:23 +00:00
|
|
|
const STREAM_PAGE_SIZE: usize = 1000;
|
|
|
|
const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100;
|
|
|
|
|
2021-10-25 04:01:02 +00:00
|
|
|
#[derive(Clone)]
|
2021-09-29 18:25:05 +00:00
|
|
|
pub struct Table;
|
|
|
|
|
|
|
|
//NOTE: this is not a real implementation :D. It's just a simple one to test with until we port the real one.
|
|
|
|
impl Command for Table {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"table"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Render the table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
2021-12-23 21:41:29 +00:00
|
|
|
Signature::build("table")
|
|
|
|
.named(
|
2022-02-17 14:55:17 +00:00
|
|
|
"start-number",
|
2021-12-23 21:41:29 +00:00
|
|
|
SyntaxShape::Int,
|
|
|
|
"row number to start viewing from",
|
|
|
|
Some('n'),
|
|
|
|
)
|
|
|
|
.category(Category::Viewers)
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-11-09 06:14:00 +00:00
|
|
|
engine_state: &EngineState,
|
2021-11-14 19:25:57 +00:00
|
|
|
stack: &mut Stack,
|
2021-09-29 18:25:05 +00:00
|
|
|
call: &Call,
|
2021-10-25 04:24:10 +00:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
2021-12-24 07:22:11 +00:00
|
|
|
let head = call.head;
|
2021-11-09 06:14:00 +00:00
|
|
|
let ctrlc = engine_state.ctrlc.clone();
|
2021-12-13 03:16:51 +00:00
|
|
|
let config = stack.get_config().unwrap_or_default();
|
2021-11-20 13:12:35 +00:00
|
|
|
let color_hm = get_color_config(&config);
|
2022-02-17 14:55:17 +00:00
|
|
|
let start_num: Option<i64> = call.get_flag(engine_state, stack, "start-number")?;
|
2021-12-23 21:41:29 +00:00
|
|
|
let row_offset = start_num.unwrap_or_default() as usize;
|
2021-11-09 06:14:00 +00:00
|
|
|
|
2021-10-08 13:14:32 +00:00
|
|
|
let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
|
2021-11-18 05:48:15 +00:00
|
|
|
(w - 1) as usize
|
2021-10-08 13:14:32 +00:00
|
|
|
} else {
|
|
|
|
80usize
|
|
|
|
};
|
|
|
|
|
2021-09-29 18:25:05 +00:00
|
|
|
match input {
|
2022-01-28 18:32:33 +00:00
|
|
|
PipelineData::RawStream(..) => Ok(input),
|
|
|
|
PipelineData::Value(Value::Binary { val, .. }, ..) => Ok(PipelineData::RawStream(
|
|
|
|
RawStream::new(
|
|
|
|
Box::new(
|
|
|
|
vec![Ok(format!("{}\n", nu_pretty_hex::pretty_hex(&val))
|
|
|
|
.as_bytes()
|
|
|
|
.to_vec())]
|
|
|
|
.into_iter(),
|
|
|
|
),
|
2021-12-24 18:15:01 +00:00
|
|
|
ctrlc,
|
2022-01-28 20:32:46 +00:00
|
|
|
head,
|
2021-12-24 18:15:01 +00:00
|
|
|
),
|
|
|
|
head,
|
|
|
|
None,
|
|
|
|
)),
|
2021-12-02 05:59:10 +00:00
|
|
|
PipelineData::Value(Value::List { vals, .. }, ..) => {
|
2021-12-23 21:41:29 +00:00
|
|
|
let table = convert_to_table(row_offset, &vals, ctrlc, &config, call.head)?;
|
2021-09-29 18:25:05 +00:00
|
|
|
|
|
|
|
if let Some(table) = table {
|
2021-12-01 19:20:23 +00:00
|
|
|
let result = nu_table::draw_table(&table, term_width, &color_hm, &config);
|
2021-09-29 18:25:05 +00:00
|
|
|
|
|
|
|
Ok(Value::String {
|
|
|
|
val: result,
|
|
|
|
span: call.head,
|
2021-10-25 04:24:10 +00:00
|
|
|
}
|
|
|
|
.into_pipeline_data())
|
2021-09-29 18:25:05 +00:00
|
|
|
} else {
|
2021-11-06 05:50:33 +00:00
|
|
|
Ok(PipelineData::new(call.head))
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-24 07:22:11 +00:00
|
|
|
PipelineData::ListStream(stream, metadata) => {
|
2021-12-02 05:59:10 +00:00
|
|
|
let stream = match metadata {
|
|
|
|
Some(PipelineMetadata {
|
|
|
|
data_source: DataSource::Ls,
|
|
|
|
}) => {
|
|
|
|
let config = config.clone();
|
|
|
|
let ctrlc = ctrlc.clone();
|
|
|
|
|
2022-01-04 22:30:34 +00:00
|
|
|
let ls_colors = match stack.get_env_var(engine_state, "LS_COLORS") {
|
2021-12-17 01:04:54 +00:00
|
|
|
Some(v) => LsColors::from_string(&env_to_string(
|
|
|
|
"LS_COLORS",
|
|
|
|
v,
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
&config,
|
|
|
|
)?),
|
2022-02-08 17:13:04 +00:00
|
|
|
None => LsColors::from_string("st=0:di=0;38;5;81:so=0;38;5;16;48;5;203:ln=0;38;5;203:cd=0;38;5;203;48;5;236:ex=1;38;5;203:or=0;38;5;16;48;5;203:fi=0:bd=0;38;5;81;48;5;236:ow=0:mi=0;38;5;16;48;5;203:*~=0;38;5;243:no=0:tw=0:pi=0;38;5;16;48;5;81:*.z=4;38;5;203:*.t=0;38;5;48:*.o=0;38;5;243:*.d=0;38;5;48:*.a=1;38;5;203:*.c=0;38;5;48:*.m=0;38;5;48:*.p=0;38;5;48:*.r=0;38;5;48:*.h=0;38;5;48:*.ml=0;38;5;48:*.ll=0;38;5;48:*.gv=0;38;5;48:*.cp=0;38;5;48:*.xz=4;38;5;203:*.hs=0;38;5;48:*css=0;38;5;48:*.ui=0;38;5;149:*.pl=0;38;5;48:*.ts=0;38;5;48:*.gz=4;38;5;203:*.so=1;38;5;203:*.cr=0;38;5;48:*.fs=0;38;5;48:*.bz=4;38;5;203:*.ko=1;38;5;203:*.as=0;38;5;48:*.sh=0;38;5;48:*.pp=0;38;5;48:*.el=0;38;5;48:*.py=0;38;5;48:*.lo=0;38;5;243:*.bc=0;38;5;243:*.cc=0;38;5;48:*.pm=0;38;5;48:*.rs=0;38;5;48:*.di=0;38;5;48:*.jl=0;38;5;48:*.rb=0;38;5;48:*.md=0;38;5;185:*.js=0;38;5;48:*.go=0;38;5;48:*.vb=0;38;5;48:*.hi=0;38;5;243:*.kt=0;38;5;48:*.hh=0;38;5;48:*.cs=0;38;5;48:*.mn=0;38;5;48:*.nb=0;38;5;48:*.7z=4;38;5;203:*.ex=0;38;5;48:*.rm=0;38;5;208:*.ps=0;38;5;186:*.td=0;38;5;48:*.la=0;38;5;243:*.aux=0;38;5;243:*.xmp=0;38;5;149:*.mp4=0;38;5;208:*.rpm=4;38;5;203:*.m4a=0;38;5;208:*.zip=4;38;5;203:*.dll=1;38;5;203:*.bcf=0;38;5;243:*.awk=0;38;5;48:*.aif=0;38;5;208:*.zst=4;38;5;203:*.bak=0;38;5;243:*.tgz=4;38;5;203:*.com=1;38;5;203:*.clj=0;38;5;48:*.sxw=0;38;5;186:*.vob=0;38;5;208:*.fsx=0;38;5;48:*.doc=0;38;5;186:*.mkv=0;38;5;208:*.tbz=4;38;5;203:*.ogg=0;38;5;208:*.wma=0;38;5;208:*.mid=0;38;5;208:*.kex=0;38;5;186:*.out=0;38;5;243:*.ltx=0;38;5;48:*.sql=0;38;5;48:*.ppt=0;38;5;186:*.tex=0;38;5;48:*.odp=0;38;5;186:*.log=0;38;5;243:*.arj=4;38;5;203:*.ipp=0;38;5;48:*.sbt=0;38;5;48:*.jpg=0;38;5;208:*.yml=0;38;5;149:*.txt=0;38;5;185:*.csv=0;38;5;185:*.dox=0;38;5;149:*.pro=0;38;5;149:*.bst=0;38;5;149:*TODO=1:*.mir=0;38;5;48:*.bat=1;38;5;203:*.m4v=0;38;5;208:*.pod=0;38;5;48:*.cfg=0;38;5;149:*.pas=0;38;5;48:*.tml=0;38;5;149:*.bib=0;38;5;149:*.ini=0;38;5;149:*.apk=4;38;5;203:*.h++=0;38;5;48:*.pyc=0;38;5;243:*.img=4;38;5;203:*.rst=0;38;5;185:*.swf=0;38;5;208:*.htm=0;38;5;185:*.ttf=0;38;5;208:*.elm=0;38;5;48:*hgrc=0;38;5;149:*.bmp=0;38;5;208:*.fsi=0;38;5;48:*.pgm=0;38;5;208:*.dpr=0;38;5;48:*.xls=0;38;5;186:*.tcl=0;38;5;48:*.mli=0;38;5;48:*.ppm=0;38;5;208:*.bbl=0;38;5;243:*.lua=0;38;5;48:*.asa=0;38;5;48:*.pbm=0;38;5;208:*.avi=0;38;5;208:*.def=0;38;5;48:*.mov=0;38;5;208:*.hxx=0;38;5;48:*.tif=0;38;5;208:*.fon=0;38;5;208:*.zsh=0;38;5;48:*.png=0;38;5;208:*.inc=0;38;5;48:*.jar=4;38;5;203:*.swp=0;38;5;243:*.pid=0;38;5;243:*.gif=0;38;5;208:*.ind=0;38;5;243:*.erl=0;38;5;48:*.ilg=0;38;5;243:*.eps=0;38;5;208:*.tsx=0;38;5;48:*.git=0;38;5;243:*.inl=0;38;5;48:*.rtf=0;38;5;186:*.hpp=0;38;5;48:*.kts=0;38;5;48:*.deb=4;38;5;203:*.svg=0;38;5;208:*.pps=0;38;5;186:*.ps1=0;38;5;48:*.c++=0;38;5;48:*.cpp=0;38;5;48:*.bsh=0;38;5;48:*.php=0;38;5;48:*.exs=0;38;5;48:*.toc=0;38;5;243:*.mp3=0;38;5;208:*.epp=0;38;5;48:*.rar=4;38;5;203:*.wav=0;38;5;208:*.xlr=0;38;5;186:*.tmp=0;38;5;243:*.cxx=0;38;5;48:*.iso=4;38;5;203:*.dmg=4;38;5;203:*.gvy=0;38;5;48:*.bin=4;38;5;203:*.wmv=0;38;5;208:*.blg=0;38;5;243:*.ods=0;38;5;186:*.psd=0;38;5;208:*.mpg=0;38;5;208:*.dot=0;38;5;48:*.cgi=0;38;5;48:*.xml=0;38;5;185:*.htc=0;38;5;48:*.ics=0;38;5;186:*.bz2=4;38;5;203:*.tar=4;38;5;203:*.csx=0;38;5;48:*.ico=0;38;5;208:*.sxi=0;38;5;186:*.nix=0;38;5;149:*.pkg=4;38;5;203:*.bag=4;38;5;203:*.fnt=0;38;5;208:*.idx=0;38;5;243:*.xcf=0;38;5;208:*.exe=1;38;5;203:*.flv=0;38;5;208:*.fls=0;38;5;243:*.otf=0;38;5;208:*.vcd=4;38;5;203:*.vim=0;38;5;48:*.sty=0;38;5;243:*.pdf=0;38;5;186:*.odt=0;38;5;186:*.purs=0;38;5;48:*.h264=0;38;5;208:*.jpeg=0;38;5;208:*.dart=0;38;5;48:*.pptx=0;38;5;186:*.lock=0;38;5;243:*.bash=0;38;5;48:*.rlib=0;38;5;243:*.hgrc=0;38;5;149:*.psm1=0;38;5;48:*.toml=0;38;5;149:*.tbz2=4;38;5;203:*.yaml=0;38;5;149:*.make=0;38;5;149:*.orig=0;38;5;243:*.html=0;38;5;185:*.fish=0;38;5;48:*.diff=0;38;5;48:*.xlsx=0;38;5;186:*.docx=0;38;5;186:*.json=0;38;5;149:*.psd1=0;38;5;48:*.tiff=0;38;5;208:*.flac=0;38;5;208:*.java=0;38;5;48:*.less=0;38;5;48:*.mpeg=0;38;5;208:*.conf=0;38;5;149:*.lisp=0;38;5;48:*.epub=0;38;5;1
|
2021-12-02 05:59:10 +00:00
|
|
|
};
|
|
|
|
|
2022-01-28 18:32:33 +00:00
|
|
|
ListStream::from_stream(
|
2021-12-02 05:59:10 +00:00
|
|
|
stream.map(move |mut x| match &mut x {
|
|
|
|
Value::Record { cols, vals, .. } => {
|
|
|
|
let mut idx = 0;
|
|
|
|
|
|
|
|
while idx < cols.len() {
|
|
|
|
if cols[idx] == "name" {
|
|
|
|
if let Some(Value::String { val: path, span }) =
|
|
|
|
vals.get(idx)
|
|
|
|
{
|
|
|
|
match std::fs::symlink_metadata(&path) {
|
|
|
|
Ok(metadata) => {
|
|
|
|
let style = ls_colors
|
|
|
|
.style_for_path_with_metadata(
|
|
|
|
path.clone(),
|
|
|
|
Some(&metadata),
|
|
|
|
);
|
|
|
|
let ansi_style = style
|
|
|
|
.map(Style::to_crossterm_style)
|
|
|
|
.unwrap_or_default();
|
|
|
|
let use_ls_colors = config.use_ls_colors;
|
|
|
|
|
|
|
|
if use_ls_colors {
|
|
|
|
vals[idx] = Value::String {
|
|
|
|
val: ansi_style
|
|
|
|
.apply(path)
|
|
|
|
.to_string(),
|
|
|
|
span: *span,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
let style =
|
|
|
|
ls_colors.style_for_path(path.clone());
|
|
|
|
let ansi_style = style
|
|
|
|
.map(Style::to_crossterm_style)
|
|
|
|
.unwrap_or_default();
|
|
|
|
let use_ls_colors = config.use_ls_colors;
|
|
|
|
|
|
|
|
if use_ls_colors {
|
|
|
|
vals[idx] = Value::String {
|
|
|
|
val: ansi_style
|
|
|
|
.apply(path)
|
|
|
|
.to_string(),
|
|
|
|
span: *span,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
x
|
|
|
|
}
|
|
|
|
_ => x,
|
|
|
|
}),
|
|
|
|
ctrlc,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => stream,
|
|
|
|
};
|
|
|
|
|
2021-12-03 06:15:23 +00:00
|
|
|
let head = call.head;
|
2021-09-29 18:25:05 +00:00
|
|
|
|
2022-01-28 18:32:33 +00:00
|
|
|
Ok(PipelineData::RawStream(
|
|
|
|
RawStream::new(
|
|
|
|
Box::new(PagingTableCreator {
|
2021-12-24 07:22:11 +00:00
|
|
|
row_offset,
|
|
|
|
config,
|
|
|
|
ctrlc: ctrlc.clone(),
|
|
|
|
head,
|
|
|
|
stream,
|
2022-01-28 18:32:33 +00:00
|
|
|
}),
|
2021-12-24 07:22:11 +00:00
|
|
|
ctrlc,
|
2022-01-28 20:32:46 +00:00
|
|
|
head,
|
2021-12-24 07:22:11 +00:00
|
|
|
),
|
2021-12-03 06:15:23 +00:00
|
|
|
head,
|
2021-12-24 07:22:11 +00:00
|
|
|
None,
|
|
|
|
))
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
2021-12-02 05:59:10 +00:00
|
|
|
PipelineData::Value(Value::Record { cols, vals, .. }, ..) => {
|
2021-10-01 06:01:22 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
for (c, v) in cols.into_iter().zip(vals.into_iter()) {
|
|
|
|
output.push(vec![
|
|
|
|
StyledString {
|
|
|
|
contents: c,
|
2021-11-20 13:12:35 +00:00
|
|
|
style: TextStyle::default_field(),
|
2021-10-01 06:01:22 +00:00
|
|
|
},
|
|
|
|
StyledString {
|
2021-12-02 21:07:44 +00:00
|
|
|
contents: v.into_abbreviated_string(&config),
|
2021-11-20 13:12:35 +00:00
|
|
|
style: TextStyle::default(),
|
2021-10-01 06:01:22 +00:00
|
|
|
},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
let table = nu_table::Table {
|
|
|
|
headers: vec![],
|
|
|
|
data: output,
|
2021-11-14 19:25:57 +00:00
|
|
|
theme: load_theme_from_config(&config),
|
2021-10-01 06:01:22 +00:00
|
|
|
};
|
|
|
|
|
2021-12-01 19:20:23 +00:00
|
|
|
let result = nu_table::draw_table(&table, term_width, &color_hm, &config);
|
2021-10-01 06:01:22 +00:00
|
|
|
|
|
|
|
Ok(Value::String {
|
|
|
|
val: result,
|
|
|
|
span: call.head,
|
2021-10-25 04:24:10 +00:00
|
|
|
}
|
|
|
|
.into_pipeline_data())
|
2021-10-01 06:01:22 +00:00
|
|
|
}
|
2021-12-02 05:59:10 +00:00
|
|
|
PipelineData::Value(Value::Error { error }, ..) => Err(error),
|
|
|
|
PipelineData::Value(Value::CustomValue { val, span }, ..) => {
|
2021-11-23 08:14:40 +00:00
|
|
|
let base_pipeline = val.to_base_value(span)?.into_pipeline_data();
|
|
|
|
self.run(engine_state, stack, call, base_pipeline)
|
|
|
|
}
|
2021-09-29 18:25:05 +00:00
|
|
|
x => Ok(x),
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 13:52:50 +00:00
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
let span = Span::test_data();
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "List the files in current directory with index number start from 1.",
|
|
|
|
example: r#"ls | table -n 1"#,
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Render data in table view",
|
|
|
|
example: r#"echo [[a b]; [1 2] [3 4]] | table"#,
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::Record {
|
|
|
|
cols: vec!["a".to_string(), "b".to_string()],
|
|
|
|
vals: vec![Value::test_int(1), Value::test_int(2)],
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Value::Record {
|
|
|
|
cols: vec!["a".to_string(), "b".to_string()],
|
|
|
|
vals: vec![Value::test_int(3), Value::test_int(4)],
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 17:35:40 +00:00
|
|
|
fn convert_to_table(
|
2021-12-03 06:15:23 +00:00
|
|
|
row_offset: usize,
|
2021-12-20 21:03:47 +00:00
|
|
|
input: &[Value],
|
2021-11-09 06:14:00 +00:00
|
|
|
ctrlc: Option<Arc<AtomicBool>>,
|
2021-11-14 19:25:57 +00:00
|
|
|
config: &Config,
|
2021-12-19 07:46:13 +00:00
|
|
|
head: Span,
|
2021-10-11 17:35:40 +00:00
|
|
|
) -> Result<Option<nu_table::Table>, ShellError> {
|
2021-12-20 21:03:47 +00:00
|
|
|
let mut headers = get_columns(input);
|
|
|
|
let mut input = input.iter().peekable();
|
2021-12-07 20:06:14 +00:00
|
|
|
let color_hm = get_color_config(config);
|
|
|
|
let float_precision = config.float_precision as usize;
|
2021-09-29 18:25:05 +00:00
|
|
|
|
2021-12-20 21:03:47 +00:00
|
|
|
if input.peek().is_some() {
|
2021-09-29 18:25:05 +00:00
|
|
|
if !headers.is_empty() {
|
|
|
|
headers.insert(0, "#".into());
|
|
|
|
}
|
|
|
|
|
2021-11-20 13:12:35 +00:00
|
|
|
// Vec of Vec of String1, String2 where String1 is datatype and String2 is value
|
|
|
|
let mut data: Vec<Vec<(String, String)>> = Vec::new();
|
2021-09-29 18:25:05 +00:00
|
|
|
|
2021-12-20 21:03:47 +00:00
|
|
|
for (row_num, item) in input.enumerate() {
|
2021-11-09 06:14:00 +00:00
|
|
|
if let Some(ctrlc) = &ctrlc {
|
|
|
|
if ctrlc.load(Ordering::SeqCst) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 17:35:40 +00:00
|
|
|
if let Value::Error { error } = item {
|
2021-12-20 21:03:47 +00:00
|
|
|
return Err(error.clone());
|
2021-10-11 17:35:40 +00:00
|
|
|
}
|
2021-11-20 13:12:35 +00:00
|
|
|
// String1 = datatype, String2 = value as string
|
2021-12-03 06:15:23 +00:00
|
|
|
let mut row: Vec<(String, String)> =
|
|
|
|
vec![("string".to_string(), (row_num + row_offset).to_string())];
|
2021-09-29 18:25:05 +00:00
|
|
|
|
2021-12-21 09:05:16 +00:00
|
|
|
if headers.is_empty() {
|
2022-01-16 22:40:40 +00:00
|
|
|
row.push((
|
|
|
|
item.get_type().to_string(),
|
|
|
|
item.into_abbreviated_string(config),
|
|
|
|
))
|
2021-12-21 09:05:16 +00:00
|
|
|
} else {
|
|
|
|
for header in headers.iter().skip(1) {
|
|
|
|
let result = match item {
|
|
|
|
Value::Record { .. } => {
|
|
|
|
item.clone().follow_cell_path(&[PathMember::String {
|
|
|
|
val: header.into(),
|
|
|
|
span: head,
|
|
|
|
}])
|
|
|
|
}
|
|
|
|
_ => Ok(item.clone()),
|
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(value) => row.push((
|
|
|
|
(&value.get_type()).to_string(),
|
|
|
|
value.into_abbreviated_string(config),
|
|
|
|
)),
|
|
|
|
Err(_) => row.push(("empty".to_string(), "❎".into())),
|
|
|
|
}
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.push(row);
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:35:40 +00:00
|
|
|
Ok(Some(nu_table::Table {
|
2021-09-29 18:25:05 +00:00
|
|
|
headers: headers
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| StyledString {
|
|
|
|
contents: x,
|
2021-11-20 13:12:35 +00:00
|
|
|
style: TextStyle {
|
|
|
|
alignment: nu_table::Alignment::Center,
|
|
|
|
color_style: Some(color_hm["header"]),
|
|
|
|
},
|
2021-09-29 18:25:05 +00:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
data: data
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
x.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(col, y)| {
|
|
|
|
if col == 0 {
|
|
|
|
StyledString {
|
2021-11-20 13:12:35 +00:00
|
|
|
contents: y.1,
|
|
|
|
style: TextStyle {
|
2021-12-01 15:17:50 +00:00
|
|
|
alignment: nu_table::Alignment::Right,
|
|
|
|
color_style: Some(color_hm["row_index"]),
|
2021-11-20 13:12:35 +00:00
|
|
|
},
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
2021-12-07 20:06:14 +00:00
|
|
|
} else if &y.0 == "float" {
|
|
|
|
// set dynamic precision from config
|
|
|
|
let precise_number =
|
|
|
|
match convert_with_precision(&y.1, float_precision) {
|
|
|
|
Ok(num) => num,
|
|
|
|
Err(e) => e.to_string(),
|
|
|
|
};
|
|
|
|
StyledString {
|
|
|
|
contents: precise_number,
|
|
|
|
style: style_primitive(&y.0, &color_hm),
|
|
|
|
}
|
2021-09-29 18:25:05 +00:00
|
|
|
} else {
|
|
|
|
StyledString {
|
2021-11-20 13:12:35 +00:00
|
|
|
contents: y.1,
|
|
|
|
style: style_primitive(&y.0, &color_hm),
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<StyledString>>()
|
|
|
|
})
|
|
|
|
.collect(),
|
2021-11-14 19:25:57 +00:00
|
|
|
theme: load_theme_from_config(config),
|
2021-10-11 17:35:40 +00:00
|
|
|
}))
|
2021-09-29 18:25:05 +00:00
|
|
|
} else {
|
2021-10-11 17:35:40 +00:00
|
|
|
Ok(None)
|
2021-09-29 18:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 19:25:57 +00:00
|
|
|
|
2021-12-07 20:06:14 +00:00
|
|
|
fn convert_with_precision(val: &str, precision: usize) -> Result<String, ShellError> {
|
|
|
|
// vall will always be a f64 so convert it with precision formatting
|
|
|
|
let val_float = match val.trim().parse::<f64>() {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::LabeledError(
|
|
|
|
format!("error converting string [{}] to f64", &val),
|
|
|
|
e.to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(format!("{:.prec$}", val_float, prec = precision))
|
|
|
|
}
|
|
|
|
|
2021-12-03 06:15:23 +00:00
|
|
|
struct PagingTableCreator {
|
|
|
|
head: Span,
|
2022-01-28 18:32:33 +00:00
|
|
|
stream: ListStream,
|
2021-12-03 06:15:23 +00:00
|
|
|
ctrlc: Option<Arc<AtomicBool>>,
|
|
|
|
config: Config,
|
|
|
|
row_offset: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for PagingTableCreator {
|
2022-01-28 18:32:33 +00:00
|
|
|
type Item = Result<Vec<u8>, ShellError>;
|
2021-12-03 06:15:23 +00:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let mut batch = vec![];
|
|
|
|
|
|
|
|
let start_time = Instant::now();
|
|
|
|
|
|
|
|
let mut idx = 0;
|
|
|
|
|
|
|
|
// Pull from stream until time runs out or we have enough items
|
|
|
|
for item in self.stream.by_ref() {
|
|
|
|
batch.push(item);
|
|
|
|
idx += 1;
|
|
|
|
|
|
|
|
if idx % STREAM_TIMEOUT_CHECK_INTERVAL == 0 {
|
|
|
|
let end_time = Instant::now();
|
|
|
|
|
|
|
|
// If we've been buffering over a second, go ahead and send out what we have so far
|
|
|
|
if (end_time - start_time).as_secs() >= 1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx == STREAM_PAGE_SIZE {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ctrlc) = &self.ctrlc {
|
|
|
|
if ctrlc.load(Ordering::SeqCst) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let color_hm = get_color_config(&self.config);
|
|
|
|
|
|
|
|
let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
|
|
|
|
(w - 1) as usize
|
|
|
|
} else {
|
|
|
|
80usize
|
|
|
|
};
|
|
|
|
|
|
|
|
let table = convert_to_table(
|
|
|
|
self.row_offset,
|
2021-12-20 21:03:47 +00:00
|
|
|
&batch,
|
2021-12-03 06:15:23 +00:00
|
|
|
self.ctrlc.clone(),
|
|
|
|
&self.config,
|
2021-12-19 07:46:13 +00:00
|
|
|
self.head,
|
2021-12-03 06:15:23 +00:00
|
|
|
);
|
|
|
|
self.row_offset += idx;
|
|
|
|
|
|
|
|
match table {
|
|
|
|
Ok(Some(table)) => {
|
|
|
|
let result = nu_table::draw_table(&table, term_width, &color_hm, &self.config);
|
|
|
|
|
2022-01-28 18:32:33 +00:00
|
|
|
Some(Ok(result.as_bytes().to_vec()))
|
2021-12-03 06:15:23 +00:00
|
|
|
}
|
2021-12-24 07:22:11 +00:00
|
|
|
Err(err) => Some(Err(err)),
|
2021-12-03 06:15:23 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 19:25:57 +00:00
|
|
|
fn load_theme_from_config(config: &Config) -> Theme {
|
|
|
|
match config.table_mode.as_str() {
|
|
|
|
"basic" => nu_table::Theme::basic(),
|
|
|
|
"compact" => nu_table::Theme::compact(),
|
|
|
|
"compact_double" => nu_table::Theme::compact_double(),
|
|
|
|
"light" => nu_table::Theme::light(),
|
|
|
|
"with_love" => nu_table::Theme::with_love(),
|
|
|
|
"rounded" => nu_table::Theme::rounded(),
|
|
|
|
"reinforced" => nu_table::Theme::reinforced(),
|
|
|
|
"heavy" => nu_table::Theme::heavy(),
|
|
|
|
"none" => nu_table::Theme::none(),
|
|
|
|
_ => nu_table::Theme::rounded(),
|
|
|
|
}
|
|
|
|
}
|