Skip to content

Commit cdf5d30

Browse files
author
Bruno Sutic
committed
First commit
0 parents  commit cdf5d30

5 files changed

Lines changed: 119 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Changelog
2+
3+
### master
4+
- got the results file

LICENSE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2014 Bruno Sutic
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the "Software"),
5+
to deal in the Software without restriction, including without limitation
6+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
and/or sell copies of the Software, and to permit persons to whom the
8+
Software is furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included
11+
in all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tmux copycat
2+
3+
Plugin for "stuff"
4+
5+
### License
6+
7+
[MIT](LICENSE.md)

copycat.tmux

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
5+
default_key_bindings='*'
6+
tmux_option="@copycat_key"
7+
8+
get_tmux_option() {
9+
local option=$1
10+
local default_value=$2
11+
local option_value=$(tmux show-option -gqv "$option")
12+
if [ -z "$option_value" ]; then
13+
echo "$default_value"
14+
else
15+
echo "$option_value"
16+
fi
17+
}
18+
19+
# Multiple bindings can be set.
20+
set_bindings() {
21+
local key_bindings="$(get_tmux_option "$tmux_option" "$default_key_bindings")"
22+
for key in "$key_bindings"; do
23+
tmux bind-key "$key" run-shell "$CURRENT_DIR/scripts/tmux_copycat.sh"
24+
done
25+
}
26+
27+
main() {
28+
set_bindings
29+
}
30+
main
31+

scripts/tmux_copycat.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
3+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
5+
get_tmp_dir() {
6+
if [ -n "$TMPDIR" ]; then
7+
echo "$TMPDIR"
8+
else
9+
echo "/tmp/"
10+
fi
11+
}
12+
13+
# returns a string unique to current pane
14+
pane_unique_id() {
15+
tmux display-message -p "#{session_name}_#{window_index}_#{pane_index}"
16+
}
17+
18+
get_scrollback_filename() {
19+
echo "$(get_tmp_dir)tmux_scrollback_$(pane_unique_id)"
20+
}
21+
22+
# simplest solution, taken from here: http://unix.stackexchange.com/a/81689
23+
remove_empty_lines_from_end_of_file() {
24+
local file=$1
25+
local temp=$(cat $file)
26+
printf '%s\n' "$temp" > "$file"
27+
}
28+
29+
capture_pane() {
30+
local file=$1
31+
# copying 9M lines back will hopefully fetch the whole scrollback
32+
tmux capture-pane -S -9000000
33+
tmux save-buffer "$file"
34+
tmux delete-buffer
35+
remove_empty_lines_from_end_of_file "$file"
36+
}
37+
38+
# doing 2 things in 1 step so that we don't write to disk too much
39+
reverse_and_create_results_file() {
40+
local file=$1
41+
local target_file=$2
42+
local grep_pattern=$3
43+
# The below line had to be eval-ed, otherwise it doesn't work
44+
eval "tail -r "$file" | grep -oni "$grep_pattern" > "$target_file""
45+
}
46+
47+
generate_results() {
48+
local grep_pattern=$1
49+
local scrollback_filename=$(get_scrollback_filename)
50+
capture_pane "$scrollback_filename"
51+
reverse_and_create_results_file "$scrollback_filename" "${scrollback_filename}_result" "$grep_pattern"
52+
}
53+
54+
main() {
55+
url_pattern="'https\?://[^ ]*'"
56+
generate_results "$url_pattern"
57+
}
58+
main

0 commit comments

Comments
 (0)