Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ask, choose, confirm: implement #99, #213, #188 #229

Merged
merged 41 commits into from
Aug 8, 2024
Merged

Conversation

balupton
Copy link
Member

@balupton balupton commented Jun 12, 2024

Implements #99, #213, #188

renames:

  • many bash.bash helpers now have __ prefix to indicate they are safe for conditional usage
    • require_array is now __require_array
    • require_globstar is now __require_globstar
    • print_string is now __print_string
    • print_line is now __print_line
    • print_lines is now __print_lines
    • lowercase_string is now __lowercase_string
    • uppercase_first_letter is now __uppercase_first_letter
    • ^ and so on, b/c aliases were provided
  • choose-option is now choose
    • choose-option --filter= is now choose --default-fuzzy=
  • choose-menu is now choose --index
  • echo-element --open= --close= --openclose= is now echo-style --element= --/element= --element/=
  • echo-segment functionality is now inlined into echo-style, to avoid conflict, prior echo-style functionality changed to:
    • echo-style --h1= --h2= --h3= is now echo-style --header1= --header2= --header3=
    • echo-style --g1= --g2= --h3= is now echo-style --good1= --good2= --good3=
    • echo-style --e1= --e2= --e3= is now echo-style --error1= --error2= --error3=
    • echo-style --n1= --n2= --n3= is now echo-style --notice1= --notice2= --notice3=

new functionality:

  • because echo-style now inlined echo-element and echo-segment functionality, complex heading and element renderings are now possilbe
  • choose, ask, confirm now all support --linger for retaining the question and answer in the TTY afterwards, and consistent question and question detail argument handling
  • ask now supports editing a default value
  • choose interface has been rewritten for clarity and performance, with improved and comprehensive interaction and paging support
  • choose no longer supports filters, but instead --default-fuzzy which has clearer usage, which is especially useful when say selecting a matching encryption/hashing algorithm
  • read-key now supports way more special keys
  • choose and ask now have a --skip-default= aka --no-confirm-default= option

changed functionality:

  • callers of choose, ask, confirm now have consistent confirm and linger usage
  • choose will default to defaults on timeout, rather than a modified but not confirmed selection
  • choose, ask, confirm now have clearer and speedier timeout handling, as well as consistent timeout styling that respects color/nocolor mode
  • echo-style and styles.bash has been rewritten to clearly support color, nocolor, and end styles, as well as performance has improved by only doing a single write rather than streaming writes, and missing styles will not be output with --debug mode
  • echo-trim-colors better support for inline strings
  • get-flag-value removed affirmative options and simplified with only boolean and invert options, added more tests

trivial changes:

  • choose-path fix help arguments
  • dorothy fix false flag unsafe name checks

places to see the most improvements:

  • echo-checksum --algorithm=sha
  • setup-util-nerd-fonts
  • setup-utils --configure
  • sparse-vault
  • ssh-helper

balupton and others added 16 commits February 25, 2024 12:42
* ask: support editing the default value /ref 6357fac
* ask: as read now supports defaults, redo the confirm flow /ref ddff467
* ask: use cursor rather than alt tty, implement --linger /ref 4d3e6fb
- implement `--linger`, default to enabled
- fix readline defaults failing when programatic stdin
- fix tests
- fix `echo-clear-lines` not retaining inlines which caused incorrect lines to be cleared
- adapt callers for new defaults and expectations
Still need to imlpement:
- `--linger`
- `--confirm-default=no`
- `--confirm-input`

...and to make them consistent between ask, confirm, choose.

This removes `--filter` from choose, as it is better facilitated by the new `--default(s)-fuzzy=`

/close #188
changes:

- bash.bash:
    - native __ prefix on methods, b/c alias (rather than non-performant inverse)
    - removed unused __print_lines_no_trail
- ask, choose, confirm:
    - fix question title and body implementation, title is uniformly bold
    - linger answer will be dim
    - fix outdated linger option implementation
    - implement confirm on choose
    - implement linger on confirm
    - new options implemented in caller where appropriate
- dorothy: remove require_globstar as already provided by bash.bash
- get-flag-value:
    - remove unused complex --*-fallback options, fix inversions
    - update tests
