Add theme-default plugin
[canto-curses.git] / plugins / xtermtitle.py
1 # Xterm title set on selection change
2 # by Jack Miller
3 # v1.0
4
5 # Set to True if you want the selection title included.
6 USE_TITLE=False
7
8 from canto_next.plugins import check_program
9
10 check_program("canto-curses")
11
12 from canto_next.hooks import on_hook
13 import locale
14 import os
15
16 prefcode = locale.getpreferredencoding()
17
18 def set_xterm_title(s):
19     os.write(1, ("\033]0; %s \007" % s).encode(prefcode))
20
21 def clear_xterm_title():
22     os.write(1, "\033]0; \007".encode(prefcode))
23
24 def xt_on_var_change(var_dict):
25     if "selected" in var_dict:
26         if var_dict["selected"] and "title" in var_dict["selected"].content:
27             set_xterm_title("Canto - " + var_dict["selected"].content["title"])
28         else:
29             set_xterm_title("Canto")
30
31 if USE_TITLE:
32     on_hook("curses_var_change", xt_on_var_change)
33 else:
34     on_hook("curses_start", lambda: set_xterm_title("Canto"))
35
36 on_hook("curses_exit", clear_xterm_title)