Skip to content

Commit 6e8272a

Browse files
metcalfcBruno Sutic
authored andcommitted
Addresses issue #61
awk counts bytes not characters. Use gawk if available to fix multibyte issues. Signed-off-by: Chad Metcalf <[email protected]>
1 parent bcc9a69 commit 6e8272a

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
### master
4+
- if installed use `gawk` instead of `awk` (@metcalfc)
45

56
### v2.1.0, Jan 01, 2015
67
- combine send-keys calls to reduce flickering (@toupeira)

scripts/copycat_jump.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TMUX_COPY_MODE="$(tmux_copy_mode)"
1515

1616
_file_number_of_lines() {
1717
local file="$1"
18-
echo "$(wc -l $file | awk '{print $1}')"
18+
echo "$(wc -l $file | $AWK_CMD '{print $1}')"
1919
}
2020

2121
_get_result_line() {
@@ -91,7 +91,7 @@ _get_match_line_position() {
9191
result_line="$(_escape_backslash "$result_line")"
9292
match="$(_escape_backslash "$match")"
9393

94-
local index=$(awk -v a="$result_line" -v b="$match" 'BEGIN{print index(a,b)}')
94+
local index=$($AWK_CMD -v a="$result_line" -v b="$match" 'BEGIN{print index(a,b)}')
9595
local zero_index=$((index - 1))
9696
echo "$zero_index"
9797
}
@@ -272,7 +272,7 @@ get_new_position_number() {
272272

273273
# doing a forward/up jump
274274
if [ "$NEXT_PREV" == "next" ]; then
275-
local number_of_results=$(wc -l "$copycat_file" | awk '{ print $1 }')
275+
local number_of_results=$(wc -l "$copycat_file" | $AWK_CMD '{ print $1 }')
276276
if [ "$current_position" -eq "$number_of_results" ]; then
277277
# position can't go beyond the last result
278278
new_position="$current_position"

scripts/helpers.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ tmux_option_prev="@copycat_prev"
99
# keeps track of number of panes in copycat mode
1010
tmux_option_counter="@copycat_counter"
1111

12+
# === awk vs gawk ===
13+
command_exists() {
14+
command -v "$@" > /dev/null 2>&1
15+
}
16+
AWK_CMD='awk'
17+
if command_exists gawk; then
18+
AWK_CMD='gawk'
19+
fi
20+
1221
# === general helpers ===
1322

1423
get_tmux_option() {
@@ -145,7 +154,7 @@ copycat_quit_copy_mode_keys() {
145154
local copy_mode="$(tmux_copy_mode)-copy"
146155
tmux list-keys -t "$copy_mode" |
147156
\grep "$commands_that_quit_copy_mode" |
148-
awk '{ print $4}' |
157+
$AWK_CMD '{ print $4}' |
149158
sort -u |
150159
xargs echo
151160
}

0 commit comments

Comments
 (0)