Skip to content

Commit 04b2f2f

Browse files
committed
Added support for ANSI styling watched command output lines
1 parent 4037368 commit 04b2f2f

File tree

17 files changed

+894
-299
lines changed

17 files changed

+894
-299
lines changed

Cargo.lock

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ repository = "https://github.com/fritzrehde/watchbind"
1010
description = "A CLI menu for periodically watching a program's output and executing commands on its lines through keybindings"
1111

1212
[dependencies]
13-
clap = { version = "4.4.7", default-features = false, features = ["std", "help", "cargo", "derive", "error-context"] }
13+
clap = { version = "4.4.7", default-features = false, features = ["std", "help", "cargo", "derive", "error-context", "string", "color"] }
1414
serde = { version = "1.0", default-features = false, features = ["derive"] }
1515
toml = { version = "0.7.8", default-features = false, features = ["parse"] }
1616
ratatui = "0.22.0"
1717
crossterm = { version = "0.27", features = ["events", "event-stream"] }
1818
itertools = "0.11.0"
1919
anyhow = "1.0.75"
2020
indoc = "2.0.4"
21-
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into_iterator", "as_ref"] }
21+
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "into_iterator", "as_ref"] }
2222
tabwriter = "1.3.0"
2323
parse-display = "0.8.2"
2424
derive-new = "0.5.9"
@@ -28,6 +28,7 @@ ranges = "0.3.3"
2828
# TODO: maybe we don't need all tokio and futures features, try to reduce
2929
tokio = { version = "1.33.0", features = ["full"] }
3030
futures = "0.3.29"
31+
ansi-to-tui = "3.1.0"
3132

3233
# Config for 'cargo dist'
3334
[workspace.metadata.dist]

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- [Installation](#installation)
1616
- [Customizations](#customizations)
1717
- [Keybindings](#customizations)
18-
- [Formatting with Field Separators and Selections](#formatting-with-field-separators-and-selections)
18+
- [Formatting with Field Separators and Field Selections](#formatting-with-field-separators-and-field-selections)
1919
- [Styling](#styling)
2020
- [Tips](#tips)
2121

@@ -157,10 +157,9 @@ tab
157157

158158
#### Operations
159159

160-
All supported `OP` values:
161-
162-
<!-- Make table of toml config reference name,example,description -->
160+
<!-- TODO: Make table of toml config reference name,example,description -->
163161

162+
All supported `OP` values:
164163

165164
Operation | Description
166165
:-- | :--
@@ -186,7 +185,7 @@ The environment variable `lines` set to all selected lines, or if none are selec
186185
All set environment variables `ENV` will be made available in all future spawned commands/processes, including the watched command, any executed subcommands, as well as commands executed in `set-env` operations.
187186
If multiple lines are selected, they will be separated by newlines in `lines`.
188187

189-
### Formatting with Field Separators and Selections
188+
### Formatting with Field Separators and Field Selections
190189

191190
`watchbind` supports some extra formatting features reminiscent of the Unix `cut` command:
192191

@@ -203,13 +202,19 @@ For instance, the field selection `1,3-4,6-` will display the first, third and f
203202

204203
### Styling
205204

206-
Foreground colors, background colors and boldness of the line the cursor is on, the header lines and all other lines can be customized.
205+
Foreground colors, background colors and boldness can be customized.
206+
These styling options are available for:
207+
- The line the cursor is currently on with `cursor-[fg|bg|boldness]`.
208+
- The header lines with `header-[fg|bg|boldness]`.
209+
- All other lines with `non-cursor-non-header-[fg|bg|boldness]`.
210+
- The selection indicator with `selected-bg`.
207211

208-
To see all available fields you can customize, run `watchbind -h`.
209212
The names of the customization fields from the command-line options (e.g. `--cursor-fg blue`) are the same in the TOML config file (e.g. `cursor-fg = "blue"`).
210213

214+
Furthermore, `watchbind` also supports styling according to ANSI codes in the input text.
215+
211216
All supported `COLOR` values:
212-
```
217+
```sh
213218
white
214219
black
215220
red
@@ -226,6 +231,15 @@ light_yellow
226231
light_blue
227232
light_magenta
228233
light_cyan
234+
reset # Reset the fg and bg
235+
unspecified # Don't applying any styling => use style from ANSI input text
236+
```
237+
238+
All supported `BOLDNESS` values:
239+
```sh
240+
bold # Make everything bold
241+
non-bold # Make sure nothing is bold (i.e. remove any bold styling from input ANSI)
242+
unspecified # Don't applying any styling => use style from ANSI input text
229243
```
230244

231245
## Tips

examples/fields.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ watched-command = """
22
printf \
33
"id,col1,col2,col3
44
123,v1,v2,v3
5-
456,v4,v5,v6"
5+
456,v4,v5,v6
6+
789,v7,v8,v9"
67
"""
78
interval = 5.0
8-
header-lines = 1
9+
header-lines = 2
910
field-separator = ","
1011
fields = "1,3-4"
11-
12-
[keybindings]

examples/ls.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
watched-command = "ls"
22
interval = 3.0
3-
bold = false
4-
cursor-fg = "black"
3+
4+
# cursor line
55
cursor-bg = "blue"
6-
cursor-bold = true
7-
selected-bg = "red"
6+
cursor-boldness = "bold"
7+
8+
# header lines
89
header-lines = 1
10+
header-fg = "blue"
11+
header-boldness = "non-bold"
12+
13+
# selected lines
14+
selected-bg = "red"
915

1016
[keybindings]
1117
"esc" = [ "unselect-all", "help-hide" ]

src/command.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub struct Interruptible {
4040
}
4141

4242
// Advantages of the Type-State Builder Pattern:
43-
// 1. We don't have any option/enum (an alternative configuration strategy)
44-
// checking overhead at runtime.
43+
// 1. We don't have any option/enum checking (an alternative configuration
44+
// strategy) overhead at runtime.
4545
// 2. We can guarantee that we handled all possible Command "variants"
4646
// (combination of config options), that we use, at compile-time.
4747
// 3. Arguably, this also results in separated, cleaner code.
@@ -50,7 +50,6 @@ pub struct Interruptible {
5050
/// environment variables, whether the output is captured and whether the
5151
/// execution can be interrupted. Utilizes the type-state builder pattern to
5252
/// enforce these configurations at compile-time.
53-
// #[derive(Clone)]
5453
pub struct CommandBuilder<B = NonBlocking, E = WithoutEnv, O = NoOutput, I = NonInterruptible> {
5554
command: String,
5655
blocking: B,

src/config/fields/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ impl Fields {
3232
}
3333
}
3434

35-
/// Format a string as a table that has its fields separated by an elastic
36-
/// tabstop, and only displays the fields that should be selected.
37-
/// Only applies any formatting if a separator or selection is present.
3835
pub trait TableFormatter {
36+
/// Format a string as a table that has its fields separated by an elastic
37+
/// tabstop, and only displays the fields that should be selected.
38+
/// Only applies any formatting if a field separator or a field selection
39+
/// are present.
40+
/// Returns `None` if no formatting was applied.
3941
fn format_as_table(&self, fields: &Fields) -> Result<Option<String>>;
4042
}
4143

0 commit comments

Comments
 (0)