Table indexes (#6620)

* Table indexes

* Renamed to `show_table_indexes`

* Renamed to `table_index_mode`
This commit is contained in:
Nano 2022-09-29 10:07:33 +12:00 committed by GitHub
parent d2e4f03d19
commit 5c99921e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 15 deletions

View File

@ -5,7 +5,8 @@ use nu_protocol::{
ast::{Call, PathMember},
engine::{Command, EngineState, Stack, StateWorkingSet},
format_error, Category, Config, DataSource, Example, IntoPipelineData, ListStream,
PipelineData, PipelineMetadata, RawStream, ShellError, Signature, Span, SyntaxShape, Value,
PipelineData, PipelineMetadata, RawStream, ShellError, Signature, Span, SyntaxShape,
TableIndexMode, Value,
};
use nu_table::{Alignments, StyledString, TableTheme, TextStyle};
use nu_utils::get_ls_colors;
@ -348,10 +349,14 @@ fn convert_to_table(
let mut input = input.iter().peekable();
let color_hm = get_color_config(config);
let float_precision = config.float_precision as usize;
let disable_index = config.disable_table_indexes;
let with_index = match config.table_index_mode {
TableIndexMode::Always => true,
TableIndexMode::Never => false,
TableIndexMode::Auto => headers.iter().any(|header| header == INDEX_COLUMN_NAME),
};
if input.peek().is_some() {
if !headers.is_empty() && !disable_index {
if !headers.is_empty() && with_index {
headers.insert(0, "#".into());
}
@ -373,7 +378,7 @@ fn convert_to_table(
}
// String1 = datatype, String2 = value as string
let mut row: Vec<(String, String)> = vec![];
if !disable_index {
if with_index {
let row_val = match &item {
Value::Record { .. } => item
.get_data_by_key(INDEX_COLUMN_NAME)
@ -390,7 +395,7 @@ fn convert_to_table(
item.into_abbreviated_string(config),
));
} else {
let skip_num = if !disable_index { 1 } else { 0 };
let skip_num = if with_index { 1 } else { 0 };
for header in headers.iter().skip(skip_num) {
let result = match item {
Value::Record { .. } => item.clone().follow_cell_path(
@ -432,7 +437,7 @@ fn convert_to_table(
x.into_iter()
.enumerate()
.map(|(col, y)| {
if col == 0 && !disable_index {
if col == 0 && with_index {
StyledString {
contents: y.1,
style: TextStyle {

View File

@ -77,7 +77,7 @@ pub struct Config {
pub rm_always_trash: bool,
pub shell_integration: bool,
pub buffer_editor: String,
pub disable_table_indexes: bool,
pub table_index_mode: TableIndexMode,
pub cd_with_abbreviations: bool,
pub case_sensitive_completions: bool,
pub enable_external_completion: bool,
@ -114,7 +114,7 @@ impl Default for Config {
rm_always_trash: false,
shell_integration: false,
buffer_editor: String::new(),
disable_table_indexes: false,
table_index_mode: TableIndexMode::Always,
cd_with_abbreviations: false,
case_sensitive_completions: false,
enable_external_completion: true,
@ -145,6 +145,16 @@ pub enum HistoryFileFormat {
PlainText,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum TableIndexMode {
/// Always show indexes
Always,
/// Never show indexes
Never,
/// Show indexes when a table has "index" column
Auto,
}
/// A Table view configuration, for a situation where
/// we need to limit cell width in order to adjust for a terminal size.
#[derive(Serialize, Deserialize, Clone, Debug)]
@ -370,11 +380,19 @@ impl Value {
eprintln!("$config.buffer_editor is not a string")
}
}
"disable_table_indexes" => {
if let Ok(b) = value.as_bool() {
config.disable_table_indexes = b;
"table_index_mode" => {
if let Ok(b) = value.as_string() {
let val_str = b.to_lowercase();
match val_str.as_ref() {
"always" => config.table_index_mode = TableIndexMode::Always,
"never" => config.table_index_mode = TableIndexMode::Never,
"auto" => config.table_index_mode = TableIndexMode::Auto,
_ => eprintln!(
"$config.table_index_mode must be a never, always or auto"
),
}
} else {
eprintln!("$config.disable_table_indexes is not a bool")
eprintln!("$config.table_index_mode is not a string")
}
}
"cd_with_abbreviations" => {

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use nu_ansi_term::Style;
use nu_protocol::{Config, FooterMode, TrimStrategy};
use nu_protocol::{Config, FooterMode, TableIndexMode, TrimStrategy};
use tabled::{
builder::Builder,
formatting_settings::AlignmentStrategy,
@ -95,7 +95,15 @@ fn draw_table(
let theme = &table.theme;
let with_header = headers.is_some();
let with_footer = with_header && need_footer(config, data.len() as u64);
let with_index = !config.disable_table_indexes;
let with_index = match config.table_index_mode {
TableIndexMode::Always => true,
TableIndexMode::Never => false,
TableIndexMode::Auto => table
.headers
.iter()
.flatten()
.any(|header| header.contents == "index"),
};
let table = build_table(data, headers, with_footer);
let table = load_theme(table, color_hm, theme, with_footer, with_header);

View File

@ -259,7 +259,7 @@ let-env config = {
sync_history_on_enter: true # Enable to share the history between multiple sessions, else you have to close the session to persist history to file
history_file_format: "plaintext" # "sqlite" or "plaintext"
shell_integration: true # enables terminal markers and a workaround to arrow keys stop working issue
disable_table_indexes: false # set to true to remove the index column from tables
table_index_mode: always # always, never, auto
cd_with_abbreviations: false # set to true to allow you to do things like cd s/o/f and nushell expand it to cd some/other/folder
case_sensitive_completions: false # set to true to enable case-sensitive completions
enable_external_completion: true # set to false to prevent nushell looking into $env.PATH to find more suggestions, `false` recommended for WSL users as this look up my be very slow