Skip to content

Commit

Permalink
ask, choose, confirm: comprehensive support of TTY modes and ca…
Browse files Browse the repository at this point in the history
…pabilities (#254)

/close #253 #234

- general:
    - use the correct quote/escape for the context
    - `IFS=''` to `IFS=` as they are equivalent
    - added a deprecation system with `dorothy-warnings` and `commands.deprecated` suggestions
    - change `echo` to either `__print_line` or `__print_lines`
    - compress multiple `echo-style` calls into a single call for performance
    - implement new styles where appropriate, however this is not comprehensive at all
    - rewrite quiet/verbose handling, now ignores the environment variable, which is a superior behaviour that prevents unexpected behaviour

- docs:
    - add `terminals-and-tty.md`:
        - debug information from `is-tty --test` moved into new `debug-terminal-(tty|stdin)` and learnings documented in this doc file
    - add ``ansi-escape-codes.md`:
        - moved ansi escape code documentation from various commands in this doc file
    - `prompts.md`:
        - remove outdated information and update with latest coventions
    - `readme`:
        - list @balupton's github list of [Dorothy User Configurations](https://github.com/stars/balupton/lists/dorothy-user-configurations)
        - remove unnecessary deps

- ci:
    - `dorothy-workflow.yml`:
        - support testing on branches and forks
        - remove unnecessary deps
    - `shellcheckrc`:
        - ignore `SC2016`, it is always intentional
        - ignore `SC2059`, it is always intentional
        - add docs on all the disabled rules

- deprecate `tty.bash`, `choose-menu`, `choose-option`, `echo-clear-line`, `echo-color-enabled`, `echo-element`, `echo-quiet-enabled`, `echo-title`, `is-color-enabled`, `is-interactive`, `is-match`, `is-quiet-enabled`, `is-tty`
    - refer to their source code or their warning messages for their replacements
    - `is-tty --test` debug information moved into `debug-terminal-(stdin|tty)` beta commands, and add more information
    - add `get-terminal-alternative-support`
    - add `get-terminal-color-support`:
        - if ci isn't github, continue to fail, however if dumb terminal and not ci, continue to fallback
            - this enables fallback on `ssh -T ...`, which does support colors
    - add `get-terminal-cursor-line-and-column`
    - add `get-terminal-device-file`
    - add `get-terminal-lines-and-columns`
    - add `get-terminal-position-support`
    - add `get-terminal-quiet-support`:
        - adapt for the `get-terminal-color-support` convention, however keep same functionality, this could be a mistake - will need to evaluate
    - add `get-terminal-reactivity-support`
    - add `get-terminal-size-support`
    - add `get-terminal-title-support`:
        - vscode doesn't support titles, so fail on vscode
    - add `get-terminal-tty-support`

- move `echo-exit-affirmative`, `echo-mkdir`, `echo-nothing-or-fail` to beta commands as they are no longer (or were never) used internally

- `stdinargs.bash`:
    - rewrite to use `[[ ... ]]` instead of `test`/`[` to better handle edge cases, such as a trailing line containing only `>`

- `bash.bash`:
    - remove deprecated usage of `__print_line ...`, use `__print_lines ...` instead
    - support initial `--` argument in appropriate functions
    - rewrite to use `[[ ... ]]` instead of `test`/`[` to better handle edge cases
    - add `__substr` shim, fix for `cpr` and `echo-style`
    - fix escape quoting in `mapfile` shim for bash v3
    - add some common helpers for performance, and use them wherever appropriate
        - add `__is_brew`, `__command_(missing|exists)`, `__try_sudo`, `__is_brew`, `__mkdirp`, `__sudo_mkdirp`, `__escape_shapeshifting`, `__is_shapeshifter`, `__can_read_decimal_timeout`
        - I did add `has_needles` however it wasn't justified compared to continuing `is-needle`

- `zsh.zsh`:
    - consistency with `bash.bash` changes

- `styles.bash` and `echo-style`:
    - `styles.bash`:
        - check various support abilities at the begining for clarity and performance
        - add styles for various ansi escape codes, for consolidation, and to ensure correct escape codes are used based on support, including that of `newline`
        - change clear screen to use `\e[J` instead of `\e[2J` for reasoning documented inside the file
        - if the alternative screen buffer is not used, then adapt the screen buffer and clear screen styles to be a newline and clear screen instead, this makes it so that even if clear screen is not supported, the newline still ensures things are visually separated, which works great for GitHub Actions
        - fix broken `code_notice` style and replace to `code-notice1`
        - rejigged styles for `ask`, `choose`, and `confirm` to be consistent and to support no-TTY mode
        - ensure that refreshed styles have their color and no color styles set appropriately
    - `echo-style`:
        - support `--=...`, `--=`, `--` arguments
        - `echo-style --tty` instead of `echo-style >/dev/tty` as the former can support when `/dev/tty` doesn't exist

- `ask`, `choose`, `confirm`, `read-key`, `is-shapeshifter`, `echo-count-lines`, `echo-clear-lines`, `echo-revolving-door`, `echo-wrap`, `eval-helper`:
    - significant changes to support no-TTY mode and to support all known edge cases
    - consistent commentary, result, and exception styling
    - `ask`:
        - rewrote for modern conventions and new stdin/tty learnings, properly supports no-TTY
        - fix read failures not being output
        - if non-reactive, use alternative screen buffer
            - added `--no-inline` option to force usage of alternative screen buffer if supported/reactive
            - this solves the impossibility of clearing the accurate amount of lines in no-TTY mode, as `ssh -T` and pipe/redirect/CI result in different line counts due to `ssh -T` outputting the enter key as a visual new line
    - `choose`:
        - don't crash on unknown keys, instead flash bell
        - improve performance of legend rendering
        - don't hide/show cursor on environments that don't support it
        - don't try to update the title on environments that don't support it
        - auto-select solo required item for `git-helper` use case
        - fix blockquote line being a different color or breaking colors
    - `confirm`: 
        - rewrote for no-TTY support, looks and behave differently, now being clear about all options and appropriate keys
        - also fixes a bug with `--required` not always enforcing requirement
        - rejig color and no-color rendering
    - `eval-helper`:
        - fix undefined columns
    - `echo-clear-lines`:
        - rewritten to support all known edge cases
        - `--piped` option has been removed, a new `--here-string` argument has been added
        - it calls `echo-wrap` to ensure correct behaviour
        - supports `--column=*` alias for `--width=*`, and supports `--count=*` option if we already know how many lines we want to clear
    - `echo-count-lines`:
        - note in its help that it does not wrap lines
    - `echo-revolving-door`:
        - rewrote to fix all known edge cases
    - `echo-wrap`:
        - fix help errors not showing, support --column=* alias for --width=*
    - `is-shapeshifter`:
        - rewrote and add tests, to fix all known edge cases
        - now uses `bash.bash` helpers
    - `read-key`:
        - went through and redid all the keys
        - add `(ctrl|alt|shift)+(left|right)` macos keys, add `ctrl+a` (all key)
        - rewrote for no-TTY support, and now supports multiple keys, as such callers need to take note
        - supporting multiple keys is essential in line buffer mode or when in bash v3 (bash v3 has a delay in which multiple keys may be sent)
        - will now output unknown keys

- `brew`:
    - fix duplication in error messages and improve styling

- `brew-installed`:
    - support `--quiet` argument
    - return exit status `46` if brew is not installed

- `(command-(exists|missing|working)|is-(brew|needle))`:
    - add tests
    - `command-working`:
        - if the command is missing, return exit status `3` instead of `1`

- `debug-network:`
    - fix bad maintenance over the years
    - add comment to avoid 632e605

- `dorothy`:
    - install `coreutils` on alpine, remove `ps` dependency
    - fix bad `remote_name` automation
    - prompt for lint upgrade before dev deps, to speed up UX
    - moved common helpers into `bash.bash`, and improved them
    - source the `bash.bash` that is appropriately local or remote
    - rejig clone and update to support `slug`/`branch`/`reference`/`remote-name`, properly enabling support for branches, PRs, and forks
    - remove update technique for legacy dorothy installations
        - turns out the modern technique was always failing due to issues in `git-helper` which are now resolved
        - the legacy technique is for before `git-helper` existed, and it has existed for a long time now, multiple years
    - add `__eval_wrap` helper for debug clarity

- `ask`, `dorothy`,` setup-util-bash`, `setup-util-gh`
    - use new `get-terminal-reactivity-support` command instead of the deprecated `is-interactive`

- `dorothy`, `dorothy-warnings`, `init.*`:
    - added `dorothy-warnings` for deprecations and other warnings
    - warnings will be output on new terminal instances if there are new warnings

- `dorothy`, `fs-realpath`, `symlink-helper`:
    - detect when alpine uses `busybox` or `coreutils` and react accordingly
        - `readlink` on alpine's `busybox` doesn't support needed options
        - `realpath` on alpine's `busybox` doesn't support any options
    - move alpine's `busybox` implementation to fallback
    - fix alpine symlink failures
    - `symlink-helper`:
        - fix situation when the symlink is broken

- `echo-regexp`, `echo-regexp.ts`:
    - fix including `stdinargs.bash` when it didn't need to
    - better support for character classes, supporting `[[:XXX:]]` and `[:XXX:]` forms

- `echo-file` and `get-terminal-theme`:
    - add `get-terminal-theme --ignore-cache` option
    - correctly handle `bat` theme in cases where theme could not be detected, such as inside `ssh -T`
    - improve the styling of `bat` and `plain` output

- `echo-quote`, `echo-escape-command`, `echo-escape-bash`, `echo-lines`, `config-helper`, `dorothy-config`, `fs-size`, `mount-helper`, `nvm-env`, `setup-dns`, `sudo-helper`, `video-merge`, and so on:
    - use appropriate quote format for the context
    - `echo-lines` and `config-helper`:
        - `--quote=` option now supports the type of quote format you want
        - in `config-helper` this will default to autodetection based on the filetype we are writing to
    - `echo-quote`:
        - add tests
        - support `--single` and `--double` arguments

- `(echo-(html|url)-coder|fs-dequarantine|get-desktop-theme|setup-util-(1password-cli|apple-(cli-tools|rosetta)|yt-dlp))`:
    - fix incorrect function names

- `echo-escape-special`:
    - fix everything being dumped together

- `echo-mkdir`:
    - moved to beta commands as it is no longer used internally
    - fixed it never actually working

- `echo-nothing-or-fail`:
    - moved to beta commands as it is no longer used internally
    - fixed it never actually working

- `echo-substr` new command:
    - as command alias for `__substr` helper in `bash.bash`

- `echo-wait`:
    - rejig some logic for clarity

- `expand-path-zsh` renamed to `expand-path.zsh`:
    - also rewrote for clarity and modern conventions

- `fs-rm`:
    - note when moving to trash is not available

- `get-font`:
    - improve performance

- `get-installer`:
    - add aliases for `setup-util-ps`

- `git-helper`:
    - improve the way remotes and protocols are gathered
    - add `verify` action and step
    - fix `path` not sent to `review` action
    - correctly handle no remotes, no origin, and multiple remotes

- `is-abort`:
    - remove `ignore-empty` option, and hardcode how it is handled

- `is-nonempty-file`:
    - add help and options

- `setup-util`:
    - redid the clearing functionality to implement modern conventions and support a wide range of edge cases, such as when installer outputs more than LINES, or when clear is not available, or when the installer needs interaction
    - don't remove non-xdg-bins when upgrading as that causes issues with homebrew if the homebrew-installed bin was removed but homebrew still thinks it is installed
    - improve performance by using new `bash.bash` helpers
    - improve performance by reducing separate `echo-style` calls
    - if uninstalling/upgrading the currently executing `bash`, output a warning
    - only output the installer output if there was output
    - improved performance by merging a few redundant calls
    - output to `/dev/tty` if supported and appropriate

- `setup-util-bash`:
    - compact `echo-style` calls for performance, include `setup-util --check` for performance
    - improvements to clearing
    - two workarounds implemented for when uninstalling/upgrading the currently executing bash
        - we will try to reload the command with an alternative bash
        - if unable to load an alternative bash, a notice will be outputted on what to expect

- `setup-environment-commands`: 
    - improve performance on `rubygems`
    - improve performance by using new `bash.bash` helpers
    - add support for `commands.deprecated`

- `setup-mac-brew`:
    - verify homebrew git repo is setup correctly with new `git-helper verify` action

- `setup-util-node`:
    - verify nvm git repo is setup correctly with new `git-helper verify` action

- add `setup-util-ps`

- `until-success`:
    - rewrote for new TTY conventions
    - things that should be output to the terminal device file are now outputted there
    - added `--delay=...` alias for `--interval=...`

- `waiter`:
    - removed vestigial `--magic` option

- `what-is-running`:
    - support `ps` and `procs`, and install the appropriate fallback if both are missing

- `video-merge`:
    - use `is-headful` which is appropriate, instead of the deprecated `is-tty`

- `oz`:
    - cleanup a little bit, now that docs are elsewhere

- new beta commands:
    - added `echo-revolving-screen` command that unfortunately doesn't work properly and I don't know why

merged commits:

- 4f66b71 [re-add --needle= prefix on needle option on is-needle calls, to prevent edge case failures when the needle looks like an option](4f66b71)
- 1c7b588 [brew-installed: fix comment typos](1c7b588)
- d2b29cb [use the correct quote/escape for the context](d2b29cb)
    - /ref https://github.com/bevry/dorothy/actions/runs/11512899768
- d605711 [ask: rewrote for new stdin/tty learnings, properly support no-TTY](d605711)
- 613cec8 [improvements to TTY and STDIN reporting](613cec8)
- 0dfebda [ask/choose/confirm: fix broken tests, although ask test has a clearing issue when testing](0dfebda)
- 099bb6b [ask/choose/confirm: complete no-TTY support](099bb6b)
- 52958a2 [shellcheck: ignore SC2016, it is always intentional](52958a2)
- 8324b75 [mprovements to clearing, and resolve setup-util(-bash) edge case](8324b75)
- 713322d [echo-clear-lines: prep for improvements](713322d)
- e8a0640 [readme: list @balupton's github list of Dorothy User Configurations](e8a0640)
- a3ba02f [debug-network: add comment to avoid 632e605](a3ba02f)
- a37639d [consistentcy for commands.deprecated](a37639d)
- d99a9a6 [add dorothy-warnings, a deprecation system](d99a9a6)
- 888b090 [styles: newline, clear, alt screen buffer adjustments for CI](888b090)
- cc483c3 [styles: newline and clear if alt screen buffer not supported](cc483c3)
    - /ref https://github.com/bevry/dorothy/actions/runs/11358441972/job/31592966478#step:2:2805
- 598918e [style.bash: clear screen if alt buffer not available](598918e)
    - /ref https://github.com/bevry/dorothy/actions/runs/11358242517/job/31592464176#step:2:3754
- 648843c [get-terminal-(alternative|title)-support fixed](648843c)
- 383e36a [don't output screen buffer styles if not supported](383e36a)
    - /ref https://github.com/bevry/dorothy/actions/runs/11357972748/job/31591723554#step:3:785
- b4ef3c8 [bash.bash: add __substr shim, fix for cpr and echo-style](b4ef3c8)
    - /ref https://github.com/bevry/dorothy/actions/runs/11345441621/job/31552330617#step:3:2984
- 5405c39 [choose and read-key bugfixes](5405c39)
- f62bebd [eval-helper: fix undefined columns](f62bebd)
    - /ref https://github.com/bevry/dorothy/actions/runs/11340094950/job/31535923344#step:4:71
- fc24131 [echo-file: fix bat usage, all: improve styling](fc24131)
- 41de4c2 [dorothy: install coreutils installed on alpine, remove ps dependency](41de4c2)
    - /ref https://github.com/bevry/dorothy/actions/runs/11338434503/job/31531633175#step:4:58
- a9a57f5 [dorothy, fs-realpath: detect when alpine uses busybox or coreutils and react accordingly](a9a57f5)
    - /ref https://github.com/bevry/dorothy/actions/runs/11337845512/job/31530095505#step:4:56
- 99ac325 [dorothy, fs-realpath: move alpine's implementation to fallback](99ac325)
    - /ref https://github.com/bevry/dorothy/actions/runs/11337745165/job/31529845513#step:4:36
- 369ff92 [dorothy, fs-realpath: readlink on alpine doesn't support needed options](369ff92)
- 06a2cae [dorothy, fs-realpath: realpath on alpine doesn't support options](06a2cae)
- acee8c9 [dorothy, symlink-helper: fix alpine symlink failures](acee8c9)
    - /ref 
https://github.com/bevry/dorothy/actions/runs/11323459946/job/31486170602#step:4:11
- 5def66e [WIP: removed tty.bash for get-terminal-* commands](5def66e)
- c71c557 [is-tty: add proc and -p tests](c71c557)
- 8a4583b [bash.bash: linting](8a4583b)
- e15cab8 [choose: try to fix choose prompt question title not showing on CI](e15cab8)
- 6b7f98a [choose: auto-select solo required item for git-helper use case](6b7f98a)
    - /ref https://github.com/bevry/dorothy/actions/runs/11267661909/job/31333162360#step:3:1812
    - /ref https://github.com/bevry/dorothy/actions/runs/11115550380/job/30884206594#step:3:1873
    - /ref https://github.com/bevry/dorothy/actions/runs/11277567607/job/31364020536#step:3:1873
    - /ref https://github.com/bevry/dorothy/actions/runs/11286493561/job/31390927875#step:3:2128
    - /ref https://github.com/bevry/dorothy/actions/runs/11286573839/job/31391154425#step:3:1896
    - /ref https://github.com/bevry/dorothy/actions/runs/11286652092/job/31391365190#step:3:2141
    - /ref https://github.com/bevry/dorothy/actions/runs/11287510416/job/31393567790#step:3:1797
    - /ref https://github.com/bevry/dorothy/actions/runs/11287614318/job/31393844817#step:3:1798
    - /ref https://github.com/bevry/dorothy/actions/runs/11287762099/job/31394250547#step:3:1861
- 64c9cf6 [bash.bash: add has_needles helper for performance](64c9cf6)
- 4193b8e [dorothy: fix bad remote_name automation](4193b8e)
- 7a3236c [setup-environment-commands: improve performance](7a3236c)
- 4098286 [confirm, read-key: linting](4098286)
- c48e182 [command-(missing|working): fix bad commit](c48e182)
- 7b9d99c [read-key: turns out the all key is ctrl+a](7b9d99c)
- ee11038 [fix mistakes in WIP commits](ee11038)
- b60ef24 [dorothy: rejig clone and update to all support slug/branch/reference/remote-name](b60ef24)
- 47f29cb [dorothy: add __eval_wrap helper for debug clarity](47f29cb)
- 2fc7757 [echo-wait: rejig some logic for clarity](2fc7757)
- 1dbdd43 [brew: fix duplication in error messages and improve styling](1dbdd43)
- 2582cdf [confirm: rejig color and no-color rendering](2582cdf)
- ba73e79 [read-key: rejig some lines for internal clarity](ba73e79)
- c7f31b1 [bash.bash: add common helpers for performance](c7f31b1)
- 2c1b508 [WIP: choose, confirm, read-key: improve no-TTY support](2c1b508)
- 7f91040 [choose, echo-wrap, read-key: support no-TTY](7f91040)
  • Loading branch information
balupton authored Oct 27, 2024
1 parent 624d1db commit c4d1e1f
Show file tree
Hide file tree
Showing 251 changed files with 6,329 additions and 3,379 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/dorothy-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
setup-util-dash
setup-util-ksh
setup-util-carapace
dorothy install
dorothy --slug=${{ github.repository }} install
# nu -c 'echo $nu.loginshell-path'
- name: 'Dorothy Login Shell: bash'
shell: bash -leo pipefail {0}
Expand Down Expand Up @@ -172,15 +172,15 @@ jobs:
if command -v apt-get; then
# for ubuntu/debian/kali
apt-get update
apt-get install -y bash curl git
apt-get install -y bash curl
elif command -v zypper; then
# for opensuse
zypper --non-interactive --gpg-auto-import-keys refresh
zypper --non-interactive install bash curl
elif command -v apk; then
# for alpine
apk update
apk add bash curl git grep
apk add bash curl
elif command -v pacman; then
# for manjaro (manjaro CI image doesn't have pamac installed) and arch
pacman-key --init
Expand Down
13 changes: 12 additions & 1 deletion .trunk/configs/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# https://github.com/trunk-io/configs
enable=all
source-path=SCRIPTDIR
disable=SC2154,SC2164,SC2250,SC2310,SC2249,SC2312,SC1090,SC1091,SC2317
disable=SC2154,SC2164,SC2250,SC2310,SC2249,SC2312,SC1090,SC1091,SC2317,SC2016,SC2059
# https://www.shellcheck.net/wiki/SC2154 var is referenced but not assigned.
# https://www.shellcheck.net/wiki/SC2164 Use cd ... || exit in case cd fails.
# https://www.shellcheck.net/wiki/SC2250 Prefer putting braces around variable references even when not strictly required.
# https://www.shellcheck.net/wiki/SC2310 This function is invoked in an 'if' condition so set -e will be disabled. Invoke separately if failures should cause the script to exit.
# https://www.shellcheck.net/wiki/SC2249 Consider adding a default *) case, even if it just exits with error.
# https://www.shellcheck.net/wiki/SC2312 Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore).
# https://www.shellcheck.net/wiki/SC1090 Can't follow non-constant source. Use a directive to specify location
# https://www.shellcheck.net/wiki/SC1091 Not following: (error message here)
# https://www.shellcheck.net/wiki/SC2317 Command appears to be unreachable. Check usage (or ignore if invoked indirectly).
# https://www.shellcheck.net/wiki/SC2016 Expressions don't expand in single quotes, use double quotes for that.
# https://www.shellcheck.net/wiki/SC2059 Don't use variables in the printf format string. Use printf "..%s.." "$foo".
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Alpine:

```bash
doas apk update
doas apk add bash curl grep
doas apk add bash curl
```

Manjaro:
Expand Down Expand Up @@ -273,7 +273,8 @@ Use these sources to find inspiration for your own user commands and configurati
- [@balupton](https://github.com/balupton) / [dotfiles](https://github.com/balupton/dotfiles): uses Fish as login shell, plenty of Bash commands
- [@molleweide](https://github.com/molleweide) / [dotfiles](https://github.com/molleweide/dotfiles): uses Zsh as login shell, plenty of Bash commands, kmonad user
- [@jondpenton](https://github.com/jondpenton) / [dotfiles](https://github.com/jondpenton/dotfiles): uses Nu as login shell, plenty of Nu commands
- if you use Dorothy, send a pull request to add your own user configuration to this list.
- [See more Dorothy User Configurations](https://github.com/stars/balupton/lists/dorothy-user-configurations)
- To feature your own Dorothy User Configuration, send a pull request.

After installing Dorothy, there will now a plethora of commands available to you. You can invoke any stable command with `--help` to learn more about it. The most prominent commands are noted below.

Expand Down
11 changes: 0 additions & 11 deletions commands.beta/choose-menu

This file was deleted.

11 changes: 0 additions & 11 deletions commands.beta/choose-option

This file was deleted.

2 changes: 1 addition & 1 deletion commands.beta/echo-escape-special
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function echo_escape_special() (
}

function on_input {
printf '%q' "$1"
printf '%q\n' "$1"
}

stdinargs "$@"
Expand Down
File renamed without changes.
14 changes: 9 additions & 5 deletions commands/echo-mkdir → commands.beta/echo-mkdir
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ function echo_mkdir() (
}

# process our own arguments, delegate everything else to stdinargs
local item option_sudo='no' option_args=() option_quiet
option_quiet="$(echo-quiet-enabled -- "$@")"
local item option_quiet='' option_sudo='no' option_args=()
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--no-quiet'* | '--quiet'* | '--no-verbose'* | '--verbose'*) ;; # handled by echo-quiet-enabled
'--no-verbose'* | '--verbose'*)
option_quiet="$(get-flag-value --non-affirmative --fallback="$option_quiet" -- "$item")"
;;
'--no-quiet'* | '--quiet'*)
option_quiet="$(get-flag-value --affirmative --fallback="$option_quiet" -- "$item")"
;;
'--no-sudo'* | '--sudo'*)
option_sudo="$(get-flag-value --affirmative --fallback="$option_sudo" -- "$item")"
;;
Expand All @@ -60,11 +64,11 @@ function echo_mkdir() (
# construct command
if test "$option_sudo" = 'yes'; then
function __mkdir {
sudo-helper --reason="Your sudo/root/login password is required to make the directory: $*" -- mkdir -p "$@"
sudo-helper --reason='Your sudo/root/login password is required to ensure the directory exists:' -- mkdir -p "$@"
}
else
function __mkdir {
mkdir -p
mkdir -p "$@"
}
fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function echo_nothing_or_fail() (
return 1 # failure
}

function on_on_lines {
function on_no_lines {
return 0 # success
}

Expand Down
101 changes: 101 additions & 0 deletions commands.beta/echo-revolving-screen
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash

function echo_revolving_screen() (
source "$DOROTHY/sources/bash.bash"

# =====================================
# Arguments

function help {
cat <<-EOF >/dev/stderr
ABOUT:
Continously clear the output of a command, showing only the latest output, then clearing it upon completion.
USAGE:
(echo-lines -- 1 2; sleep 2; echo-lines -- 3 4; sleep 2) | echo-revolving-door [...options]
# outputs 2, then waits, then outputs 4, then waits, then clears
OPTIONS:
--columns=<columns>
The number of columns to display. If not provided, the terminal's columns will be used. If the terminal's columns cannot be determined, or if <= 0, then the full line will be displayed.
EOF
if test "$#" -ne 0; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

# process
local item option_lines='' option_columns=''
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--lines='*) option_lines="${item#*=}" ;;
'--columns='*) option_columns="${item#*=}" ;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) help "An unrecognised argument was provided: $item" ;;
esac
done

# determine columns
if test -z "$option_lines" -o -z "$option_columns"; then
local terminal_size=()
mapfile -t terminal_size < <(get-terminal-lines-and-columns || :)
if test "${#terminal_size[@]}" -eq 2; then
if test -z "$option_lines"; then
option_lines="${terminal_size[0]}"
fi
if test -z "$option_columns"; then
option_columns="${terminal_size[1]}"
fi
fi
fi

# =====================================
# Action

if test -z "$option_lines" -o -z "$option_columns" || test "$option_lines" -le 0 -o "$option_columns" -le 0; then
cat
else
local terminal_device_file clear_screen
terminal_device_file="$(get-terminal-device-file)"
clear_screen=$'\e[H\e[J'

local input total_lines=0 status wrapped lines=0
while IFS= read -r input || test -n "$input"; do
eval_capture --statusvar=status --outputvar=wrapped \
-- gfold --width="$option_columns" <<<"$input"
#-- echo-wrap --width="$option_columns" -- "$input"
if test "$status" -ne 0; then
echo-error "$wrapped"
return "$status"
fi
# count lines
lines="$(echo-count-lines -- "$wrapped")"
total_lines="$((total_lines + lines))"
if test "$total_lines" -ge "$option_lines"; then
if test "$lines" -ge "$option_lines"; then
# this line is very long and wraps across all lines of our screen, this is an edge case
# use a coreutil to trim the excess lines
__print_lines "${clear_screen}${wrapped}" | head -n "$option_lines"
else
# clear the screen
__print_lines "${clear_screen}${wrapped}" >"$terminal_device_file"
fi
total_lines="$lines"
else
# print the wrapped output
__print_lines "$wrapped" >"$terminal_device_file"
fi
done
# we are now done, so clear wrapped
__print_string "${clear_screen}" >"$terminal_device_file"
fi
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
echo_revolving_screen "$@"
fi
2 changes: 1 addition & 1 deletion commands.beta/edit-dns
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function edit_dns() (

if get-app --quiet -- 'ProtonVPN'; then
__print_lines 'ProtonVPN enforces its own DNS'
elif command-exists -- dnscrypt-proxy; then
elif __command_exists -- dnscrypt-proxy; then
__print_lines 'Editing DNS configuration of dnscrypt-proxy'
edit --wait -- "${HOMEBREW_PREFIX:-"/usr/local"}/etc/dnscrypt-proxy.toml"
else
Expand Down
6 changes: 3 additions & 3 deletions commands.beta/ensure-trailing-newline
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function ensure_trailing_newline() (
if test -f "$path"; then
if test "$(tail -n1 <"$path" | wc -l)" -eq 0; then
if test -w "$path"; then
echo >>"$path"
__print_line >>"$path"
else
echo-style --error="$0: File is not writable: " --code="$path" >/dev/stderr
echo-style --error1='File is not writable: ' --code-error1="$path" >/dev/stderr
result=13 # EACCES 13 Permission denied
fi
fi
else
echo-style --error="$0: Path is not a file: " --code="$path" >/dev/stderr
echo-style --error1='Path is not a file: ' --code-error1="$path" >/dev/stderr
result=2 # ENOENT 2 No such file or directory
fi
done
Expand Down
2 changes: 1 addition & 1 deletion commands.beta/ensure-trailing-slash
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function ensure_trailing_slash() (
__print_lines "$path"
fi
else
echo-style --error='Not a file nor directory:' ' ' --code="$path" >/dev/stderr
echo-style --error1='Not a file nor directory: ' --code-error1="$path" >/dev/stderr
return 1
fi
done
Expand Down
5 changes: 2 additions & 3 deletions commands.beta/get-codec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function get_codec_test() (
ogg_media_file="$(fs-temp --directory='get-codec' --file --extension='ogg')"
down --url="$ogg_media_url" --filepath="$ogg_media_file"

if command-exists -- ffprobe; then
if __command_exists -- ffprobe; then
eval-tester --name='ogg vorbis file is ogg vorbis' --stdout='vorbis' \
-- get-codec --path="$ogg_media_file"
else
Expand Down Expand Up @@ -69,13 +69,12 @@ function get_codec() (
# =====================================
# Action

if ! command-exists -- ffprobe; then
if __command_missing -- ffprobe; then
echo-error 'ffprobe is required for this command.'
return 6 # ENXIO 6 No such device or address
fi

if test "$option_verbose" = 'no'; then
# trunk-ignore(shellcheck/SC2016)
ffprobe -i "$option_path" 2>&1 | echo-regexp -fon --regexp='Audio: ([\w\d-]+)' --replace='$1'
else
ffprobe -i "$option_path"
Expand Down
4 changes: 4 additions & 0 deletions commands.beta/get-terminal-lines-and-columns.stty
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

result="$(stty size 2>/dev/null || exit 19)" || exit # ENODEV 19 Operation not supported by device
printf '%s\n' "${result// /$'\n'}"
8 changes: 8 additions & 0 deletions commands.beta/get-terminal-lines-and-columns.tput
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

{
tput lines
tput cols
} 2>/dev/null || {
exit 19 # ENODEV 19 Operation not supported by device
}
Loading

0 comments on commit c4d1e1f

Please sign in to comment.