2020-03-15 03:04:44 +00:00
use nu_test_support ::{ nu , pipeline } ;
#[ test ]
fn out_html_simple ( ) {
let actual = nu! (
cwd : " . " , pipeline (
r #"
2020-05-04 08:44:33 +00:00
echo 3 | to html
2020-03-15 03:04:44 +00:00
" #
) ) ;
2020-07-16 13:19:29 +00:00
assert_eq! (
actual . out ,
2020-07-20 12:57:29 +00:00
r "<html><style>body { background-color:white;color:black; }</style><body>3</body></html>"
2020-07-16 13:19:29 +00:00
) ;
2020-03-15 03:04:44 +00:00
}
2020-08-02 20:47:54 +00:00
#[ test ]
fn out_html_partial ( ) {
let actual = nu! (
cwd : " . " , pipeline (
r #"
echo 3 | to html - p
" #
) ) ;
assert_eq! (
actual . out ,
" <div style= \" background-color:white;color:black; \" >3</div> "
) ;
}
2020-03-15 03:04:44 +00:00
#[ test ]
fn out_html_table ( ) {
let actual = nu! (
cwd : " . " , pipeline (
r #"
2020-07-20 12:57:29 +00:00
echo ' { " name " : " darren " } ' | from json | to html
2020-03-15 03:04:44 +00:00
" #
) ) ;
assert_eq! (
2020-05-07 11:03:43 +00:00
actual . out ,
2022-06-02 22:34:31 +00:00
r "<html><style>body { background-color:white;color:black; }</style><body><table><thead><tr><th>name</th></tr></thead><tbody><tr><td>darren</td></tr></tbody></table></body></html>"
2020-07-20 12:57:29 +00:00
) ;
}
#[ test ]
2022-08-31 08:15:03 +00:00
#[ ignore ]
2020-07-20 12:57:29 +00:00
fn test_cd_html_color_flag_dark_false ( ) {
let actual = nu! (
cwd : " . " , pipeline (
r #"
2022-02-17 14:55:17 +00:00
cd - - help | to html - - html - color
2020-07-20 12:57:29 +00:00
" #
)
) ;
assert_eq! (
actual . out ,
FEATURE: print example command results in the `help` (#8189)
Should close #8035.
> **Note**
> this is my first technical PR for `nushell`
> - i might very well miss things
> - i tried to be as complete as possible about the changes
> - please require further changes if i did something wrong, i'm open to
any remark :relieved:
# Description
this PR adds, when it is defined in the `examples` method of the
`Command` implementations, the output of the examples to the output of
the `help` command.
this PR
- only modifies `crates/nu-engine/src/documentation.rs` and the
`get_documentation` function
- defines a new `WD` constant to print a **W**hite **D**immed `...`
- a `match` statement at the end of the example loop to
- print a white dimmed `...` when the example is not set, i.e. set to
`None` in the `examples` method of the `Command` implementation of a
command
- pretty print the output of the associated example `Value` when it has
been defined
> **Warning**
> LIMITATIONS:
> - i use snippets from `crates/nu-protocol/src/pipeline_data.rs`
> - the table creation from `pub PipelineData::print`, i.e. the `let
decl_id = ...;` and `let table = ...;` in the change
> - the table item printing from `PipelineData::write_all_and_flush`,
i.e. the `for item in table { ... }`
>
> ADDRESSED:
> - ~~the formatting of the output is not perfect and has to be fully
left aligned with the first column for now~~ (fixed with
[`5abeefd558c34ba9bae15e2f183ff4625442921e`..`a62be1b5a2c730959da5dbc028bb91ffe5093f63`](5abeefd558c34ba9bae15e2f183ff4625442921e..a62be1b5a2c730959da5dbc028bb91ffe5093f63))
> - ~~i'm using `.unwrap()` on both the changes above, not sure how to
handle this for now~~ (fixed for now thanks to 49f1dc080)
> - ~~the tests and `clippy` checks do not pass for now, see below~~
(`clippy` now is happy with 49f1dc080 and the tests pass with
11666bc71526b31a27105a362ce11cf94a55b8b6)
# User-Facing Changes
the output of the `help <command>` command is now augmented with the
outputs of the examples, when they are defined.
- `with-env`
```bash
> help with-env
...
Examples:
Set the MYENV environment variable
> with-env [MYENV "my env value"] { $env.MYENV }
my env value
Set by primitive value list
> with-env [X Y W Z] { $env.X }
Y
Set by single row table
> with-env [[X W]; [Y Z]] { $env.W }
Z
Set by key-value record
> with-env {X: "Y", W: "Z"} { [$env.X $env.W] }
╭───┬───╮
│ 0 │ Y │
│ 1 │ Z │
╰───┴───╯
```
instead of the previous
```bash
> help with-env
...
Examples:
Set the MYENV environment variable
> with-env [MYENV "my env value"] { $env.MYENV }
Set by primitive value list
> with-env [X Y W Z] { $env.X }
Set by single row table
> with-env [[X W]; [Y Z]] { $env.W }
Set by key-value record
> with-env {X: "Y", W: "Z"} { [$env.X $env.W] }
```
- `merge`
```bash
> help merge
...
Examples:
Add an 'index' column to the input table
> [a b c] | wrap name | merge ( [1 2 3] | wrap index )
╭───┬──────╮
│ # │ name │
├───┼──────┤
│ 1 │ a │
│ 2 │ b │
│ 3 │ c │
╰───┴──────╯
Merge two records
> {a: 1, b: 2} | merge {c: 3}
╭───┬───╮
│ a │ 1 │
│ b │ 2 │
│ c │ 3 │
╰───┴───╯
Merge two tables, overwriting overlapping columns
> [{columnA: A0 columnB: B0}] | merge [{columnA: 'A0*'}]
╭───┬─────────┬─────────╮
│ # │ columnA │ columnB │
├───┼─────────┼─────────┤
│ 0 │ A0* │ B0 │
╰───┴─────────┴─────────╯
```
instead of the previous
```bash
> help merge
...
Examples:
Add an 'index' column to the input table
> [a b c] | wrap name | merge ( [1 2 3] | wrap index )
Merge two records
> {a: 1, b: 2} | merge {c: 3}
Merge two tables, overwriting overlapping columns
> [{columnA: A0 columnB: B0}] | merge [{columnA: 'A0*'}]
```
2023-02-26 20:05:11 +00:00
r "<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br><span style='color:green;'>Usage<span style='color:black;font-weight:normal;'>:<br> > cd (path) <br><br></span></span><span style='color:green;'>Flags<span style='color:black;font-weight:normal;'>:<br> </span></span><span style='color:#037979;'>-h</span>,<span style='color:black;font-weight:normal;'> </span><span style='color:#037979;'>--help<span style='color:black;font-weight:normal;'> - Display the help message for this command<br><br></span><span style='color:green;'>Signatures<span style='color:black;font-weight:normal;'>:<br> <nothing> | cd <string?> -> <nothing><br> <string> | cd <string?> -> <nothing><br><br></span></span><span style='color:green;'>Parameters<span style='color:black;font-weight:normal;'>:<br> (optional) </span></span></span><span style='color:#037979;'>path<span style='color:black;font-weight:normal;'> <</span><span style='color:blue;font-weight:bold;'>directory<span style='color:black;font-weight:normal;'>>: the path to change to<br><br></span></span><span style='color:green;'>Examples<span style='color:black;font-weight:normal;'>:<br> Change to your home directory<br> > </span><span style='color:#037979;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'> </span></span></span></span><span style='color:#037979;'>~<span style='color:black;font-weight:normal;'><br><br> Change to a directory via abbreviations<br> > </span><span style='color:#037979;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'> </span></span></span><span style='color:#037979;'>d/s/9<span style='color:black;font-weight:normal;'><br><br> Change to the previous working directory ($OLDPWD)<br> > </span><span style='color:#037979;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'> </span></span></span><span style='color:#037979;'>-<span style='color:black;font-weight:normal;'><br><br></body></html></span></span>"
2020-07-20 12:57:29 +00:00
) ;
}
#[ test ]
fn test_no_color_flag ( ) {
let actual = nu! (
cwd : " . " , pipeline (
r #"
2022-02-17 14:55:17 +00:00
cd - - help | to html - - no - color
2020-07-20 12:57:29 +00:00
" #
)
) ;
assert_eq! (
actual . out ,
2022-12-13 12:10:55 +00:00
r "<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br>Usage:<br> > cd (path) <br><br>Flags:<br> -h, --help - Display the help message for this command<br><br>Signatures:<br> <nothing> | cd <string?> -> <nothing><br> <string> | cd <string?> -> <nothing><br><br>Parameters:<br> (optional) path <directory>: the path to change to<br><br>Examples:<br> Change to your home directory<br> > cd ~<br><br> Change to a directory via abbreviations<br> > cd d/s/9<br><br> Change to the previous working directory ($OLDPWD)<br> > cd -<br><br></body></html>"
2020-03-15 03:04:44 +00:00
) ;
}
2022-11-15 17:12:56 +00:00
#[ test ]
fn test_list ( ) {
let actual = nu! (
cwd : " . " ,
r # "to html --list | where name == C64 | get 0 | to nuon"#
) ;
assert_eq! (
actual . out ,
2022-11-19 11:09:39 +00:00
r ## "{name: "C64", black: "#090300", red: "#883932", green: "#55a049", yellow: "#bfce72", blue: "#40318d", purple: "#8b3f96", cyan: "#67b6bd", white: "#ffffff", brightBlack: "#000000", brightRed: "#883932", brightGreen: "#55a049", brightYellow: "#bfce72", brightBlue: "#40318d", brightPurple: "#8b3f96", brightCyan: "#67b6bd", brightWhite: "#f7f7f7", background: "#40318d", foreground: "#7869c4"}"##
2022-11-15 17:12:56 +00:00
) ;
}