detect columns: intruduce a --guess flag, remove --legacy (#12333)
# Description This pr is addressing feedback from https://github.com/nushell/nushell/pull/12277#issuecomment-2027246752 Currently I think it's fine to replace `--legacy` flag with `--guess` one. Only use `guess_width` algorithm if `--guess` is provided. # User-Facing Changes So it won't be a breaking change to previous version.
This commit is contained in:
parent
cf923fc44c
commit
ff2aba7ae3
|
@ -29,7 +29,11 @@ impl Command for DetectColumns {
|
||||||
"columns to be combined; listed as a range",
|
"columns to be combined; listed as a range",
|
||||||
Some('c'),
|
Some('c'),
|
||||||
)
|
)
|
||||||
.switch("legacy", "use another algorithm to detect columns, it may be useful if default one doesn't work", None)
|
.switch(
|
||||||
|
"guess",
|
||||||
|
"detect columns by guessing width, it may be useful if default one doesn't work",
|
||||||
|
None,
|
||||||
|
)
|
||||||
.category(Category::Strings)
|
.category(Category::Strings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,20 +52,20 @@ impl Command for DetectColumns {
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
if !call.has_flag(engine_state, stack, "legacy")? {
|
if call.has_flag(engine_state, stack, "guess")? {
|
||||||
guess_width(engine_state, stack, call, input)
|
guess_width(engine_state, stack, call, input)
|
||||||
} else {
|
} else {
|
||||||
detect_columns_legacy(engine_state, stack, call, input)
|
detect_columns(engine_state, stack, call, input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "detect columns by df output",
|
description: "use --guess if you find default algorithm not working",
|
||||||
example: r"
|
example: r"
|
||||||
'Filesystem 1K-blocks Used Available Use% Mounted on
|
'Filesystem 1K-blocks Used Available Use% Mounted on
|
||||||
none 8150224 4 8150220 1% /mnt/c' | detect columns",
|
none 8150224 4 8150220 1% /mnt/c' | detect columns --guess",
|
||||||
result: Some(Value::test_list(vec![Value::test_record(record! {
|
result: Some(Value::test_list(vec![Value::test_record(record! {
|
||||||
"Filesystem" => Value::test_string("none"),
|
"Filesystem" => Value::test_string("none"),
|
||||||
"1K-blocks" => Value::test_string("8150224"),
|
"1K-blocks" => Value::test_string("8150224"),
|
||||||
|
@ -72,8 +76,8 @@ none 8150224 4 8150220 1% /mnt/c' | detect columns",
|
||||||
})])),
|
})])),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Use --legacy parameter if you find default one does not work",
|
description: "detect columns with no headers",
|
||||||
example: "'a b c' | detect columns --legacy --no-headers",
|
example: "'a b c' | detect columns --no-headers",
|
||||||
result: Some(Value::test_list(vec![Value::test_record(record! {
|
result: Some(Value::test_list(vec![Value::test_record(record! {
|
||||||
"column0" => Value::test_string("a"),
|
"column0" => Value::test_string("a"),
|
||||||
"column1" => Value::test_string("b"),
|
"column1" => Value::test_string("b"),
|
||||||
|
@ -83,19 +87,19 @@ none 8150224 4 8150220 1% /mnt/c' | detect columns",
|
||||||
Example {
|
Example {
|
||||||
description: "",
|
description: "",
|
||||||
example:
|
example:
|
||||||
"$'c1 c2 c3 c4 c5(char nl)a b c d e' | detect columns --combine-columns 0..1 --legacy",
|
"$'c1 c2 c3 c4 c5(char nl)a b c d e' | detect columns --combine-columns 0..1 ",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Splits a multi-line string into columns with headers detected",
|
description: "Splits a multi-line string into columns with headers detected",
|
||||||
example:
|
example:
|
||||||
"$'c1 c2 c3 c4 c5(char nl)a b c d e' | detect columns --combine-columns -2..-1 --legacy",
|
"$'c1 c2 c3 c4 c5(char nl)a b c d e' | detect columns --combine-columns -2..-1 ",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Splits a multi-line string into columns with headers detected",
|
description: "Splits a multi-line string into columns with headers detected",
|
||||||
example:
|
example:
|
||||||
"$'c1 c2 c3 c4 c5(char nl)a b c d e' | detect columns --combine-columns 2.. --legacy",
|
"$'c1 c2 c3 c4 c5(char nl)a b c d e' | detect columns --combine-columns 2.. ",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
|
@ -184,7 +188,7 @@ fn guess_width(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn detect_columns_legacy(
|
fn detect_columns(
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
|
|
|
@ -11,14 +11,14 @@ fn detect_columns_with_legacy() {
|
||||||
for case in cases.into_iter() {
|
for case in cases.into_iter() {
|
||||||
let out = nu!(
|
let out = nu!(
|
||||||
cwd: dirs.test(),
|
cwd: dirs.test(),
|
||||||
"({} | detect columns --legacy) == {}",
|
"({} | detect columns) == {}",
|
||||||
case.0,
|
case.0,
|
||||||
case.1
|
case.1
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
out.out, "true",
|
out.out, "true",
|
||||||
"({} | detect columns --legacy) == {}",
|
"({} | detect columns) == {}",
|
||||||
case.0, case.1
|
case.0, case.1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ fn detect_columns_with_legacy_and_flag_c() {
|
||||||
for case in cases.into_iter() {
|
for case in cases.into_iter() {
|
||||||
let out = nu!(
|
let out = nu!(
|
||||||
cwd: dirs.test(),
|
cwd: dirs.test(),
|
||||||
"({} | detect columns --legacy --combine-columns {}) == {}",
|
"({} | detect columns --combine-columns {}) == {}",
|
||||||
case.0,
|
case.0,
|
||||||
case.2,
|
case.2,
|
||||||
case.1,
|
case.1,
|
||||||
|
@ -57,7 +57,7 @@ fn detect_columns_with_legacy_and_flag_c() {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
out.out, "true",
|
out.out, "true",
|
||||||
"({} | detect columns --legacy --combine-columns {}) == {}",
|
"({} | detect columns --combine-columns {}) == {}",
|
||||||
case.0, case.2, case.1
|
case.0, case.2, case.1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user