From 7e214a65b0c78a6a8de30de2a4b44d648b2fa27f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Fri, 11 Jan 2013 13:37:22 +0100 Subject: [PATCH 1/5] Create tmux segments Refs #44. --- powerline/ext/tmux/segments.py | 92 ++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/powerline/ext/tmux/segments.py b/powerline/ext/tmux/segments.py index c42c5faf..9f952628 100644 --- a/powerline/ext/tmux/segments.py +++ b/powerline/ext/tmux/segments.py @@ -2,10 +2,94 @@ import os +from powerline.lib.memoize import memoize -def user_name(): - user_name = os.environ.get('USER') +# Weather condition code descriptions available at http://developer.yahoo.com/weather/#codes +weather_conditions_codes = { + u'〇': [25, 34], + u'⚑': [24], + u'☔': [5, 6, 8, 9, 10, 11, 12, 35, 40, 45, 47], + u'☁': range(26, 30) + [44], + u'❅': [7] + range(13, 18) + [41, 42, 43, 46], + u'☈': range(0, 4) + range(37, 39), + u'〰': range(19, 23), + u'☼': [32, 36], + u'☾': [31, 33], +} + + +def date(format='%Y-%m-%d'): + from datetime import datetime + return datetime.now().strftime(format) + + +@memoize(600, persistent=True) +def external_ip(query_url='http://icanhazip.com/'): + import urllib2 + try: + return urllib2.urlopen(query_url).read().strip() + except urllib2.HTTPError: + return + + +def system_load(format='{avg[0]:.1f}, {avg[1]:.1f}, {avg[2]:.1f}'): + from multiprocessing import cpu_count + averages = os.getloadavg() + normalized = averages[1] / cpu_count() + if normalized < 1: + gradient = 'system_load_good' + elif normalized < 2: + gradient = 'system_load_bad' + else: + gradient = 'system_load_ugly' return { - 'contents': user_name, - 'highlight': 'user_name' if user_name != 'root' else ['user_name_root', 'user_name'], + 'contents': format.format(avg=averages), + 'highlight': [gradient, 'system_load'] } + + +def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'): + # TODO: make this work with operating systems without /proc/uptime + try: + with open('/proc/uptime', 'r') as f: + seconds = int(float(f.readline().split()[0])) + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) + days, hours = divmod(hours, 24) + + return format.format(days=int(days), hours=hours, minutes=minutes) + except IOError: + pass + + +@memoize(600, persistent=True) +def forecast(unit='c', location_query=None): + import json + import urllib + import urllib2 + + if not location_query: + try: + location = json.loads(urllib2.urlopen('http://freegeoip.net/json/' + external_ip()).read()) + location_query = ','.join([location['city'], location['region_name'], location['country_name']]) + except ValueError: + return None + + query_data = { + 'q': + 'use "http://github.com/yql/yql-tables/raw/master/weather/weather.bylocation.xml" as we;' + 'select * from we where location="{0}" and unit="{1}"'.format(location_query, unit), + 'format': 'json' + } + url = 'http://query.yahooapis.com/v1/public/yql?' + urllib.urlencode(query_data) + response = json.loads(urllib2.urlopen(url).read()) + + condition = response['query']['results']['weather']['rss']['channel']['item']['condition'] + condition_code = int(condition['code']) + icon = u'〇' + + for icon, codes in weather_conditions_codes.items(): + if condition_code in codes: + break + + return u'{0} {1}°{2}'.format(icon, condition['temp'], unit.upper()) From f827420ea16c3eba00fae3785bdece522a127825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Wed, 16 Jan 2013 08:01:15 +0100 Subject: [PATCH 2/5] Rename default colorscheme --- powerline/colorschemes/{default.json => vim_default.json} | 0 powerline/config.json | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename powerline/colorschemes/{default.json => vim_default.json} (100%) diff --git a/powerline/colorschemes/default.json b/powerline/colorschemes/vim_default.json similarity index 100% rename from powerline/colorschemes/default.json rename to powerline/colorschemes/vim_default.json diff --git a/powerline/config.json b/powerline/config.json index dfae53ff..e181034c 100644 --- a/powerline/config.json +++ b/powerline/config.json @@ -21,7 +21,7 @@ "theme": "default" }, "vim": { - "colorscheme": "default", + "colorscheme": "vim_default", "theme": "default", "local_themes": { "help": "help" From f6050fc08f05446da1987692908e2fd708d7c74d Mon Sep 17 00:00:00 2001 From: Liam Curry Date: Thu, 10 Jan 2013 13:55:18 -0500 Subject: [PATCH 3/5] Add right/left options to tmux script --- examples/tmux/pl.py | 16 +++++++++++++--- examples/tmux/tmux.conf | 5 ++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/tmux/pl.py b/examples/tmux/pl.py index 526d0e3b..d816f76d 100755 --- a/examples/tmux/pl.py +++ b/examples/tmux/pl.py @@ -4,12 +4,22 @@ Run with `tmux -f tmux.conf`. ''' - +import argparse import os import sys + sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) from powerline.core import Powerline -pl = Powerline('tmux') -print(pl.renderer.render('n').encode('utf-8')) +parser = argparse.ArgumentParser(description='powerline outputter') +parser.add_argument('side', nargs='?', default='all', choices=('all', 'left', 'right')) +parser.add_argument('--ext', default='tmux') + +if __name__ == '__main__': + args = parser.parse_args() + pl = Powerline(args.ext) + segments = pl.renderer.get_theme().get_segments() + if args.side != 'all': + segments = [s for s in segments if s['side'] == args.side] + print(pl.renderer.render('n', segments=segments).encode('utf-8')) diff --git a/examples/tmux/tmux.conf b/examples/tmux/tmux.conf index c0c1aec9..fd4d656b 100644 --- a/examples/tmux/tmux.conf +++ b/examples/tmux/tmux.conf @@ -2,4 +2,7 @@ set-option -g status on set-option -g status-interval 2 set-option -g status-utf8 on set-option -g status-left-length 100 -set-option -g status-left "#(./pl.py)" +set-option -g status-left "#(./pl.py left)" +set-option -g status-right-length 100 +set-option -g status-right "#(./pl.py right)" +set-option -g status-justify "centre" From 5f0ec038e4059a0f8c85401f91094fedf80b80c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Wed, 16 Jan 2013 08:21:36 +0100 Subject: [PATCH 4/5] Create neutral tmux colorscheme --- examples/tmux/tmux.conf | 2 ++ powerline/colorschemes/tmux_default.json | 29 ++++++++++++++++++++++++ powerline/config.json | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 powerline/colorschemes/tmux_default.json diff --git a/examples/tmux/tmux.conf b/examples/tmux/tmux.conf index fd4d656b..78ebb3fa 100644 --- a/examples/tmux/tmux.conf +++ b/examples/tmux/tmux.conf @@ -6,3 +6,5 @@ set-option -g status-left "#(./pl.py left)" set-option -g status-right-length 100 set-option -g status-right "#(./pl.py right)" set-option -g status-justify "centre" +set-option -g status-bg "colour235" +set-option -g status-fg "colour249" diff --git a/powerline/colorschemes/tmux_default.json b/powerline/colorschemes/tmux_default.json new file mode 100644 index 00000000..20ea2ce0 --- /dev/null +++ b/powerline/colorschemes/tmux_default.json @@ -0,0 +1,29 @@ +{ + "name": "Default color scheme for terminal prompts", + "colors": { + "black": 16, + "white": 231, + + "darkestblue": 24, + "darkblue": 31, + "mediumblue": 38, + "brightblue": 117, + "brightestblue": 153, + + "green": 148, + "yellow": 220, + "red": 202 + }, + "groups": { + "user": { "fg": "white", "bg": "mediumblue", "attr": ["bold"] }, + "date": { "fg": "white", "bg": "mediumblue" }, + "forecast": { "fg": "brightestblue", "bg": "darkblue" }, + "external_ip": { "fg": "brightestblue", "bg": "darkblue" }, + "system_load": { "fg": "brightblue", "bg": "darkestblue" }, + "system_load_good": { "fg": "green", "bg": "darkestblue" }, + "system_load_bad": { "fg": "yellow", "bg": "darkestblue" }, + "system_load_ugly": { "fg": "red", "bg": "darkestblue" }, + "uptime": { "fg": "brightblue", "bg": "darkestblue" } + }, + "mode_translations": { } +} diff --git a/powerline/config.json b/powerline/config.json index e181034c..306139bf 100644 --- a/powerline/config.json +++ b/powerline/config.json @@ -17,7 +17,7 @@ "theme": "default" }, "tmux": { - "colorscheme": "default", + "colorscheme": "tmux_default", "theme": "default" }, "vim": { From 52c2ba5c9fc3eada390f99454b59ad6569923933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Wed, 16 Jan 2013 08:21:52 +0100 Subject: [PATCH 5/5] Enable tmux segments --- powerline/themes/tmux/default.json | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/powerline/themes/tmux/default.json b/powerline/themes/tmux/default.json index 4f368ee5..f5cb4bad 100644 --- a/powerline/themes/tmux/default.json +++ b/powerline/themes/tmux/default.json @@ -2,7 +2,35 @@ "segments": { "left": [ { - "name": "user_name" + "module": "powerline.ext.terminal.segments", + "name": "user" + }, + { + "name": "external_ip", + "before": "ⓦ " + }, + { + "name": "uptime", + "before": "⇑ " + } + ], + "right": [ + { + "name": "system_load" + }, + { + "name": "forecast" + }, + { + "name": "date", + "args": {"format": "%a"} + }, + { + "name": "date" + }, + { + "name": "date", + "args": {"format": "%H:%M"} } ] }