- macos-settings: remove fodder usage, was unnecessary

todos:

- need to update tests for ask, confirm, choose
…ults, which unfortunately was left behind on my mac mini when I did the rebase on my macbook

# Conflicts:
#	commands/ask
#	commands/choose-menu
#	commands/confirm
#	commands/setup-git
#	commands/sparse-vault

Also:

- implement better timeout and error handling, no need for a delay anymore, and now commentary visible in linger
- update callers accordingly, also update `dorothy` to remove unnecessary indentation, update `echo-lines` to remove typo in comment
- add tests for the `echo-trim-colors` new handling of inline

Still need to:

- `choose`:
    - update tests for rewrite
    - fix duplicate `DEFAULTS` in choose (probably a long-standing regression)
    - support multiple default matches in `--no-multi` (as done by `echo-checksum`) by adding the concept of preferences
    - see details at #188 (comment)
- add linger and timeout tests to `ask`, `choose`, `confirm`
- ask/choose/confirm: remove delay on errors
- choose-path: fix help usage convention
- read-key: rewrote hotkey handling for documented cross-os support, with support for more hotkeys
- styles: documented terminal emulator support for all complex styles
- choose: rewrite for page-up, page-down, home, end support, and do infrastructure for new confirm menu and paging indicators
this adds the infrastructure for a possible "cancel" menu mode
/close #231

Co-authored-by: Benjamin Lupton <b@lupton.cc>
changes:
- deprecated `echo-element` and `echo-segment`, as they are now supported by `echo-style`
- `echo-style` and `styles.bash` updated for begin/end and color/nocolor styles
- `echo-style` updated to do the minimal writes for performance and simplicity
- `styles.bash` now has the `choose` styles, so `choose` theme can be customised by the user
- `choose` fixed and styled no selection / no items

/close #232
/ref #231 #188
- choose:
    - will now resize terminal if terminal too small, and if resize failed, output a error mode until resolved
    - fix terminal title showing false count for selected after refactor
    - fix middle and bottom bars being incorrect after refactor
    - fixed commas appearing in legend
    - fixed odd behaviour with cursor behaviour
    - fixed bad selected rendering
- read-key: improve documentation, note keyboards, note symbol divergence, fix macos paging hotkeys
- styles.bash: fix unbound var

/ref #188
now that they are deprecated as their functionality has been added to echo-style

/ref #188
- choose: `is_preference` -> `__is_preference`
- dorothy: fix false unsafe flagging on for conditionals

/ref https://github.com/bevry/dorothy/actions/runs/10124332719/job/27998559165
/ref #188
echo-style, styles.bash: added new helpers for caching color/nocolor lookups

refactors also fix respect of no-color

/ref #188
choose:
- add more tests
- fix conditional actions causes crashes when conditions not met

echo-style, styles:
- fix fetching end styles
choose, styles.bash:
- renamed default to defaults

choose:
- do revert before confirm cancel
- introduce fallbacks array for containing a single default for single mode
- rename last_index to last to avoid confusion
ask:
- fix non TTY causing failure
- fix repeat prompts concat instead of reset

bash.bash:
- fix outdated bash version

ask, choose:
- linting

/ref https://github.com/bevry/dorothy/actions/runs/10284567335/job/28460877061
balupton and others added 11 commits August 8, 2024 00:28
fetch: fix curl still causing pipe errors

setup-util-1password, setup-util-1password-cli: update for brew changes
config-helper just seems flakey in general, so reduce its timeout and run it three times

choose failures were actually read-key failures, so reduce the timeout

dorothy move unsafe calls to linting, and use arrays with flakey array

/ref d513c42
/ref https://github.com/bevry/dorothy/actions/runs/10289349834/job/28476911341
dorothy: add echo-wait to flakey

choose, config-helper: use 3 second delay on ci

/ref https://github.com/bevry/dorothy/actions/runs/10291311574/job/28483286557
@balupton
Copy link
Member Author

balupton commented Aug 7, 2024

First commits for this were November 11th 2023, so 9 months of work getting merged in. 207 files changed, and 5000 new lines and 3000 deleted lines, crazy!

CleanShot 2024-08-08 at 05 41 22@2x

