diff --git a/python/buffer_dmenu.py b/python/buffer_dmenu.py index 16893d84..56989de5 100644 --- a/python/buffer_dmenu.py +++ b/python/buffer_dmenu.py @@ -25,6 +25,8 @@ # Optionally requires i3-py [py2] (or i3ipc [py3]) to focus weechat in i3 # # History: +# 2026-03-23, Ferus +# version 0.2.2: add support for window focusing under sway # 2021-03-19, Seirdy # version 0.2.1: add support for fzf-tmux # 2020-06-08, Ferus @@ -46,7 +48,7 @@ SCRIPT_NAME = "buffer_dmenu" SCRIPT_AUTHOR = "Ferus " -SCRIPT_VERSION = "0.2.1" +SCRIPT_VERSION = "0.2.2" SCRIPT_LICENSE = "GPL3" SCRIPT_DESC = ( "List buffers in dmenu (or rofi/fzf-tmux), changes active window to selected buffer" @@ -62,12 +64,6 @@ print("This script must be run under WeeChat.") exit(1) -try: - import i3ipc as i3 - - have_i3 = True -except ImportError as e: - have_i3 = False settings = { "launcher": ("dmenu", "launcher to use (supported: dmenu, rofi, fzf_tmux)"), @@ -75,7 +71,7 @@ "false", "whether to immediately focus the terminal after selecting buffer", ), - "focus.wm": ("i3", "wm focus logic to use (supported: i3)"), + "focus.wm": ("i3", "wm focus logic to use (supported: i3, sway)"), "dmenu.command": ("dmenu -b -i -l 20", "command used to call dmenu"), "rofi.command": ( "rofi -p '# ' -dmenu -lines 10 -columns 8 -auto-select -mesg 'Pick a buffer to jump to:'", @@ -85,7 +81,7 @@ "sed -e \"s/b'//\" -e s#\\\\n#\\n#g | fzf-tmux -w 40 -h 70%", "command used to call fzf-tmux", ), - "title.regex": ("WeeChat \d+\.\d+", "regex used to match weechat's title window"), + "title.regex": (r"WeeChat \d+\.\d+", "regex used to match weechat's title window"), } @@ -131,17 +127,36 @@ def launch(options): def focus(): if w.config_string_to_boolean(w.config_get_plugin("focus")): - if w.config_get_plugin("focus.wm") == "i3": + wm = w.config_get_plugin("focus.wm") + if wm == "i3": focus_i3() + elif wm == "sway": + focus_sway() def focus_i3(): - if have_i3: + try: + import i3ipc as i3 regex = w.config_get_plugin("title.regex") - i3conn = i3.Connection() - weechat = i3conn.get_tree().find_named(regex)[0] - weechat.command("focus") + weechat_window = i3conn.get_tree().find_named(regex)[0] + weechat_window.command("focus") + except ImportError as e: + # TODO: Print error to window + w.prnt("", "{}{}: Focusing windows under i3 requires 'i3ipc' to be installed." + .format(w.prefix("error"), SCRIPT_NAME)) + + +def focus_sway(): + try: + import swayipc + regex = r'{}'.format(w.config_get_plugin("title.regex")) + result = swayipc.run_command('[title="{}"] focus'.format(regex)) + if not result[0].success: + w.prnt("", "{}Sway failed to focus window".format(w.prefix("error"))) + except ImportError as e: + w.prnt("", "{}{}: Focusing windows under sway requires 'swayipc' to be installed." + .format(w.prefix("error"), SCRIPT_NAME)) def call(command, options): diff --git a/python/weestreamer.py b/python/weestreamer.py index aa14860d..5ab32166 100644 --- a/python/weestreamer.py +++ b/python/weestreamer.py @@ -14,9 +14,20 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +# History: +# 2026-03-04, Ferus (feruscastor@proton.me) +# 0.5.0: Update syntax from py2 to py3 +# Make player configurable +# Add toggle for displaying cli output in buffer + +settings = { + "player": ("streamlink -p /usr/bin/mpv", "Full command for player"), + "output": ("true", "Display player output in buffer"), +} + import weechat -weechat.register("weestreamer", "Miblo", "0.4.2", "GPL3", "Streamlink companion for WeeChat", "", "") +weechat.register("weestreamer", "Miblo", "0.5.0", "GPL3", "Streamlink companion for WeeChat", "", "") def stream(data, buffer, args): bufserver = weechat.buffer_get_string(weechat.current_buffer(), "localvar_server") @@ -34,32 +45,40 @@ def stream(data, buffer, args): server = input[0] channel = input[1] else: - weechat.prnt(weechat.current_buffer(), "%sToo many arguments (%s). Please see /help weestreamer" - % (weechat.prefix("error"),(len(input)))) + weechat.prnt(weechat.current_buffer(), "{}Too many arguments ({!s}). Please see /help weestreamer" + .format(weechat.prefix("error"), len(input))) return weechat.WEECHAT_RC_ERROR # NOTE(matt): https://streamlink.github.io/plugin_matrix.html - servers = { "afreeca":"http://play.afreeca.com/%s" % (channel), - "hitbox":"http://www.hitbox.tv/%s" % (channel), - "twitch":"http://www.twitch.tv/%s" % (channel), - "ustream":"http://www.ustream.tv/%s" % (channel.replace("-", ""))} + servers = {"afreeca":"https://play.afreeca.com/{channel}" + ,"hitbox":"https://www.hitbox.tv/{channel}" + ,"twitch":"https://www.twitch.tv/{channel}" + ,"ustream":"https://www.ustream.tv/{channel}" + } streamurl = "" for key in servers.keys(): if key in server: streamurl = servers[key] if not streamurl: - weechat.prnt(weechat.current_buffer(), "%sUnsupported server: %s" - % (weechat.prefix("error"), server)) + weechat.prnt(weechat.current_buffer(), "{}Unsupported server: {}" + .format(weechat.prefix("error"), server)) weechat.prnt(weechat.current_buffer(), "Currently supported servers:") for key in sorted(servers.keys()): - weechat.prnt(weechat.current_buffer(), " %s" % key) + weechat.prnt(weechat.current_buffer(), " {}".format(key)) return weechat.WEECHAT_RC_ERROR - command = "streamlink %s %s" % (streamurl, quality) + streamurl = streamurl.format(channel=channel) + + if server == "ustream": + streamurl = streamurl.replace("-", "") - weechat.prnt(weechat.current_buffer(), "%sLAUNCHING: %s" % (weechat.prefix("action"), command)) - weechat.hook_process("%s" % (command), 0, "handle_output", "") + comm = weechat.config_get_plugin("player") + command = "{} {} {}".format(comm, streamurl, quality) + + weechat.prnt(weechat.current_buffer(), "{}LAUNCHING: {}" + .format(weechat.prefix("action"), command)) + weechat.hook_process(command, 0, "handle_output", "") return weechat.WEECHAT_RC_OK def handle_output(data, command, rc, out, err): @@ -67,10 +86,19 @@ def handle_output(data, command, rc, out, err): process_output = "" if out != "": process_output += out - if int(rc) >= 0: + if int(rc) >= 0 and weechat.config_string_to_boolean(weechat.config_get_plugin("output")): weechat.prnt(weechat.current_buffer(), process_output) return weechat.WEECHAT_RC_OK + +for option, value in settings.items(): + if not weechat.config_is_set_plugin(option): + weechat.config_set_plugin(option, value[0]) + if int(weechat.info_get("version_number", "")) >= 0x00030500: + weechat.config_set_desc_plugin( + option, '{} (default: "{}")'.format(value[1], value[0]) + ) + weechat.hook_command("weestreamer", "Streamlink companion for WeeChat", "server channel",