weechat/python/autoload/brows.py @ 905a5f33e231

Merge.
author Steve Losh <steve@stevelosh.com>
date Sun, 23 Feb 2020 12:35:20 -0500
parents b83a98ba30ee
children 7023c500dd7a
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',
            '')