aboutsummaryrefslogtreecommitdiffstats
path: root/src/interface.py
blob: d6535a2598af268cb4e466a12e93eda6fc08e1be (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
#!/usr/bin/env python3
# -*- python -*-
'''
nightshift - A terminal user interface for redshift
Copyright © 2014  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/>.
'''

import sys
import threading


def user_interface():
    '''
    Start user interface
    '''
    print('\033[?1049h')
    try:
        sock.sendall('status\n'.encode('utf-8'))
        def callback(status):
            if status is None:
                return
            print('\033[H\033[2J', end = '')
            for key in status:
                print(key + ': ' + status[key])
            #brightness = [float(status['%s brightness' % k]) for k in ('Night', 'Current', 'Daytime')]
            #temperature = [float(status['%s temperature' % k]) for k in ('Night', 'Current', 'Daytime')]
            #dayness = float(status['Dayness'])
            #enabled = status['Enabled'] == 'yes'
            #running = status['Running'] == 'yes'
            #location = [float(status['Latitude']), float(status['Longitude'])]
        thread = threading.Thread(target = ui_status, args = (callback,))
        thread.setDaemon(True)
        thread.start()
        
        input()
    except:
        pass
    finally:
        print('\033[?1049l')


def ui_status(callback):
    buf = ''
    continue_to_run = True
    while continue_to_run:
        while '\n\n' not in buf:
            got = sock.recv(1024)
            if (got is None) or (len(got) == 0):
                continue_to_run = False
                break
            buf += got.decode('utf-8', 'replace')
            if '\n\n' in buf:
                break
        if continue_to_run:
            msg, buf = buf.split('\n\n')[0], '\n\n'.join(buf.split('\n\n')[1:])
            callback(dict([line.split(': ') for line in msg.split('\n')]))
    callback(None)