diff options
| -rw-r--r-- | src/interface.py | 71 | ||||
| -rwxr-xr-x | src/nightshift.py | 38 | 
2 files changed, 87 insertions, 22 deletions
diff --git a/src/interface.py b/src/interface.py new file mode 100644 index 0000000..d6535a2 --- /dev/null +++ b/src/interface.py @@ -0,0 +1,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) + diff --git a/src/nightshift.py b/src/nightshift.py index 0804a7d..7992311 100755 --- a/src/nightshift.py +++ b/src/nightshift.py @@ -635,25 +635,22 @@ def run():          do_client() -def user_interface(): -    ''' -    Start user interface -    ''' -    pass # TODO -    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 -        msg, buf = buf.split('\n\n')[0] + '\n\n', '\n\n'.join(buf.split('\n\n')[1:]) -        sys.stdout.buffer.write(msg.encode('utf-8')) -        sys.stdout.buffer.flush() +g, l = globals(), dict(locals()) +for key in l: +    g[key] = l[key] + + +## Import interface.py with shared globals +# Get the Python version +v = sys.version_info +if (v.major > 3) or ((v.major == 3) and (v.minor >= 4)): +    # The (new) Python 3.4 way +    import importlib.util +    exec(importlib.util.find_spec('interface').loader.get_code('interface'), g) +else: +    # The deprecated legacy way +    import importlib +    exec(importlib.find_loader('interface').get_code('interface'), g)  ## Load extension and configurations via blueshiftrc @@ -717,9 +714,6 @@ if config_file is not None:      # globals as this module, so that it can      # not only use want we have defined, but      # also redefine it for us. -    g, l = globals(), dict(locals()) -    for key in l: -        g[key] = l[key]      exec(code, g)  | 
