aboutsummaryrefslogblamecommitdiffstats
path: root/examples/plugins/leapsec
blob: 124f11519b8133031280051ed535d1746d5b94b6 (plain) (tree)
1
2
3
4
                

                                               
                                                                        













                                                                     


































































                                                                                            
# -*- python -*-
'''
xpybar – xmobar replacement written in python
Copyright © 2014, 2015, 2016  Mattias Andrée (maandree@member.fsf.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

# A xpybar configuration example testing the features of plugins.leapsec

import time
import threading

from plugins.leapsec import LeapSeconds
from plugins.clock import Clock


OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True


clock = Clock(sync_to = 0.5 * Clock.SECONDS)

start_ = start
def start():
    start_()
    async(lambda : clock.continuous_sync(lambda : bar.invalidate()))


leapsec_ = Sometimes(LeapSeconds, 30 * 24 * 60 * 2)


def redraw():
    announcements = leapsec_()
    found = -1
    now = time.time()
    for index in range(len(announcements)):
        if announcements[index][3] >= now:
            found = index
            break
    announcement = announcements[found]
    text = '\033[%sm%s\033[00m ago' if found < 0 else 'in \033[%sm%s\033[00m'
    def dur(t):
        s, t = t % 60, t // 60
        m, t = t % 60, t // 60
        h, d = t % 24, t // 24
        if d > 0:
            return '%id%ih%i\'%02i"' % (d, h, m, s)
        elif h > 0:
            return '%ih%i\'%02i"' % (h, m, s)
        elif m > 0:
            return '%i\'%02i"' % (m, s)
        else:
            return '%02is' % s
    amount = ('+%i' if announcement[4] > 0 else '%i') % announcement[4]
    c = '00'
    time_until = announcement[3] - int(now)
    if time_until >= 0:
        if time_until < 10:
            c = '41'
        elif time_until < 60:
            c = '31'
        elif time_until < 60 * 60:
            c = '33'
        elif time_until < 24 * 60 * 60:
            c = '32'
    text %= (c, dur(abs(time_until)))
    text = 'Leap: %s %s at end of UTC %s' % (amount, text, '%i-%02i-%0i' % announcement[:3])
    if announcement[5] == LeapSeconds.SECONDARY:
        text += ' (secondary)'
    if announcement[5] == LeapSeconds.OUT_OF_BAND:
        text += ' (out of band)'
    bar.clear()
    bar.draw_coloured_text(0, 10, 0, 2, text)