pipx now uses .local, and it seems if the old `Library/Application Support/pipx` location exists, everything falls apart, so remove the old location as well as the new preferred one (as usual) before reinstalling

solves these errors:

```
< 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'katcr' >
⚠️ Found a space in the home path. We heavily discourage this, due to multiple
    incompatibilities. Please check our docs for more information on this, as
    well as some pointers on how to migrate to a different home path.
creating virtual environment...
⚠️  Not removing existing venv /Users/balupton/Library/Application
    Support/pipx/venvs/katcr because it was not created in this session
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/__main__.py", line 14, in <module>
    sys.exit(cli())
             ^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 1149, in cli
    return run_pipx_command(parsed_pipx_args, subparsers)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 267, in run_pipx_command
    return commands.install(
           ^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/commands/install.py", line 92, in install
    venv.create_venv(venv_args, pip_args, override_shared)
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/venv.py", line 172, in create_venv
    pipx_pth = get_site_packages(self.python_path) / PIPX_SHARED_PTH
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 132, in get_site_packages
    output = run_subprocess(
             ^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 183, in run_subprocess
    completed_process = subprocess.run(
                        ^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/balupton/Library/Application Support/pipx/venvs/katcr/bin/python'
Installing to existing venv 'katcr'

</ 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'katcr' >[1]
< 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'poetry' >
⚠️ Found a space in the home path. We heavily discourage this, due to multiple
    incompatibilities. Please check our docs for more information on this, as
    well as some pointers on how to migrate to a different home path.
creating virtual environment...
⚠️  Not removing existing venv /Users/balupton/Library/Application
    Support/pipx/venvs/poetry because it was not created in this session
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/__main__.py", line 14, in <module>
    sys.exit(cli())
             ^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 1149, in cli
    return run_pipx_command(parsed_pipx_args, subparsers)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 267, in run_pipx_command
    return commands.install(
           ^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/commands/install.py", line 92, in install
    venv.create_venv(venv_args, pip_args, override_shared)
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/venv.py", line 172, in create_venv
    pipx_pth = get_site_packages(self.python_path) / PIPX_SHARED_PTH
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 132, in get_site_packages
    output = run_subprocess(
             ^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 183, in run_subprocess
    completed_process = subprocess.run(
                        ^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/balupton/Library/Application Support/pipx/venvs/poetry/bin/python'
Installing to existing venv 'poetry'

</ 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'poetry' >[1]
< 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'stig' >
⚠️ Found a space in the home path. We heavily discourage this, due to multiple
    incompatibilities. Please check our docs for more information on this, as
    well as some pointers on how to migrate to a different home path.
creating virtual environment...
⚠️  Not removing existing venv /Users/balupton/Library/Application
    Support/pipx/venvs/stig because it was not created in this session
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/__main__.py", line 14, in <module>
    sys.exit(cli())
             ^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 1149, in cli
    return run_pipx_command(parsed_pipx_args, subparsers)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 267, in run_pipx_command
    return commands.install(
           ^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/commands/install.py", line 92, in install
    venv.create_venv(venv_args, pip_args, override_shared)
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/venv.py", line 172, in create_venv
    pipx_pth = get_site_packages(self.python_path) / PIPX_SHARED_PTH
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 132, in get_site_packages
    output = run_subprocess(
             ^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 183, in run_subprocess
    completed_process = subprocess.run(
                        ^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/balupton/Library/Application Support/pipx/venvs/stig/bin/python'
Installing to existing venv 'stig'

</ 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'stig' >[1]
< 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'youtube_dl' >
⚠️ Found a space in the home path. We heavily discourage this, due to multiple
    incompatibilities. Please check our docs for more information on this, as
    well as some pointers on how to migrate to a different home path.
creating virtual environment...
⚠️  Not removing existing venv /Users/balupton/Library/Application
    Support/pipx/venvs/youtube-dl because it was not created in this session
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/__main__.py", line 14, in <module>
    sys.exit(cli())
             ^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 1149, in cli
    return run_pipx_command(parsed_pipx_args, subparsers)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/main.py", line 267, in run_pipx_command
    return commands.install(
           ^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/commands/install.py", line 92, in install
    venv.create_venv(venv_args, pip_args, override_shared)
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/venv.py", line 172, in create_venv
    pipx_pth = get_site_packages(self.python_path) / PIPX_SHARED_PTH
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 132, in get_site_packages
    output = run_subprocess(
             ^^^^^^^^^^^^^^^
  File "/Users/balupton/Library/Python/3.12/lib/python/site-packages/pipx/util.py", line 183, in run_subprocess
    completed_process = subprocess.run(
                        ^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/balupton/Library/Application Support/pipx/venvs/youtube-dl/bin/python'
Installing to existing venv 'youtube-dl'

</ 'python3' '-m' 'pipx' 'install' '--include-deps' '--force' 'youtube_dl' >[1]
```
@balupton balupton changed the title ask, choose, confirm: implement #99, #213, #188 ask, choose, confirm: implement #99, #213, #188 🔥 Aug 8, 2024
@balupton balupton changed the title ask, choose, confirm: implement #99, #213, #188 🔥 ask, choose, confirm: implement #99, #213, #188 🧙‍♀️ Aug 8, 2024
@balupton balupton changed the title ask, choose, confirm: implement #99, #213, #188 🧙‍♀️ ask, choose, confirm: implement #99, #213, #188 Aug 8, 2024
@balupton balupton merged commit a461bc6 into master Aug 8, 2024
31 checks passed
@balupton balupton deleted the dev-defaults branch August 8, 2024 18:15
balupton added a commit that referenced this pull request Aug 10, 2024
`ask` and `choose` now use our own `echo-wrap` and `echo-count-lines` instead of `gfold`, `gfmt`, `gwc`.
The reasoning for this was that they did not handle ansi escape codes, including colours, nor indentation of broken lines correctly.
To achieve this, I have introduce `echo-gnu-command` to fetch a GNU command (or fallback), with optional install.
Calls to `echo-gnu-command` may need `--no-fallback` if it turns out they actually require the GNU variant.
Considering this, we may be able to remove the symlink alias inside `setup-util-gawk` and `setup-util-gsed`.

