Skip to content

Commit

Permalink
Merge pull request #28 from BartSte/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
BartSte authored Oct 9, 2024
2 parents c5efe1c + bd40fef commit 59abf04
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Maintainer: BartSte bartsteensma@outlook.com

pkgname=fzf-help
pkgver=2.1.2
pkgver=2.2.0
pkgrel=2
pkgdesc="Use fzf to select command line options from --help"
arch=('any')
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ The following environment variables can be set to configure the behaviour of
- `FZF_HELP_BAT_WARNING`: set this variable to `false` to disable the warning
that is displayed when `bat` is not installed. Defaults to `true`.

- `HELP_MESSAGE_CMD`: controls which command is used to retrieve the command
line options. Here, the `$cmd` variable is the command to get the options for.
Defaults to `$cmd --help`. You can use `man -P cat $cmd` if you want to use the
man page instead of the `--help` documentation.

- `HELP_MESSAGE_RC`: set this environment variable to a file you want to be
sourced before getting the help message. Typically, this file will contain
aliases and functions from which you may want to get the help message. When
this variable is set, alias expansion is also enabled.

- `CLI_OPTIONS_CMD`: set this environment variable to the command you want to
use to retrieve the command line options. When defining the command, ensure
that the output is in the form of: the line number on which the option was
Expand Down Expand Up @@ -213,10 +223,11 @@ The following environment variables can be set to configure the behaviour of
export CLI_OPTIONS_CMD='ag -o --numbers -- $RE'
```

- `HELP_MESSAGE_CMD`: controls which command is used to retrieve the command
line options. Here, the `$cmd` variable is the command to get the options for.
Defaults to `$cmd --help`. You can use `man -P cat $cmd` if you want to use the
man page instead of the `--help` documentation.
- `FZF_HELP_LOG`: the path to the log file. Defaults to
`~/.local/state/fzf-help.log`.

- `FZF_HELP_LOG_LINES`: the number of lines to keep in the log file. Defaults to
`10000`.

## Usage

Expand Down
48 changes: 48 additions & 0 deletions src/fzf-help-log
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

################################################################################
# Logger
#
# Logs messages to the file at `FZF_HELP_LOG_PATH`. If the variable is not set,
# the log file will be created at `~/.local/state/fzf-help.log`. The log file
# will be truncated to the last `FZF_HELP_LOG_LINES` lines (default 10000).
fzf_help_log() {
local message fzf_help_log_path fzf_help_log_lines

message=$1
fzf_help_log_path=${FZF_HELP_LOG_PATH:-~/.local/state/fzf-help.log}
fzf_help_log_lines=${FZF_HELP_LOG_LINES:-10000}

_fifo_log "$fzf_help_log_path" "$fzf_help_log_lines" "$message"
}

################################################################################
# _fifo_log
#
# Appends messages to the log file at `path`. If the file does not exist, it
# will be created. If the number of lines in the log file exceeds the value of
# `max_lines` then the log file will be truncated to the last `max_lines` lines.
################################################################################
_fifo_log() {
local path max_lines message

path=$1
max_lines=$2
message=$3

if [ ! -f "$path" ]; then
mkdir -p "$(dirname "$path")"
touch "$path"
fi

echo "$message" >> "$path"

if grep -E "^/dev/(null|stdout|stderr)$" <<< "$path" ; then
return
fi

if [ $(wc -l < "$path") -gt "$max_lines" ]; then
tail -n "$max_lines" "$path" > "$path".tmp
mv "$path".tmp "$path"
fi
}
6 changes: 6 additions & 0 deletions src/fzf-select-option
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail

_fzf_help_directory=$(dirname $(realpath "${BASH_SOURCE:-$0}"))
source "$_fzf_help_directory"/fzf-help-log

usage="Usage: $(basename "$0") [-h | --help] <cmd>
Displays the cli options for <cmd> using fzf, allowing the user to select one
Expand Down Expand Up @@ -87,6 +90,9 @@ fzf_debug() {
echo -e "$msg"
}

fzf_help_log "--- New run ---"
fzf_help_log "Time: $(date)"

cmd=""
fzf_cmd="fzf"
fzf_help_opts=""
Expand Down
26 changes: 16 additions & 10 deletions src/help-message
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail

_fzf_help_directory=$(dirname $(realpath "${BASH_SOURCE:-$0}"))
source "$_fzf_help_directory"/fzf-help-log

usage="Usage: $(basename "$0") [-h | --help] <cmd>
Evaluates the HELP_MESSAGE_CMD environment variable and writes the result to
Expand Down Expand Up @@ -42,19 +45,22 @@ parse_args() {
done
}

get_temp() {
local tmpdir file
tmpdir=$(dirname "$(mktemp -u)")
file="$tmpdir/fzf-help-message"
touch "$file"
echo "$file"
}

cmd=""
parse_args "$@"
help_message=$(get_temp)

help_message=$("$_fzf_help_directory"/make-temp "fzf-help-message")
help_message_cmd="${HELP_MESSAGE_CMD:-$cmd --help}"
help_message_rc="${HELP_MESSAGE_RC:-""}"
fzf_help_log "Help command is: $help_message_cmd"
fzf_help_log "Help message file is: $help_message"
fzf_help_log "Aliases: $help_message_rc"

if [ -f "$help_message_rc" ]; then
shopt -s expand_aliases
. "$help_message_rc"
fi


[[ $cmd ]] && { eval "$help_message_cmd" >"$help_message" || true; }

cat "$help_message"
cat "$help_message" > /dev/stdout
8 changes: 8 additions & 0 deletions src/make-temp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

name=$1
tmpdir=$(dirname "$(mktemp -u)")
file="$tmpdir/$name"
touch "$file"
echo "$file"
1 change: 1 addition & 0 deletions test/setup_suite.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ setup_suite() {
unset FZF_HELP_SYNTAX
unset CLI_OPTIONS_CMD
unset HELP_MESSAGE_CMD
unset HELP_MESSAGE_RC
}

0 comments on commit 59abf04

Please sign in to comment.