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