-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzf.py
74 lines (57 loc) · 2.58 KB
/
fzf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import Iterator, Tuple
import weechat
SCRIPT_NAME = "fzf"
SCRIPT_AUTHOR = "Trygve Aaberge <[email protected]>"
SCRIPT_VERSION = "0.1.0"
SCRIPT_LICENSE = "MIT"
SCRIPT_DESC = "Switch buffer using fzf (currently only works inside tmux)"
REPO_URL = "https://github.com/trygveaa/weechat-fzf"
def print_error(message: str) -> None:
weechat.prnt("", weechat.prefix("error") + message)
def fzf_process_cb(
data: str, command: str, return_code: int, out: str, err: str
) -> int:
if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR or return_code == 2 or err:
print_error("Error running fzf (code {}): {}".format(return_code, err))
return weechat.WEECHAT_RC_OK
if out != "":
pointer, _ = out.split("\t", 1)
weechat.buffer_set(pointer, "display", "1")
return weechat.WEECHAT_RC_OK
def fzf_command_cb(data: str, buffer: str, args: str) -> int:
cmd = (
"fzf-tmux -- --delimiter='\t' --with-nth=3.. "
"--preview='tail -$LINES {2} 2>/dev/null'"
)
hook = weechat.hook_process_hashtable(cmd, {"stdin": "1"}, 0, "fzf_process_cb", "")
for buffer_info in buffers():
weechat.hook_set(hook, "stdin", "\t".join(buffer_info) + "\n")
weechat.hook_set(hook, "stdin_close", "")
return weechat.WEECHAT_RC_OK
def buffers() -> Iterator[Tuple[str, str, str, str]]:
logger_filenames = {}
logger_infolist = weechat.infolist_get("logger_buffer", "", "")
while weechat.infolist_next(logger_infolist):
buffer = weechat.infolist_pointer(logger_infolist, "buffer")
filename = weechat.infolist_string(logger_infolist, "log_filename")
logger_filenames[buffer] = filename
weechat.infolist_free(logger_infolist)
buffer_infolist = weechat.infolist_get("buffer", "", "")
while weechat.infolist_next(buffer_infolist):
pointer = weechat.infolist_pointer(buffer_infolist, "pointer")
number = weechat.infolist_integer(buffer_infolist, "number")
name = weechat.infolist_string(buffer_infolist, "name")
yield (pointer, logger_filenames.get(pointer, ""), str(number), name)
weechat.infolist_free(buffer_infolist)
def main() -> None:
if not weechat.register(
SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""
):
return
tmux = weechat.string_eval_expression("${env:TMUX}", {}, {}, {})
if not tmux:
print_error("Error: fzf.py currently only supports being run inside tmux")
return
weechat.hook_command(SCRIPT_NAME, SCRIPT_DESC, "", "", "", "fzf_command_cb", "")
if __name__ == "__main__":
main()