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

Allow using an arbitrary program instead of urlview, with command-line args #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ You should now be able to use the plugin.

Put `set -g @urlview-key 'x'` in `tmux.conf`.

> How can I use the default program FOO to get URLs explicitly?

Put `set -g @urlview-extractor 'FOO'` in `tmux.conf`. The default is
`urlview` if available, or `extract_url`.

> But I want to run FOO with command-line arguments '-a bar --b baz'!

Fine. Use `set -g @urlview-extractor 'FOO -a bar --b baz'`.

### Other goodies

`tmux-urlview` works great with:
Expand Down
28 changes: 21 additions & 7 deletions urlview.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,43 @@ get_tmux_option() {
local option=$1
local default_value=$2
local option_value=$(tmux show-option -gqv "$option")
if [ -z $option_value ]; then
echo $default_value
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo $option_value
echo "$option_value"
fi
}

find_executable() {
local extractor="$(get_tmux_option "@urlview-extractor")"
if type "${extractor%% *}" >/dev/null 2>&1; then
# FIXME: The %% idiom should work in sh, but it wasn't for me, so I
# reverted the interpreter to bash
echo "$extractor"
return
fi
tmux display-message "tmux-urlview: #{@urlview-extractor} not found"
if type urlview >/dev/null 2>&1; then
echo "urlview"
elif type extract_url >/dev/null 2>&1; then
return
fi
tmux display-message "tmux-urlview: urlview not found"
if type extract_url >/dev/null 2>&1; then
echo "extract_url"
return
fi
tmux display-message "tmux-urlview: extract_url not found"
}

readonly key="$(get_tmux_option "@urlview-key" "u")"
readonly cmd="$(find_executable)"
readonly temp_file="$(mktemp --tmpdir tmux-urlview.XXX)"

if [ -z "$cmd" ]; then
tmux display-message "Failed to load tmux-urlview: neither urlview nor extract_url were found on the PATH"
tmux display-message "Failed to load tmux-urlview: No extractor programs found"
else
tmux bind-key "$key" capture-pane -J \\\; \
save-buffer "${TMPDIR:-/tmp}/tmux-buffer" \\\; \
save-buffer "$temp_file" \\\; \
delete-buffer \\\; \
split-window -l 10 "$cmd '${TMPDIR:-/tmp}/tmux-buffer'"
split-window -l 10 "$cmd '$temp_file'; rm '$temp_file' "
fi