Make attached_clients segment use provided tmux bindings

This commit is contained in:
ZyX 2014-08-24 21:07:19 +04:00
parent eee150f97c
commit e4565dd3e8
2 changed files with 15 additions and 30 deletions

View File

@ -1,7 +1,6 @@
# vim:fileencoding=utf-8:noet
import os
from subprocess import Popen, PIPE
from powerline.bindings.tmux import get_tmux_output
def attached_clients(pl, minimum=1):
@ -10,25 +9,12 @@ def attached_clients(pl, minimum=1):
:param int minimum:
The minimum number of attached clients that must be present for this segment to be visible
'''
try:
with open(os.devnull, "w") as devnull:
find_session_name = ["tmux", "list-panes", "-F", "#{session_name}"]
session_name_process = Popen(find_session_name, stdout=PIPE, stderr=devnull)
session_output, err = session_name_process.communicate()
if 0 == len(session_output):
return None
session_name = session_output.rstrip().split(os.linesep)[0]
find_clients = ["tmux", "list-clients", "-t", session_name]
attached_clients_process = Popen(find_clients, stdout=PIPE, stderr=devnull)
attached_clients_output, err = attached_clients_process.communicate()
attached_count = len(attached_clients_output.rstrip().split(os.linesep))
except Exception as e:
sys.stderr.write('Could not execute attached_clients: ({0})\n'.format(e))
session_output = get_tmux_output('list-panes', '-F', '#{session_name}')
if not session_output:
return None
session_name = session_output.rstrip().split('\n')[0]
attached_clients_output = get_tmux_output('list-clients', '-t', session_name)
attached_count = len(attached_clients_output.rstrip().split('\n'))
return None if attached_count < minimum else str(attached_count)

View File

@ -12,7 +12,7 @@ from powerline.lib.vcs import get_fallback_create_watcher
import tests.vim as vim_module
from tests.lib import Args, urllib_read, replace_attr, new_module, replace_module_module, replace_env, Pl, Process
from tests.lib import Args, urllib_read, replace_attr, new_module, replace_module_module, replace_env, Pl
from tests import TestCase, SkipTest
@ -285,16 +285,15 @@ class TestShell(TestCase):
class TestTmux(TestCase):
def test_attached_clients(self):
def popen_mock(parameters, **kwargs):
if "list-panes" == parameters[1]:
return Process("session_name", "")
elif "list-clients" == parameters[1]:
clients = ["/dev/pts/2: 0 [191x51 xterm-256color] (utf8)", "/dev/pts/3: 0 [191x51 xterm-256color] (utf8)"]
return Process(os.linesep.join(clients), "")
def get_tmux_output(cmd, *args):
if cmd == 'list-panes':
return 'session_name\n'
elif cmd == 'list-clients':
return '/dev/pts/2: 0 [191x51 xterm-256color] (utf8)\n/dev/pts/3: 0 [191x51 xterm-256color] (utf8)'
pl = Pl()
with replace_attr(tmux, 'Popen', popen_mock):
self.assertEqual(tmux.attached_clients(pl=pl, ), "2")
with replace_attr(tmux, 'get_tmux_output', get_tmux_output):
self.assertEqual(tmux.attached_clients(pl=pl), '2')
self.assertEqual(tmux.attached_clients(pl=pl, minimum=3), None)