weechat-old/python/autoload/brows.py @ 8e22b36e5cba

More
author Steve Losh <steve@stevelosh.com>
date Wed, 17 Jul 2024 15:51:26 -0400
parents a9f1df02501f
children (none)
import subprocess
import os

SCRIPT_NAME = 'brows'
SCRIPT_AUTHOR = 'Steve Losh <steve@stevelosh.com>'
SCRIPT_VERSION = '1.0'
SCRIPT_LICENSE = 'MIT/X11'
SCRIPT_DESC = 'Launch brows to view URLs'
SCRIPT_COMMAND = 'brows'

import_ok = True

BROWS = os.environ.get('BROWS', 'brows')

try:
    import weechat
except ImportError:
    print('This is a weechat script, what are you doing, run it in weechat, jesus')
    import_ok = False

weechat_version = 0

def hd(fn, name, obj, attr):
    return fn(weechat.hdata_get(name), obj, attr)

def hdp(name, obj, attr):
    return hd(weechat.hdata_pointer, name, obj, attr)

def hds(name, obj, attr):
    return hd(weechat.hdata_string, name, obj, attr)

def get_lines(buffer, numlines):
    lines = hdp("buffer", buffer, "own_lines")
    if not lines:
        # null pointer wat do
        return None

    last_lines = []

    line = hdp("lines", lines, "last_line")
    for _ in range(numlines):
        if not line:
            # shit we're at the top of the buffer
            break

        data = hdp("line", line, "data")
        msg = hds("line_data", data, "message")
        msg = weechat.string_remove_color(msg, "")

        last_lines.append(msg.strip())

        line = hdp("line", line, "prev_line")

    return last_lines

def brows(data, buffer, args, numlines=100):
    lines = get_lines(buffer, numlines)

    proc = subprocess.Popen([BROWS], stdin=subprocess.PIPE)
    proc.communicate(input='\n'.join(lines))
    weechat.command("", "/window refresh")

    return weechat.WEECHAT_RC_OK


if __name__ == '__main__' and import_ok:
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
                        SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
        weechat_version = weechat.info_get('version_number', '') or 0
        weechat.hook_command(
            SCRIPT_COMMAND,
            'Launch brows to view URLs',
            '',
            '',
            '',
            'brows',
            '')