batpipe: Add BATPIPE_TERM_WIDTH

This commit is contained in:
Ethan P 2023-03-21 00:33:32 -07:00
parent 998e2ff1a9
commit ea604457ea
No known key found for this signature in database
GPG Key ID: A15E33AC0325A3F5
4 changed files with 55 additions and 2 deletions

View File

@ -17,6 +17,14 @@ Like [lesspipe](https://github.com/wofr06/lesspipe), `batpipe` is designed to wo
## Environment
| Variable | Description |
| -------------------- | ------------------------------------------------------------ |
| `BATPIPE_TERM_WIDTH` | Sets the terminal width provided to `bat`. If this variable starts with a hyphen (`-`), the number provided will be relative to the detected terminal size. |
## Built-in Viewers ## Built-in Viewers
| Files | Program | | Files | Program |
@ -51,7 +59,7 @@ The `viewer_${viewer}_supports` function is called to determine if the external
batpipe_header [pattern] [...] -- Print a viewer header line. batpipe_header [pattern] [...] -- Print a viewer header line.
batpipe_subheader [pattern] [...] -- Print a viewer subheader line. batpipe_subheader [pattern] [...] -- Print a viewer subheader line.
strip_trailing_slashes [path] -- Strips trailing slashes from a path. strip_trailing_slashes [path] -- Strips trailing slashes from a path.

View File

@ -115,9 +115,17 @@ BATPIPE_INSIDE_LESS=false
BATPIPE_INSIDE_BAT=false BATPIPE_INSIDE_BAT=false
TERM_WIDTH="$(term_width)" TERM_WIDTH="$(term_width)"
if [[ -n "${BATPIPE_TERM_WIDTH:-}" ]]; then
if [[ "${BATPIPE_TERM_WIDTH:0:1}" = "-" ]]; then
TERM_WIDTH=$((TERM_WIDTH + BATPIPE_TERM_WIDTH)) || true
else
TERM_WIDTH="$BATPIPE_TERM_WIDTH"
fi
fi
BATPIPE_PARENT_EXECUTABLE_PID="$PPID" BATPIPE_PARENT_EXECUTABLE_PID="$PPID"
for i in 1 2 3; do for i in 1 2 3; do
BATPIPE_PARENT_EXECUTABLE="$(parent_executable "$BATPIPE_PARENT_EXECUTABLE_PID")" BATPIPE_PARENT_EXECUTABLE="${BATPIPE_DEBUG_PARENT_EXECUTABLE:-$(parent_executable "$BATPIPE_PARENT_EXECUTABLE_PID")}"
BATPIPE_PARENT_EXECUTABLE_BASENAME="$(basename -- "${BATPIPE_PARENT_EXECUTABLE}" | cut -d' ' -f1)" BATPIPE_PARENT_EXECUTABLE_BASENAME="$(basename -- "${BATPIPE_PARENT_EXECUTABLE}" | cut -d' ' -f1)"
BATPIPE_PARENT_EXECUTABLE_PID="$(parent_executable_pid "$BATPIPE_PARENT_EXECUTABLE_PID")" BATPIPE_PARENT_EXECUTABLE_PID="$(parent_executable_pid "$BATPIPE_PARENT_EXECUTABLE_PID")"

View File

@ -0,0 +1,26 @@
───────┬────────────────────────────────
│ File: file.txt
───────┼────────────────────────────────
 1 │ cat
 2 │ dog
 3 │ car
 4 │ frog
 5 │ fox
 6 │ clocks
 7 │ bash
 8 │ $300
 9 │ ^$!@
───────┴────────────────────────────────
───────┬────────────────────────────────────────────────────
│ File: file.txt
───────┼────────────────────────────────────────────────────
 1 │ cat
 2 │ dog
 3 │ car
 4 │ frog
 5 │ fox
 6 │ clocks
 7 │ bash
 8 │ $300
 9 │ ^$!@
───────┴────────────────────────────────────────────────────

View File

@ -25,3 +25,14 @@ test:viewer_gzip() {
assert_equal "$(batpipe compressed.txt.gz)" "OK" assert_equal "$(batpipe compressed.txt.gz)" "OK"
} }
test:batpipe_term_width() {
description "Test support for BATPIPE_TERM_WIDTH"
snapshot STDOUT
export BATPIPE=color
export BATPIPE_DEBUG_PARENT_EXECUTABLE=less
BATPIPE_TERM_WIDTH=40 batpipe file.txt
BATPIPE_TERM_WIDTH=-20 batpipe file.txt
}