|
25 | 25 | # Optionally requires i3-py [py2] (or i3ipc [py3]) to focus weechat in i3 |
26 | 26 | # |
27 | 27 | # History: |
| 28 | +# 2026-03-23, Ferus |
| 29 | +# version 0.2.2: add support for window focusing under sway |
28 | 30 | # 2021-03-19, Seirdy |
29 | 31 | # version 0.2.1: add support for fzf-tmux |
30 | 32 | # 2020-06-08, Ferus |
|
46 | 48 |
|
47 | 49 | SCRIPT_NAME = "buffer_dmenu" |
48 | 50 | SCRIPT_AUTHOR = "Ferus <ferus+weechat@airmail.cc>" |
49 | | -SCRIPT_VERSION = "0.2.1" |
| 51 | +SCRIPT_VERSION = "0.2.2" |
50 | 52 | SCRIPT_LICENSE = "GPL3" |
51 | 53 | SCRIPT_DESC = ( |
52 | 54 | "List buffers in dmenu (or rofi/fzf-tmux), changes active window to selected buffer" |
|
62 | 64 | print("This script must be run under WeeChat.") |
63 | 65 | exit(1) |
64 | 66 |
|
65 | | -try: |
66 | | - import i3ipc as i3 |
67 | | - |
68 | | - have_i3 = True |
69 | | -except ImportError as e: |
70 | | - have_i3 = False |
71 | 67 |
|
72 | 68 | settings = { |
73 | 69 | "launcher": ("dmenu", "launcher to use (supported: dmenu, rofi, fzf_tmux)"), |
74 | 70 | "focus": ( |
75 | 71 | "false", |
76 | 72 | "whether to immediately focus the terminal after selecting buffer", |
77 | 73 | ), |
78 | | - "focus.wm": ("i3", "wm focus logic to use (supported: i3)"), |
| 74 | + "focus.wm": ("i3", "wm focus logic to use (supported: i3, sway)"), |
79 | 75 | "dmenu.command": ("dmenu -b -i -l 20", "command used to call dmenu"), |
80 | 76 | "rofi.command": ( |
81 | 77 | "rofi -p '# ' -dmenu -lines 10 -columns 8 -auto-select -mesg '<big>Pick a <b>buffer</b> to jump to:</big>'", |
|
85 | 81 | "sed -e \"s/b'//\" -e s#\\\\n#\\n#g | fzf-tmux -w 40 -h 70%", |
86 | 82 | "command used to call fzf-tmux", |
87 | 83 | ), |
88 | | - "title.regex": ("WeeChat \d+\.\d+", "regex used to match weechat's title window"), |
| 84 | + "title.regex": (r"WeeChat \d+\.\d+", "regex used to match weechat's title window"), |
89 | 85 | } |
90 | 86 |
|
91 | 87 |
|
@@ -131,17 +127,36 @@ def launch(options): |
131 | 127 |
|
132 | 128 | def focus(): |
133 | 129 | if w.config_string_to_boolean(w.config_get_plugin("focus")): |
134 | | - if w.config_get_plugin("focus.wm") == "i3": |
| 130 | + wm = w.config_get_plugin("focus.wm") |
| 131 | + if wm == "i3": |
135 | 132 | focus_i3() |
| 133 | + elif wm == "sway": |
| 134 | + focus_sway() |
136 | 135 |
|
137 | 136 |
|
138 | 137 | def focus_i3(): |
139 | | - if have_i3: |
| 138 | + try: |
| 139 | + import i3ipc as i3 |
140 | 140 | regex = w.config_get_plugin("title.regex") |
141 | | - |
142 | 141 | i3conn = i3.Connection() |
143 | | - weechat = i3conn.get_tree().find_named(regex)[0] |
144 | | - weechat.command("focus") |
| 142 | + weechat_window = i3conn.get_tree().find_named(regex)[0] |
| 143 | + weechat_window.command("focus") |
| 144 | + except ImportError as e: |
| 145 | + # TODO: Print error to window |
| 146 | + w.prnt("", "{}{}: Focusing windows under i3 requires 'i3ipc' to be installed." |
| 147 | + .format(w.prefix("error"), SCRIPT_NAME)) |
| 148 | + |
| 149 | + |
| 150 | +def focus_sway(): |
| 151 | + try: |
| 152 | + import swayipc |
| 153 | + regex = r'{}'.format(w.config_get_plugin("title.regex")) |
| 154 | + result = swayipc.run_command('[title="{}"] focus'.format(regex)) |
| 155 | + if not result[0].success: |
| 156 | + w.prnt("", "{}Sway failed to focus window".format(w.prefix("error"))) |
| 157 | + except ImportError as e: |
| 158 | + w.prnt("", "{}{}: Focusing windows under sway requires 'swayipc' to be installed." |
| 159 | + .format(w.prefix("error"), SCRIPT_NAME)) |
145 | 160 |
|
146 | 161 |
|
147 | 162 | def call(command, options): |
|
0 commit comments