comparison terminal-title.py @ 0:c54a46719c46

Initial revision
author Guido Berhoerster <guido+weechat@berhoerster.name>
date Tue, 10 Mar 2015 22:40:28 +0100
parents
children 98c1a683d9c1
comparison
equal deleted inserted replaced
-1:000000000000 0:c54a46719c46
1 #
2 # Copyright (C) 2010 by Guido Berhoerster <guido+weechat@berhoerster.name>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #
16
17 import os
18 import sys
19 import re
20 import string
21 from collections import Mapping
22 import weechat
23
24 SCRIPT_NAME = 'terminal-title'
25 VERSION = '1'
26 AUTHOR = 'Guido Berhoerster'
27 DESCRIPTION = 'Displays user defined information in the terminal title'
28 DEFAULT_SETTINGS = {
29 'title': ('WeeChat %version [%buffer_count] %buffer_number: '
30 '%buffer_name{%buffer_nicklist_count} [%hotlist]',
31 'items displayed in the terminal title')
32 }
33 TERM_TEMPLATES = [
34 ('xterm', "\033]0;%s\007"),
35 ('screen', "\033_%s\033\\")
36 ]
37 TERM_TEMPLATE = None
38
39
40 class TermTitleMapping(Mapping):
41 substitutions = {
42 'buffer_title':
43 lambda : weechat.buffer_get_string(weechat.current_buffer(),
44 "title"),
45 'buffer_name':
46 lambda : weechat.buffer_get_string(weechat.current_buffer(),
47 "name"),
48 'buffer_plugin':
49 lambda : weechat.buffer_get_string(weechat.current_buffer(),
50 "plugin"),
51 'buffer_number':
52 lambda : weechat.buffer_get_integer(weechat.current_buffer(),
53 "number"),
54 'buffer_nicklist_count':
55 lambda : weechat.buffer_get_integer(weechat.current_buffer(),
56 "nicklist_visible_count"),
57 'buffer_count': lambda : buffer_count(),
58 'hotlist': lambda : hotlist(),
59 'version': lambda : weechat.info_get("version", "")
60 }
61
62 def __getitem__(self, key):
63 return self.substitutions[key]()
64
65 def __iter__(self):
66 return self.substitutions.iterkeys()
67
68 def __len__(self):
69 return len(self.substitutions)
70
71
72 class TermTitleTemplate(string.Template):
73 delimiter = '%'
74
75
76 def buffer_count():
77 buffer_count = 0
78 buffer = weechat.infolist_get("buffer", "", "")
79 while weechat.infolist_next(buffer):
80 buffer_count += 1
81 weechat.infolist_free(buffer)
82 return buffer_count
83
84 def hotlist():
85 hotlist_items = []
86 hotlist = weechat.infolist_get("hotlist", "", "")
87 while weechat.infolist_next(hotlist):
88 buffer_number = weechat.infolist_integer(hotlist, "buffer_number")
89 buffer = weechat.infolist_pointer(hotlist, "buffer_pointer")
90 short_name = weechat.buffer_get_string(buffer, "short_name")
91 hotlist_items.append("%s:%s" % (buffer_number, short_name))
92 weechat.infolist_free(hotlist)
93 return ",".join(hotlist_items)
94
95 def set_term_title_hook(data, signal, signal_data):
96 title_template_str = weechat.config_get_plugin("title")
97 if not title_template_str:
98 return weechat.WEECHAT_RC_OK
99
100 title_template = TermTitleTemplate(title_template_str)
101 title_str = title_template.safe_substitute(TermTitleMapping())
102 sys.__stdout__.write(TERM_TEMPLATE % title_str)
103 sys.__stdout__.flush()
104
105 return weechat.WEECHAT_RC_OK
106
107
108 if __name__ == '__main__':
109 weechat.register(SCRIPT_NAME, AUTHOR, VERSION, 'GPL3', DESCRIPTION, '', '')
110
111 for option, (value, description) in DEFAULT_SETTINGS.iteritems():
112 if not weechat.config_is_set_plugin(option):
113 weechat.config_set_plugin(option, value)
114 weechat.config_set_desc_plugin(option, '%s (default: "%s")' %
115 (description, value))
116
117 term = os.environ.get("TERM", None)
118 if term:
119 for term_name, term_template in TERM_TEMPLATES:
120 if term.startswith(term_name):
121 TERM_TEMPLATE = term_template
122 for hook in ["buffer_switch", "buffer_title_changed",
123 "hotlist_changed", "upgrade_ended"]:
124 weechat.hook_signal(hook, "set_term_title_hook", "")
125 set_term_title_hook("", "", "")
126 break