Last active
February 28, 2026 20:52
-
-
Save florinpatrascu/44e054d9254b12cfb9bf35f1fb934cee to your computer and use it in GitHub Desktop.
Tinkering with zsh and bash based on a cli proxy and formatter idea derived from Simon's post on mastodon: https://mastodon.social/@simonbs/116142608738376660
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # ============================================================================= | |
| # An original idea: https://mastodon.social/@simonbs/116142608738376660 | |
| # and a highly experimental terminal command formatter; kind of ... proxy'n | |
| # formatter for long terminal commands | |
| # ============================================================================= | |
| # Installation: | |
| # source ~/.bashrc or source ~/.zshrc | |
| # add to ~/.bashrc or ~/.zshrc: | |
| # source /path/to/cli_formatter.sh | |
| # ============================================================================= | |
| CLI_FORMATTER_MAX_LEN=${CLI_FORMATTER_MAX_LEN:-80} | |
| CLI_FORMATTER_INDENT=${CLI_FORMATTER_INDENT:-2} | |
| _CF_RESET='\033[0m' | |
| _CF_CYAN='\033[0;36m' | |
| _CF_YELLOW='\033[0;33m' | |
| _CF_DIM='\033[2m' | |
| # ============================================================================= | |
| # Takes a command as string, returns the multi-line version | |
| # ============================================================================= | |
| cmd_format() { | |
| local cmd="$1" | |
| local max_len="${CLI_FORMATTER_MAX_LEN}" | |
| local indent="${CLI_FORMATTER_INDENT}" | |
| local indent_str | |
| indent_str=$(printf '%*s' "$indent" '') | |
| # Poor man tokenization: split on spaces, respecting single/double quotes | |
| # Uses python3 if available for correct tokenization | |
| # I used AI here because i sk at python .. Rust anyone?! | |
| if command -v python3 &>/dev/null; then | |
| python3 - "$cmd" "$max_len" "$indent_str" <<'PYEOF' | |
| import sys, shlex | |
| cmd = sys.argv[1] | |
| max_len = int(sys.argv[2]) | |
| indent = sys.argv[3] | |
| # Skip formatting if shorter than the limit | |
| if len(cmd) <= max_len: | |
| print(cmd) | |
| sys.exit(0) | |
| try: | |
| tokens = shlex.split(cmd) | |
| except ValueError: | |
| # fallback: simple split | |
| tokens = cmd.split() | |
| if not tokens: | |
| print(cmd) | |
| sys.exit(0) | |
| lines = [] | |
| current = tokens[0] # first part (the command itself) | |
| for token in tokens[1:]: | |
| # Append the token to the current line and check if we exceed the limit | |
| test_line = current + ' ' + token | |
| if len(test_line) > max_len and current.strip(): | |
| lines.append(current + ' \\') | |
| current = indent + token | |
| else: | |
| current = test_line | |
| lines.append(current) # last line without backslash | |
| print('\n'.join(lines)) | |
| PYEOF | |
| else | |
| # standard shell fallback; split on spaces (no complex quoting support) | |
| local -a tokens | |
| if [[ -n "$ZSH_VERSION" ]]; then | |
| read -r -A tokens <<< "$cmd" | |
| else | |
| read -r -a tokens <<< "$cmd" | |
| fi | |
| if [[ ${#tokens[@]} -eq 0 ]]; then | |
| echo "$cmd" | |
| return | |
| fi | |
| local current="${tokens[0]}" | |
| local output="" | |
| for token in "${tokens[@]:1}"; do | |
| local test_line="$current $token" | |
| if (( ${#test_line} > max_len )) && [[ -n "${current// /}" ]]; then | |
| output+="$current \\\n" | |
| current="${indent_str}${token}" | |
| else | |
| current="$test_line" | |
| fi | |
| done | |
| output+="$current" | |
| printf "%b" "$output" | |
| fi | |
| } | |
| # ============================================================================= | |
| # BASH, usng the DEBUG hook as a trap | |
| # ============================================================================= | |
| if [[ -n "$BASH_VERSION" ]]; then | |
| _CF_LAST_CMD="" | |
| _CF_FORMATTED=0 | |
| _cf_preexec() { | |
| local cmd | |
| cmd="$(history 1 | sed 's/^[[:space:]]*[0-9]\+[[:space:]]*//')" | |
| # Avoid double execution | |
| if [[ "$cmd" == "$_CF_LAST_CMD" && "$_CF_FORMATTED" -eq 1 ]]; then | |
| return | |
| fi | |
| _CF_LAST_CMD="$cmd" | |
| _CF_FORMATTED=0 | |
| # Ignore short or empty internal commands | |
| [[ -z "$cmd" ]] && return | |
| (( ${#cmd} <= CLI_FORMATTER_MAX_LEN )) && return | |
| local formatted | |
| formatted="$(cmd_format "$cmd")" | |
| # Display only if changed (contains newline → actually multi-line) | |
| if [[ "$formatted" == *$'\n'* ]]; then | |
| _CF_FORMATTED=1 | |
| echo -e "${_CF_DIM}┌─ Formatted command (>${CLI_FORMATTER_MAX_LEN} chars):${_CF_RESET}" | |
| echo -e "${_CF_CYAN}${formatted}${_CF_RESET}" | |
| echo -e "${_CF_DIM}└──────────────────────────────────────────────────${_CF_RESET}" | |
| fi | |
| } | |
| # Install the DEBUG trap | |
| trap '_cf_preexec' DEBUG | |
| # Ensure the trap propagates into subshells | |
| set -o functrace 2>/dev/null || true | |
| fi | |
| # ============================================================================= | |
| # ZSH; hook via preexec | |
| # ============================================================================= | |
| if [[ -n "$ZSH_VERSION" ]]; then | |
| autoload -Uz add-zsh-hook 2>/dev/null | |
| _cf_zsh_preexec() { | |
| local cmd="$1" | |
| [[ -z "$cmd" ]] && return | |
| (( ${#cmd} <= CLI_FORMATTER_MAX_LEN )) && return | |
| local formatted | |
| formatted="$(cmd_format "$cmd")" | |
| if [[ "$formatted" == *$'\n'* ]]; then | |
| echo -e "${_CF_DIM}┌─ Formatted command (>${CLI_FORMATTER_MAX_LEN} chars):${_CF_RESET}" | |
| echo -e "${_CF_CYAN}${formatted}${_CF_RESET}" | |
| echo -e "${_CF_DIM}└──────────────────────────────────────────────────${_CF_RESET}" | |
| fi | |
| } | |
| add-zsh-hook preexec _cf_zsh_preexec | |
| fi | |
| # ============================================================================= | |
| # SH / other shells; manual wrapper function: `rx <command>` | |
| # ============================================================================= | |
| rx() { | |
| local cmd="$*" | |
| if (( ${#cmd} > CLI_FORMATTER_MAX_LEN )); then | |
| local formatted | |
| formatted="$(cmd_format "$cmd")" | |
| if [[ "$formatted" == *$'\n'* ]]; then | |
| echo -e "${_CF_DIM}┌─ Formatted command:${_CF_RESET}" | |
| echo -e "${_CF_CYAN}${formatted}${_CF_RESET}" | |
| echo -e "${_CF_DIM}└────────────────────${_CF_RESET}" | |
| fi | |
| fi | |
| eval "$cmd" | |
| } | |
| # ============================================================================= | |
| # Load confirmation | |
| # ============================================================================= | |
| echo -e "${_CF_YELLOW}[cli_formatter]${_CF_RESET} active; limit ${_CF_CYAN}${CLI_FORMATTER_MAX_LEN}${_CF_RESET} chars, indent ${_CF_CYAN}${CLI_FORMATTER_INDENT}${_CF_RESET} spaces" | |
| echo -e " Configuration: ${_CF_DIM}CLI_FORMATTER_MAX_LEN=${CLI_FORMATTER_MAX_LEN} CLI_FORMATTER_INDENT=${CLI_FORMATTER_INDENT}${_CF_RESET}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment