2018-04-20 14:06:56 +00:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
2019-06-17 12:47:56 +00:00
|
|
|
# Copyright © 2018 Maxime “pep” Buquet
|
|
|
|
# Copyright © 2019 Madhur Garg
|
2018-04-20 14:06:56 +00:00
|
|
|
#
|
2020-06-06 19:54:12 +00:00
|
|
|
# Distributed under terms of the GPL-3.0+ license. See the COPYING file.
|
2018-04-20 14:06:56 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Search provided string in the buffer and return all results on the screen
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
2019-10-10 11:45:09 +00:00
|
|
|
from typing import Optional
|
|
|
|
from datetime import datetime
|
|
|
|
|
2018-04-20 14:06:56 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
2019-06-12 14:19:00 +00:00
|
|
|
from poezio import tabs
|
2019-09-29 16:12:48 +00:00
|
|
|
from poezio.text_buffer import TextBuffer
|
2020-08-09 18:09:50 +00:00
|
|
|
from poezio.ui.types import Message as PMessage, InfoMessage
|
2018-04-20 14:06:56 +00:00
|
|
|
|
|
|
|
|
2019-10-10 11:45:09 +00:00
|
|
|
def add_line(
|
|
|
|
text_buffer: TextBuffer,
|
|
|
|
text: str,
|
|
|
|
datetime: Optional[datetime] = None,
|
|
|
|
) -> None:
|
2018-04-20 14:06:56 +00:00
|
|
|
"""Adds a textual entry in the TextBuffer"""
|
2019-09-29 16:12:48 +00:00
|
|
|
text_buffer.add_message(InfoMessage(text, time=datetime))
|
2018-04-20 14:06:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
"""Lastlog Plugin"""
|
|
|
|
|
|
|
|
def init(self):
|
2019-07-19 16:55:16 +00:00
|
|
|
for tab in tabs.DynamicConversationTab, tabs.StaticConversationTab, tabs.PrivateTab, tabs.MucTab:
|
2019-06-12 14:19:00 +00:00
|
|
|
self.api.add_tab_command(
|
|
|
|
tab,
|
|
|
|
'lastlog',
|
|
|
|
self.command_lastlog,
|
|
|
|
usage='<keyword>',
|
|
|
|
help='Search <keyword> in the buffer and returns results'
|
|
|
|
'on the screen')
|
2018-04-20 14:06:56 +00:00
|
|
|
|
|
|
|
def command_lastlog(self, input_):
|
|
|
|
"""Define lastlog command"""
|
|
|
|
|
|
|
|
text_buffer = self.api.current_tab()._text_buffer
|
2019-06-12 14:19:00 +00:00
|
|
|
search_re = re.compile(input_, re.I)
|
2018-04-20 14:06:56 +00:00
|
|
|
|
|
|
|
res = []
|
2019-06-12 14:19:00 +00:00
|
|
|
add_line(text_buffer, "Lastlog:")
|
2018-04-20 14:06:56 +00:00
|
|
|
for message in text_buffer.messages:
|
2020-08-09 18:09:50 +00:00
|
|
|
if isinstance(message, PMessage) and \
|
2018-04-20 14:06:56 +00:00
|
|
|
search_re.search(message.txt) is not None:
|
|
|
|
res.append(message)
|
2019-10-10 11:45:09 +00:00
|
|
|
add_line(text_buffer, "%s> %s" % (message.nickname, message.txt), message.time)
|
2019-06-12 14:19:00 +00:00
|
|
|
add_line(text_buffer, "End of Lastlog")
|
|
|
|
self.api.current_tab().text_win.pos = 0
|
|
|
|
self.api.current_tab().core.refresh_window()
|