This also fixes an issue with `choose` where the right arrow (page down) was not working when on the last item of a page.

Finally, this improves the formatting of subsequent lines in choose, they now have a continious quote line.

I have also figreud out when the CI is showing truncated output for choose, it occurs with `paging_supported='no'` this will be resolved another day.
balupton added a commit that referenced this pull request Aug 10, 2024
`ask` and `choose` now use our own `echo-wrap` and `echo-count-lines` instead of `gfold`, `gfmt`, `gwc`.
The reasoning for this was that they did not handle ansi escape codes, including colours, nor indentation of broken lines correctly.
To achieve this, I have introduce `echo-gnu-command` to fetch a GNU command (or fallback), with optional install.
Calls to `echo-gnu-command` may need `--no-fallback` if it turns out they actually require the GNU variant.
Considering this, we may be able to remove the symlink alias inside `setup-util-gawk` and `setup-util-gsed`.

This also fixes an issue with `choose` where the right arrow (page down) was not working when on the last item of a page.

Finally, this improves the formatting of subsequent lines in choose, they now have a continious quote line.

I have also figreud out when the CI is showing truncated output for choose, it occurs with `paging_supported='no'` this will be resolved another day.
balupton added a commit that referenced this pull request Aug 10, 2024
`ask` and `choose` now use our own `echo-wrap` and `echo-count-lines` instead of `gfold`, `gfmt`, `gwc`.
The reasoning for this was that they did not handle ansi escape codes, including colours, nor indentation of broken lines correctly.
To achieve this, I have introduce `echo-gnu-command` to fetch a GNU command (or fallback), with optional install.
Calls to `echo-gnu-command` may need `--no-fallback` if it turns out they actually require the GNU variant.
Considering this, we may be able to remove the symlink alias inside `setup-util-gawk` and `setup-util-gsed`.

This also fixes an issue with `choose` where the right arrow (page down) was not working when on the last item of a page.

Finally, this improves the formatting of subsequent lines in choose, they now have a continious quote line.

I have also figreud out when the CI is showing truncated output for choose, it occurs with `paging_supported='no'` this will be resolved another day.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants