2019-08-15 07:02:02 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2020-08-17 09:00:04 +02:00
|
|
|
use crate::data::config::table::HasTableProperties;
|
|
|
|
use crate::data::config::NuConfig as TableConfiguration;
|
2020-06-20 05:41:53 +02:00
|
|
|
use crate::data::value::{format_leaf, style_leaf};
|
2019-06-21 06:20:06 +02:00
|
|
|
use crate::prelude::*;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-06-13 21:13:36 +02:00
|
|
|
use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value};
|
2020-08-17 09:00:04 +02:00
|
|
|
use nu_table::{draw_table, Alignment, StyledString, TextStyle};
|
2020-01-25 21:09:51 +01:00
|
|
|
use std::time::Instant;
|
2019-06-21 06:20:06 +02:00
|
|
|
|
2020-01-25 21:09:51 +01:00
|
|
|
const STREAM_PAGE_SIZE: usize = 1000;
|
|
|
|
const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100;
|
2020-01-25 04:13:12 +01:00
|
|
|
|
2019-08-03 04:17:28 +02:00
|
|
|
pub struct Table;
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2019-08-15 07:02:02 +02:00
|
|
|
impl WholeStreamCommand for Table {
|
2019-08-03 04:17:28 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"table"
|
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-10-28 06:15:35 +01:00
|
|
|
Signature::build("table").named(
|
|
|
|
"start_number",
|
|
|
|
SyntaxShape::Number,
|
|
|
|
"row number to start viewing from",
|
2020-02-12 03:24:31 +01:00
|
|
|
Some('n'),
|
2019-10-28 06:15:35 +01:00
|
|
|
)
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"View the contents of the pipeline as a table."
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2019-08-03 04:17:28 +02:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-08-17 09:00:04 +02:00
|
|
|
table(TableConfiguration::new(), args, registry).await
|
2019-06-21 06:20:06 +02:00
|
|
|
}
|
2019-08-03 04:17:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 09:00:04 +02:00
|
|
|
pub fn from_list(
|
|
|
|
values: &[Value],
|
|
|
|
configuration: &TableConfiguration,
|
|
|
|
starting_idx: usize,
|
|
|
|
) -> nu_table::Table {
|
|
|
|
let header_style = TextStyle {
|
|
|
|
is_bold: configuration.header_bold(),
|
|
|
|
alignment: configuration.header_alignment(),
|
|
|
|
color: configuration.header_color(),
|
2020-06-20 05:41:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut headers: Vec<StyledString> = nu_protocol::merge_descriptors(values)
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| StyledString::new(x, header_style.clone()))
|
|
|
|
.collect();
|
2020-08-17 09:00:04 +02:00
|
|
|
let entries = values_to_entries(values, &mut headers, configuration, starting_idx);
|
2020-06-20 05:41:53 +02:00
|
|
|
|
|
|
|
nu_table::Table {
|
|
|
|
headers,
|
|
|
|
data: entries,
|
2020-08-17 09:00:04 +02:00
|
|
|
theme: configuration.table_mode(),
|
2020-06-20 05:41:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn values_to_entries(
|
|
|
|
values: &[Value],
|
|
|
|
headers: &mut Vec<StyledString>,
|
2020-08-17 09:00:04 +02:00
|
|
|
configuration: &TableConfiguration,
|
2020-06-20 05:41:53 +02:00
|
|
|
starting_idx: usize,
|
|
|
|
) -> Vec<Vec<StyledString>> {
|
2020-08-17 09:00:04 +02:00
|
|
|
let disable_indexes = configuration.disabled_indexes();
|
|
|
|
|
2020-06-20 05:41:53 +02:00
|
|
|
let mut entries = vec![];
|
|
|
|
|
|
|
|
if headers.is_empty() {
|
|
|
|
headers.push(StyledString::new("".to_string(), TextStyle::basic()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (idx, value) in values.iter().enumerate() {
|
|
|
|
let mut row: Vec<StyledString> = headers
|
|
|
|
.iter()
|
|
|
|
.map(|d: &StyledString| {
|
|
|
|
if d.contents == "" {
|
|
|
|
match value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Row(..),
|
|
|
|
..
|
|
|
|
} => StyledString::new(
|
|
|
|
format_leaf(&UntaggedValue::nothing()).plain_string(100_000),
|
|
|
|
style_leaf(&UntaggedValue::nothing()),
|
|
|
|
),
|
|
|
|
_ => StyledString::new(
|
|
|
|
format_leaf(value).plain_string(100_000),
|
|
|
|
style_leaf(value),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Row(..),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let data = value.get_data(&d.contents);
|
|
|
|
|
|
|
|
StyledString::new(
|
|
|
|
format_leaf(data.borrow()).plain_string(100_000),
|
|
|
|
style_leaf(data.borrow()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => StyledString::new(
|
|
|
|
format_leaf(&UntaggedValue::nothing()).plain_string(100_000),
|
|
|
|
style_leaf(&UntaggedValue::nothing()),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Indices are green, bold, right-aligned:
|
|
|
|
if !disable_indexes {
|
|
|
|
row.insert(
|
|
|
|
0,
|
|
|
|
StyledString::new(
|
|
|
|
(starting_idx + idx).to_string(),
|
|
|
|
TextStyle {
|
2020-07-15 05:48:20 +02:00
|
|
|
alignment: Alignment::Right,
|
2020-06-20 05:41:53 +02:00
|
|
|
color: Some(ansi_term::Color::Green),
|
|
|
|
is_bold: true,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
entries.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !disable_indexes {
|
|
|
|
headers.insert(
|
|
|
|
0,
|
|
|
|
StyledString::new(
|
|
|
|
"#".to_owned(),
|
|
|
|
TextStyle {
|
|
|
|
alignment: Alignment::Center,
|
|
|
|
color: Some(ansi_term::Color::Green),
|
|
|
|
is_bold: true,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
entries
|
|
|
|
}
|
|
|
|
|
2020-08-17 09:00:04 +02:00
|
|
|
async fn table(
|
|
|
|
configuration: TableConfiguration,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
let registry = registry.clone();
|
2020-06-13 21:13:36 +02:00
|
|
|
let mut args = args.evaluate_once(®istry).await?;
|
|
|
|
let mut finished = false;
|
|
|
|
|
2020-06-20 05:41:53 +02:00
|
|
|
// let host = args.host.clone();
|
2020-06-13 21:13:36 +02:00
|
|
|
let mut start_number = match args.get("start_number") {
|
|
|
|
Some(Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Int(i)),
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
if let Some(num) = i.to_usize() {
|
|
|
|
num
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected a row number",
|
|
|
|
"expected a row number",
|
|
|
|
&args.args.call_info.name_tag,
|
|
|
|
));
|
2019-10-13 06:12:43 +02:00
|
|
|
}
|
2020-06-13 21:13:36 +02:00
|
|
|
}
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut delay_slot = None;
|
|
|
|
|
2020-06-27 09:54:31 +02:00
|
|
|
let term_width = args.host.lock().width();
|
2020-06-20 05:41:53 +02:00
|
|
|
|
2020-06-13 21:13:36 +02:00
|
|
|
while !finished {
|
|
|
|
let mut new_input: VecDeque<Value> = VecDeque::new();
|
|
|
|
|
|
|
|
let start_time = Instant::now();
|
|
|
|
for idx in 0..STREAM_PAGE_SIZE {
|
|
|
|
if let Some(val) = delay_slot {
|
|
|
|
new_input.push_back(val);
|
|
|
|
delay_slot = None;
|
|
|
|
} else {
|
|
|
|
match args.input.next().await {
|
|
|
|
Some(a) => {
|
|
|
|
if !new_input.is_empty() {
|
|
|
|
if let Some(descs) = new_input.get(0) {
|
|
|
|
let descs = descs.data_descriptors();
|
|
|
|
let compare = a.data_descriptors();
|
|
|
|
if descs != compare {
|
|
|
|
delay_slot = Some(a);
|
|
|
|
break;
|
2020-01-25 19:10:20 +01:00
|
|
|
} else {
|
|
|
|
new_input.push_back(a);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new_input.push_back(a);
|
|
|
|
}
|
2020-06-13 21:13:36 +02:00
|
|
|
} else {
|
|
|
|
new_input.push_back(a);
|
2020-01-25 19:10:20 +01:00
|
|
|
}
|
2020-01-25 04:13:12 +01:00
|
|
|
}
|
2020-06-13 21:13:36 +02:00
|
|
|
_ => {
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 19:10:20 +01:00
|
|
|
|
2020-06-13 21:13:36 +02:00
|
|
|
// Check if we've gone over our buffering threshold
|
|
|
|
if (idx + 1) % STREAM_TIMEOUT_CHECK_INTERVAL == 0 {
|
|
|
|
let end_time = Instant::now();
|
2020-01-25 21:09:51 +01:00
|
|
|
|
2020-06-13 21:13:36 +02:00
|
|
|
// 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;
|
2020-01-25 21:09:51 +01:00
|
|
|
}
|
2020-01-25 04:13:12 +01:00
|
|
|
}
|
2019-08-03 04:17:28 +02:00
|
|
|
}
|
2020-06-13 21:13:36 +02:00
|
|
|
}
|
2020-01-25 04:13:12 +01:00
|
|
|
|
2020-06-13 21:13:36 +02:00
|
|
|
let input: Vec<Value> = new_input.into();
|
2020-01-25 04:13:12 +01:00
|
|
|
|
2020-06-13 21:13:36 +02:00
|
|
|
if !input.is_empty() {
|
2020-08-17 09:00:04 +02:00
|
|
|
let t = from_list(&input, &configuration, start_number);
|
2020-01-25 04:13:12 +01:00
|
|
|
|
2020-06-27 09:54:31 +02:00
|
|
|
draw_table(&t, term_width);
|
2019-08-03 04:17:28 +02:00
|
|
|
}
|
2020-01-25 04:13:12 +01:00
|
|
|
|
2020-06-13 21:13:36 +02:00
|
|
|
start_number += input.len();
|
|
|
|
}
|
2019-06-21 06:20:06 +02:00
|
|
|
|
2020-06-13 21:13:36 +02:00
|
|
|
Ok(OutputStream::empty())
|
2019-06-21 06:20:06 +02:00
|
|
|
}
|