Last active
December 21, 2015 00:39
-
-
Save vdcrim/6222094 to your computer and use it in GitHub Desktop.
Python script for HexChat 2.9.6+ that sets the program's title to the last channel with activity and its unread count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Show last channel with activity and its unread count as the program's title | |
- Requires HexChat 2.9.6+ | |
- Only thought for single window | |
Copyright (C) 2013, 2014 Diego Fernández Gosende (dfgosende [at] gmail [dot] com) | |
GPL v2 or later <http://www.gnu.org/licenses/gpl-2.0.html> | |
""" | |
# activity in the following channels is ignored | |
excluded_channels = '#news', | |
__module_name__ = 'Unread count' | |
__module_version__ = '0.1' | |
__module_description__ = ('Show last channel with activity and its unread ' | |
"count as the program's title") | |
_min_version = '2.9.6' | |
import sys | |
import os | |
import os.path | |
import ctypes | |
import hexchat | |
def main(): | |
# get the main window and load gtk | |
win = int(hexchat.get_info('gtkwin_ptr'), 16) | |
if os.name == 'nt': | |
lib = 'gtk-win32-2.0.dll' | |
lib2 = os.path.join(os.path.dirname(sys.executable), lib) | |
if os.path.isfile(lib2): | |
lib = lib2 | |
else: | |
lib = 'libgtk-x11-2.0.so' | |
gtk = ctypes.CDLL(lib) | |
gtk.gtk_window_set_title.argtypes = [ctypes.c_int, ctypes.c_char_p] | |
# window title | |
class Title(object): | |
def __init__(self): | |
self._name = 'HexChat' | |
self.unread_count = 0 | |
@property | |
def name(self): | |
return self._name | |
@name.setter | |
def name(self, new_name): | |
self._name = new_name or 'HexChat' | |
self.unread_count = 0 | |
@name.deleter | |
def name(self): | |
del self._name | |
def activity(self, name): | |
if name != self.name: | |
self.name = name | |
self.unread_count += 1 | |
self.set() | |
def set(self): | |
gtk.gtk_window_set_title(win, str(self).encode('utf-8')) | |
def __repr__(self): | |
if not self.unread_count: | |
return self.name | |
return u'[{}] {}'.format(self.unread_count, self.name) | |
# event callbacks | |
def on_server_connected(word, word_eol, title): | |
title.name = hexchat.get_info('network') | |
title.set() | |
def on_focus(word, word_eol, title): | |
title.name = hexchat.get_info('channel') | |
title.set() | |
def on_activity(word, word_eol, update): | |
if is_tab_active(): | |
title.set() | |
else: | |
channel = hexchat.get_info('channel') | |
if update and channel.lower() not in excluded_channels: | |
title.activity(channel) | |
def on_activity_delay(word, word_eol, title): | |
hexchat.hook_timer(100, lambda x : title.set()) | |
def on_unload(userdata): | |
hexchat.prnt(__module_name__ + ' script unloaded') | |
def is_tab_active(): | |
return hexchat.find_context() == hexchat.get_context() and \ | |
hexchat.get_info('win_status') == 'active' | |
# bind events | |
title = Title() | |
# reset count | |
hexchat.hook_print('Connected', on_server_connected, title) | |
for event in ('Focus Window', 'Focus Tab'): | |
hexchat.hook_print(event, on_focus, title) | |
# increment count | |
for event in ('Channel Message', 'Channel Msg Hilight', | |
'Channel Action', 'Channel Action Hilight', | |
'Private Message', 'Private Message to Dialog', | |
'Private Action', 'Private Action to Dialog'): | |
hexchat.hook_print(event, on_activity, userdata=True) | |
# any text event refreshes the title if its context is active | |
for event in ('Generic Message', 'You Join', 'Join', 'Part', | |
'Part with Reason', 'Quit', 'Kick', 'Channel Ban', | |
'Channel UnBan', 'Topic Change'): | |
hexchat.hook_print(event, on_activity, userdata=False) | |
# for the following ones even if it's not, and a delay is also needed | |
for event in ('Change Nick', 'Channel Operator'): | |
hexchat.hook_print(event, on_activity_delay, title) | |
hexchat.hook_unload(on_unload) | |
return True | |
if [int(i) for i in hexchat.get_info('version').split('.')] < \ | |
[int(i) for i in _min_version.split('.')]: | |
hexchat.prnt(u'{}: HexChat {}+ is needed'.format( | |
__module_name__, _min_version)) | |
elif main(): | |
hexchat.prnt(__module_name__ + ' script loaded') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment