aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/myclock.py
blob: f97c8ea887b3435299e769ee688fe6d53b24520f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- python -*-
from plugins.tzclock import TZClock

from common import *

class MyClock(Entry):
    def __init__(self, *args, format = '%Y-%m-%d %T', long_format = '%Y-(%m)%b-%d %T, %a w%V, %Z', tzs = None, **kwargs):
        self.default_format = format
        self.long_format = long_format
        self.clock = G.clock
        self.set_format(self.clock, self.default_format)
        self.tzs = [] if tzs is None else tzs
        self.tzi = 0
        self.twilight = '39'
        mqueue_map['timefmt'] = self.mqueue
        mqueue_map['tz'] = self.mqueue
        Entry.__init__(self, *args, **kwargs)
    
    def mqueue(self, args):
        if args[0] == 'timefmt':
            args = args[1:]
            if len(args) == 0:
                self.action(0, RIGHT_BUTTON, 0, 0)
            else:
                self.set_format(self.clock, ' '.join(args))
                self.invalidate()
        if args[0] == 'tz':
            args = args[1:]
            if len(args) == 0:
                self.action(0, LEFT_BUTTON, 0, 0)
            else:
                old_clock = self.clock
                tz = '/'.join(args)
                try:
                    tzclock = TZClock(timezone = tz, format = self.clock.u_format)
                except:
                    try:
                        tzclock = TZClock(timezone = tz.upper(), format = self.clock.u_format)
                    except:
                        print('%s: unknown typezone: %s' % (sys.argv[0], tz), file = sys.stderr, flush = True)
                        return
                tzclock.u_format = tzclock.format
                self.clock = tzclock
                if old_clock is not G.clock:
                    del old_clock
                self.invalidate()
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            if self.clock is G.clock:
                G.clock.utc = not G.clock.utc
                self.set_format(G.clock, self.clock.u_format)
            else:
                self.set_format(G.clock, self.clock.u_format)
                self.clock = G.clock
            self.invalidate()
        elif button == RIGHT_BUTTON:
            if self.clock.u_format == self.default_format:
                self.set_format(self.clock, self.long_format)
            else:
                self.set_format(self.clock, self.default_format)
            self.invalidate()
        elif button in (SCROLL_UP, SCROLL_DOWN):
            i = self.tzi + (+1 if button == SCROLL_UP else -1)
            if not (0 <= i <= len(self.tzs)):
                return
            self.tzi = i
            old_clock = self.clock
            if i > 0:
                tzclock = TZClock(timezone = self.tzs[i - 1], format = self.clock.u_format)
                tzclock.u_format = tzclock.format
                self.clock = tzclock
            else:
                self.clock = G.clock
            if old_clock is not G.clock:
                del old_clock
            self.invalidate()
    
    def set_format(self, clck, format):
        clck.u_format = format
        if not (clck is G.clock and clck.utc):
            clck.format = format
        else:
            fmt, esc = '', False
            for c in format:
                if esc:
                    fmt += c
                    if c == '%' or 'a' <= c.lower() <= 'z':
                        esc = False
                        if c == 'Z':
                            while fmt[-1] != '%':
                                fmt = fmt[:-1]
                            fmt = fmt[:-1] + 'UTC'
                elif c == '%':
                    esc = True
                    fmt += c
                else:
                    fmt += c
            clck.format = fmt
    
    def function(self):
        clck = self.clock
        colour = self.twilight
        if clck is not G.clock or clck.utc:
            colour += ';7'
        return '\033[%sm%s\033[00m' % (colour, clck.read())