Skip to content

Commit 1609a89

Browse files
author
Ferus Castor
committed
buffer_dmenu.py 0.2.2: add support for focusing under sway
1 parent ec6c454 commit 1609a89

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

python/buffer_dmenu.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
# Optionally requires i3-py [py2] (or i3ipc [py3]) to focus weechat in i3
2626
#
2727
# History:
28+
# 2026-03-23, Ferus
29+
# version 0.2.2: add support for window focusing under sway
2830
# 2021-03-19, Seirdy
2931
# version 0.2.1: add support for fzf-tmux
3032
# 2020-06-08, Ferus
@@ -46,7 +48,7 @@
4648

4749
SCRIPT_NAME = "buffer_dmenu"
4850
SCRIPT_AUTHOR = "Ferus <ferus+weechat@airmail.cc>"
49-
SCRIPT_VERSION = "0.2.1"
51+
SCRIPT_VERSION = "0.2.2"
5052
SCRIPT_LICENSE = "GPL3"
5153
SCRIPT_DESC = (
5254
"List buffers in dmenu (or rofi/fzf-tmux), changes active window to selected buffer"
@@ -62,20 +64,14 @@
6264
print("This script must be run under WeeChat.")
6365
exit(1)
6466

65-
try:
66-
import i3ipc as i3
67-
68-
have_i3 = True
69-
except ImportError as e:
70-
have_i3 = False
7167

7268
settings = {
7369
"launcher": ("dmenu", "launcher to use (supported: dmenu, rofi, fzf_tmux)"),
7470
"focus": (
7571
"false",
7672
"whether to immediately focus the terminal after selecting buffer",
7773
),
78-
"focus.wm": ("i3", "wm focus logic to use (supported: i3)"),
74+
"focus.wm": ("i3", "wm focus logic to use (supported: i3, sway)"),
7975
"dmenu.command": ("dmenu -b -i -l 20", "command used to call dmenu"),
8076
"rofi.command": (
8177
"rofi -p '# ' -dmenu -lines 10 -columns 8 -auto-select -mesg '<big>Pick a <b>buffer</b> to jump to:</big>'",
@@ -85,7 +81,7 @@
8581
"sed -e \"s/b'//\" -e s#\\\\n#\\n#g | fzf-tmux -w 40 -h 70%",
8682
"command used to call fzf-tmux",
8783
),
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"),
8985
}
9086

9187

@@ -131,17 +127,36 @@ def launch(options):
131127

132128
def focus():
133129
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":
135132
focus_i3()
133+
elif wm == "sway":
134+
focus_sway()
136135

137136

138137
def focus_i3():
139-
if have_i3:
138+
try:
139+
import i3ipc as i3
140140
regex = w.config_get_plugin("title.regex")
141-
142141
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))
145160

146161

147162
def call(command, options):

0 commit comments

Comments
 (0)