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

Fix shellcheck warnings #80

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
36 changes: 21 additions & 15 deletions jumpapp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash

show_usage() {
local cmd=$(basename "${BASH_SOURCE[0]}")
local cmd
cmd=$(basename "${BASH_SOURCE[0]}")
echo "Usage: $cmd [OPTION]... COMMAND [ARG]...

Jump to (focus) the first open window for an application, if it's running.
Expand Down Expand Up @@ -46,6 +47,7 @@ main() {
w) workspace_filter="$(get_active_workspace)"; force=1 ;;
R) keep_workspace=1 ;;
C) mouse_center=1 ;;
*) exit 255
esac
done
shift $(( OPTIND - 1 ))
Expand Down Expand Up @@ -74,7 +76,8 @@ check_for_prerequisites() {

jumpapp() {
if [[ -z "$cmdid" ]]; then
local cmdid=$(basename "$cmd")
local cmdid
cmdid=$(basename "$cmd")
fi

if [[ -z "$classid" ]]; then
Expand All @@ -93,7 +96,8 @@ jumpapp() {
&& [[ "$(get_active_windowid)" -eq "${windowids[0]}" ]]; then
minimize_active_window
elif (( ${#windowids[@]} )) && ! needs_passthrough "$@"; then
local window=$(get_subsequent_window "${windowids[@]}")
local window
window=$(get_subsequent_window "${windowids[@]}")
if [[ -n "$keep_workspace" ]]; then
keep_workspace_activate_window "$window" ||
die "Error: unable to focus window for '$cmdid'"
Expand Down Expand Up @@ -148,7 +152,8 @@ where_class_or_pid_matches() {
local target_class=$1
shift

local local_hostname=$(get_hostname)
local local_hostname
local_hostname=$(get_hostname)

local windowid hostname pid workspace class title
while read -r windowid hostname pid workspace class title; do
Expand Down Expand Up @@ -210,7 +215,8 @@ get_subsequent_window() {
}

_get_subsequent_window() {
local active_window=$(get_active_windowid)
local active_window
active_window=$(get_active_windowid)

if ! is_num_in_array "$active_window" "$@"; then
if [[ -n $in_reverse ]]; then
Expand All @@ -232,15 +238,15 @@ get_oldest_focused_window() {
local windows_in_stacking_order=($(list_stacking_order))

for window in ${windows_in_stacking_order[@]}; do
get_matching_window_from_list $window "$@"
get_matching_window_from_list "$window" "$@"
done
}

get_most_recently_focused_window() {
local windows_in_stacking_order=($(list_stacking_order | reverse_words))

for window in ${windows_in_stacking_order[@]}; do
get_matching_window_from_list $window "$@"
get_matching_window_from_list "$window" "$@"
done
}

Expand All @@ -250,7 +256,7 @@ get_matching_window_from_list() {

for window in ${window_list[@]}; do
if [[ $window_to_search_for -eq $window ]]; then
printf '%s\n' $window
printf '%s\n' "$window"
return
fi
done
Expand Down Expand Up @@ -375,24 +381,24 @@ list_windows() {
}

get_active_windowid() {
local name windowid
read name windowid < <(xprop -root ' $0\n' _NET_ACTIVE_WINDOW)
local _name windowid
read -r _name windowid < <(xprop -root ' $0\n' _NET_ACTIVE_WINDOW)
printf '%s\n' "$windowid"
}

list_stacking_order() {
local name ids
read -r name ids < <(xprop -root ' $0+\n' _NET_CLIENT_LIST_STACKING)
local _name ids
read -r _name ids < <(xprop -root ' $0+\n' _NET_CLIENT_LIST_STACKING)
if [[ "$ids" == 'not found.' ]] || [[ "$ids" =~ 'no such atom' ]]; then
# in case the window manager doesn't support _NET_CLIENT_LIST_STACKING (p.e. dwm)
read -r name ids < <(xprop -root ' $0+\n' _NET_CLIENT_LIST)
read -r _name ids < <(xprop -root ' $0+\n' _NET_CLIENT_LIST)
fi
printf '%s\n' "$ids" | tr ',' ' '
}

get_window_types() {
local name window_types
read -r name window_types < <(xprop -id "$1" ' $0+\n' _NET_WM_WINDOW_TYPE)
local _name window_types
read -r _name window_types < <(xprop -id "$1" ' $0+\n' _NET_WM_WINDOW_TYPE)
if [[ "$window_types" != 'not found.' ]]; then
printf '%s\n' "$window_types"
fi
Expand Down
30 changes: 17 additions & 13 deletions t/test_jumpapp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ setUp() {
minimize_active_window_called=
die_called=
exec_command_called=
focus_window_by_class_called=
fork_command_called=
}

it_echoes_help_when_run_with_0_args() {
local output=$(main)
local output
output=$(main)
[[ "$output" == Usage:* ]] || fail 'Output does not begin with "Usage:"'
}

it_echoes_help_when_run_with_dash_h() {
local output=$(main -h)
local output
output=$(main -h)
[[ "$output" == Usage:* ]] || fail 'Output does not begin with "Usage:"'
}

Expand All @@ -38,8 +39,8 @@ it_throws_an_error_when_wmctrl_is_not_installed() {
}

it_calls_fork_command_when_process_is_not_running() {
focus_window_by_class_val=1
local output_file=$(mktemp)
local output_file
output_file=$(mktemp)

main someapp >|"$output_file"

Expand All @@ -51,9 +52,9 @@ it_calls_fork_command_when_process_is_not_running() {
}

it_calls_exec_command_when_called_with_dash_n_and_process_is_not_running() {
focus_window_by_class_val=1
exec_command_cmd=
local output_file=$(mktemp)
local output_file
output_file=$(mktemp)

main -n someapp >|"$output_file"

Expand Down Expand Up @@ -370,7 +371,7 @@ it_calls_center_cursor_on_window_when_-C_is_passed() {
it_calls_list_pids_for_command_without_the_full_path() {
list_pids_for_command_cmd_file=$(mktemp)
main /usr/bin/someapp >/dev/null
assertEquals someapp "$(<$list_pids_for_command_cmd_file)"
assertEquals someapp "$(<"$list_pids_for_command_cmd_file")"
rm -f "$list_pids_for_command_cmd_file"
}

Expand All @@ -386,7 +387,8 @@ it_calls_fork_command_if_-p_option_passed_and_a_command_argument_is_passed() {
it_prints_count_0_when_called_with_-L() {
list_windows_output=''

local output=$(main -L someapp)
local output
output=$(main -L someapp)
[[ "$output" == "Matched Windows [0]" ]] || fail 'Output is not "Matched Windows [0]"'
}

Expand All @@ -395,7 +397,8 @@ it_prints_count_2_when_called_with_-L_and_2_match() {
567 somehost 234 -1 anotherapp AnotherApp Window
678 somehost 123 -1 someapp SomeApp Window #2'

local output=$(main -L someapp)
local output
output=$(main -L someapp)
[[ "$output" == "Matched Windows [2]"* ]] || fail 'Output does not start with "Matched Windows [2]"'
}

Expand All @@ -404,7 +407,8 @@ it_prints_matching_windows_when_called_with_-L() {
567 somehost 234 -1 anotherapp AnotherApp Window
678 somehost 123 -1 someapp SomeApp Window #2'

local output=$(main -L someapp)
local output
output=$(main -L someapp)
[[ "$output" == *"456 somehost 123 -1 someapp: SomeApp Window #1"* ]] || fail 'Output does not list Window #1'
[[ "$output" != *"anotherapp"* ]] || fail 'Output lists anotherapp'
[[ "$output" == *"678 somehost 123 -1 someapp: SomeApp Window #2"* ]] || fail 'Output does not list Window #2'
Expand Down Expand Up @@ -513,7 +517,7 @@ list_stacking_order() {
# Derived from Gary Bernhardt's screencast #68
# (https://www.destroyallsoftware.com/screencasts/catalog/test-driving-shell-scripts)
suite() {
tests=( $(grep ^it_ "$0" | cut -d '(' -f 1) )
mapfile tests < <(grep ^it_ "$0" | cut -d '(' -f 1)
for name in "${tests[@]}"; do
suite_addTest "$name"
done
Expand All @@ -522,6 +526,6 @@ suite() {
if hash shunit2 2>/dev/null; then
source shunit2
else
echo 'Error: shunit2(1) could not be located. Please install it on your $PATH.' >&2
echo "Error: shunit2(1) could not be located. Please install it on your \$PATH." >&2
exit 1
fi