Merge remote-tracking branch 'zyx-i/threaded-misc' into develop

This commit is contained in:
Kim Silkebækken 2013-03-19 10:52:31 +01:00
commit 83d855d3d7
5 changed files with 62 additions and 27 deletions

View File

@ -1,4 +1,5 @@
_powerline_tmux_setenv() {
emulate -L zsh
if [[ -n "$TMUX" ]]; then
tmux setenv TMUX_"$1"_$(tmux display -p "#D" | tr -d %) "$2"
fi
@ -13,12 +14,13 @@ _powerline_tmux_set_columns() {
}
_powerline_install_precmd() {
emulate -L zsh
for f in "${precmd_functions[@]}"; do
if [[ "$f" = "_powerline_precmd" ]]; then
return
fi
done
precmd_functions+=(_powerline_tmux_set_pwd)
chpwd_functions+=( _powerline_tmux_set_pwd )
setopt promptpercent
setopt promptsubst
if zmodload zsh/zpython &>/dev/null ; then
@ -32,6 +34,6 @@ _powerline_install_precmd() {
}
trap "_powerline_tmux_set_columns" SIGWINCH
kill -SIGWINCH $$
_powerline_tmux_set_columns
_powerline_install_precmd

View File

@ -11,6 +11,8 @@ from threading import Thread, Lock
class ThreadedSegment(Thread):
daemon = True
min_sleep_time = 0.1
update_first = True
interval = 1
def __init__(self):
super(ThreadedSegment, self).__init__()
@ -46,24 +48,25 @@ class ThreadedSegment(Thread):
self.keep_going = False
self.update_lock.acquire()
def set_interval(self, interval=None, **kwargs):
def set_interval(self, interval=None):
# Allowing “interval” keyword in configuration.
# Note: Here **kwargs is needed to support foreign data, in subclasses
# it can be seen in a number of places in order to support
# .set_interval().
interval = interval or getattr(self, 'interval', 1)
interval = interval or getattr(self, 'interval')
self.interval = interval
self.has_set_interval = True
def set_state(self, **kwargs):
if not self.did_set_interval:
self.set_interval(**kwargs)
def set_state(self, interval=None, **kwargs):
if not self.did_set_interval or interval:
self.set_interval(interval)
# Without this we will not have to wait long until receiving bug “I
# opened vim, but branch information is only shown after I move cursor”.
if self.update_first:
self.update_first = False
self.update()
def startup(self, **kwargs):
# Normally .update() succeeds to run before value is requested, meaning
# that user is getting values he needs directly at vim startup. Without
# .startup() we will not have to wait long until receiving bug “I opened
# vim, but branch information is only shown after I move cursor”.
self.run_once = False
self.set_state(**kwargs)
@ -81,11 +84,12 @@ def printed(func):
class KwThreadedSegment(ThreadedSegment):
drop_interval = 10 * 60
update_missing = True
update_first = False
def __init__(self):
super(KwThreadedSegment, self).__init__()
self.queries = {}
self.update_missing = True
@staticmethod
def key(**kwargs):
@ -96,6 +100,8 @@ class KwThreadedSegment(ThreadedSegment):
try:
update_state = self.queries[key][1]
except KeyError:
# self.update_missing has the same reasoning as self.update_first in
# parent class
update_state = self.compute_state(key) if self.update_missing else None
# No locks: render method is already running with write_lock acquired.
self.queries[key] = (monotonic(), update_state)
@ -114,12 +120,13 @@ class KwThreadedSegment(ThreadedSegment):
for key in removes:
self.queries.pop(key)
def set_state(self, **kwargs):
if not self.did_set_interval or ('interval' in kwargs and self.interval > kwargs['interval']):
self.set_interval(**kwargs)
def set_state(self, interval=None, **kwargs):
if not self.did_set_interval or (interval < self.interval):
self.set_interval(interval)
key = self.key(**kwargs)
self.queries[key] = (monotonic(), None)
if key not in self.queries:
self.queries[key] = (monotonic(), self.compute_state(key) if self.update_missing else None)
@staticmethod
def render_one(update_state, **kwargs):

View File

@ -174,8 +174,8 @@ def _external_ip(query_url='http://ipv4.icanhazip.com/'):
class ExternalIpSegment(ThreadedSegment):
def set_state(self, query_url='http://ipv4.icanhazip.com/', **kwargs):
super(ExternalIpSegment, self).set_state(**kwargs)
self.query_url = query_url
super(ExternalIpSegment, self).set_state(**kwargs)
def update(self):
ip = _external_ip(query_url=self.query_url)
@ -183,6 +183,8 @@ class ExternalIpSegment(ThreadedSegment):
self.ip = ip
def render(self):
if not hasattr(self, 'ip'):
return None
return [{'contents': self.ip, 'divider_highlight_group': 'background:divider'}]
@ -295,10 +297,10 @@ class WeatherSegment(ThreadedSegment):
interval = 600
def set_state(self, location_query=None, **kwargs):
super(WeatherSegment, self).set_state(**kwargs)
self.location = location_query
self.url = None
self.condition = {}
super(WeatherSegment, self).set_state(**kwargs)
def update(self):
import json
@ -581,7 +583,7 @@ class NetworkLoadSegment(KwThreadedSegment):
t2, b2 = idata['last']
measure_interval = t2 - t1
if None in (b1, b2):
if None in (b1, b2) or measure_interval == 0:
return None
return [{

View File

@ -301,6 +301,7 @@ def modified_buffers(text='+ ', join_str=','):
class KwWindowThreadedSegment(KwThreadedSegment):
def set_state(self, **kwargs):
kwargs = kwargs.copy()
for window in vim.windows:
buffer = window.buffer
kwargs['segment_info'] = {'bufnr': buffer.number, 'buffer': buffer}
@ -372,9 +373,9 @@ class BranchSegment(RepositorySegment):
'divider_highlight_group': 'branch:divider',
}]
def startup(self, **kwargs):
def startup(self, status_colors=False, **kwargs):
super(BranchSegment, self).startup()
if kwargs.get('status_colors', False):
if status_colors:
self.started_repository_status = True
repository_status.startup()

View File

@ -15,6 +15,17 @@ VBLOCK = chr(ord('V') - 0x40)
SBLOCK = chr(ord('S') - 0x40)
def shutdown(powerline):
from powerline.segments import common, vim
try:
powerline.renderer.shutdown()
finally:
# After shutdown threads are useless, it is needed to recreate them.
from imp import reload
reload(common)
reload(vim)
class TestConfig(TestCase):
def test_vim(self):
from powerline.vim import VimPowerline
@ -49,7 +60,7 @@ class TestConfig(TestCase):
check_output(1, 0)
finally:
vim_module._start_mode('n')
powerline.renderer.shutdown()
shutdown(powerline)
def test_tmux(self):
from powerline.segments import common
@ -57,16 +68,27 @@ class TestConfig(TestCase):
reload(common)
from powerline.shell import ShellPowerline
with replace_module_attr(common, 'urllib_read', urllib_read):
ShellPowerline(Args(ext=['tmux']), run_once=True).renderer.render()
reload(common)
powerline = ShellPowerline(Args(ext=['tmux']), run_once=False)
powerline.renderer.render()
powerline = ShellPowerline(Args(ext=['tmux']), run_once=False)
powerline.renderer.render()
shutdown(powerline)
def test_zsh(self):
from powerline.shell import ShellPowerline
ShellPowerline(Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt'), run_once=True).renderer.render()
powerline = ShellPowerline(Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt'), run_once=False)
powerline.renderer.render()
powerline = ShellPowerline(Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt'), run_once=False)
powerline.renderer.render()
shutdown(powerline)
def test_bash(self):
from powerline.shell import ShellPowerline
ShellPowerline(Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})]), run_once=True).renderer.render()
powerline = ShellPowerline(Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})]), run_once=False)
powerline.renderer.render()
powerline = ShellPowerline(Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})]), run_once=False)
powerline.renderer.render()
shutdown(powerline)
def test_ipython(self):
from powerline.ipython import IpythonPowerline
@ -78,7 +100,8 @@ class TestConfig(TestCase):
powerline = IpyPowerline()
powerline.renderer.render()
powerline.renderer.shutdown()
powerline.renderer.render()
shutdown(powerline)
def test_wm(self):
from powerline.segments import common