Show Count of Attached Tmux Sessions

- This segment displays the number of attached tmux clients to the
  currently running session.
- The minimum argument is used to specify a threshold for when the
  segment should be visible.

Fixes #661
Closes #662

Conflicts:
	docs/source/index.rst
	powerline/config_files/colorschemes/shell/default.json
	powerline/config_files/colorschemes/shell/solarized.json
	powerline/config_files/colorschemes/tmux/default.json
	powerline/config_files/colorschemes/vim/default.json
	powerline/config_files/colorschemes/vim/solarized.json
	powerline/config_files/colorschemes/wm/default.json
	tests/test_segments.py
This commit is contained in:
Matthew M. Keeler 2013-09-08 10:29:21 -05:00 committed by ZyX
parent 48b575b67b
commit 88515ab472
9 changed files with 84 additions and 12 deletions

View File

@ -0,0 +1,6 @@
*************
Tmux segments
*************
.. automodule:: powerline.segments.tmux
:members:

View File

@ -38,6 +38,7 @@
"cwd": { "fg": "gray9", "bg": "gray4", "attr": [] },
"cwd:current_folder": { "fg": "gray10", "bg": "gray4", "attr": ["bold"] },
"cwd:divider": { "fg": "gray7", "bg": "gray4", "attr": [] },
"virtualenv": { "fg": "white", "bg": "darkcyan", "attr": [] }
"virtualenv": { "fg": "white", "bg": "darkcyan", "attr": [] },
"attached_clients": { "fg": "gray8", "bg": "gray0", "attr": [] }
}
}

View File

@ -1,12 +1,13 @@
{
"name": "Default color scheme for shell prompts",
"groups": {
"hostname": { "fg": "brightyellow", "bg": "mediumorange", "attr": [] },
"jobnum": { "fg": "brightyellow", "bg": "mediumorange", "attr": [] },
"exit_fail": { "fg": "white", "bg": "darkestred", "attr": [] },
"exit_success": { "fg": "white", "bg": "darkestgreen", "attr": [] },
"environment": { "fg": "white", "bg": "darkestgreen", "attr": [] },
"mode": { "fg": "darkestgreen", "bg": "brightgreen", "attr": ["bold"] }
"hostname": { "fg": "brightyellow", "bg": "mediumorange", "attr": [] },
"jobnum": { "fg": "brightyellow", "bg": "mediumorange", "attr": [] },
"exit_fail": { "fg": "white", "bg": "darkestred", "attr": [] },
"exit_success": { "fg": "white", "bg": "darkestgreen", "attr": [] },
"environment": { "fg": "white", "bg": "darkestgreen", "attr": [] },
"mode": { "fg": "darkestgreen", "bg": "brightgreen", "attr": ["bold"] },
"attached_clients": { "fg": "white", "bg": "darkestgreen", "attr": [] }
},
"mode_translations": {
"vicmd": {

View File

@ -12,6 +12,7 @@
"cwd:current_folder": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] },
"cwd:divider": { "fg": "gray61", "bg": "darkgreencopper", "attr": [] },
"hostname": { "fg": "oldlace", "bg": "darkgreencopper", "attr": [] },
"environment": { "fg": "oldlace", "bg": "green", "attr": [] }
"environment": { "fg": "oldlace", "bg": "green", "attr": [] },
"attached_clients": { "fg": "oldlace", "bg": "green", "attr": [] }
}
}

View File

@ -27,6 +27,7 @@
"virtcol_current_gradient": { "fg": "dark_GREEN_Orange_red", "bg": "gray10", "attr": [] },
"col_current": { "fg": "gray6", "bg": "gray10", "attr": [] },
"modified_buffers": { "fg": "brightyellow", "bg": "gray2", "attr": [] },
"attached_clients": { "fg": "gray8", "bg": "gray2", "attr": [] },
"error": { "fg": "brightestred", "bg": "darkred", "attr": ["bold"] },
"warning": { "fg": "brightyellow", "bg": "darkorange", "attr": ["bold"] },
"current_tag": { "fg": "gray9", "bg": "gray2", "attr": [] }

View File

@ -28,6 +28,7 @@
"virtcol_current_gradient": { "fg": "GREEN_Orange_red", "bg": "lightyellow", "attr": [] },
"col_current": { "fg": "azure4", "bg": "lightyellow", "attr": [] },
"environment": { "fg": "gray61", "bg": "royalblue5", "attr": [] },
"attached_clients": { "fg": "gray61", "bg": "royalblue5", "attr": [] },
"error": { "fg": "oldlace", "bg": "red", "attr": ["bold"] },
"warning": { "fg": "oldlace", "bg": "orange", "attr": ["bold"] },
"current_tag": { "fg": "oldlace", "bg": "royalblue5", "attr": ["bold"] }

View File

@ -0,0 +1,34 @@
# vim:fileencoding=utf-8:noet
import os
from subprocess import Popen, PIPE
def attached_clients(pl, minimum=1):
'''Return the number of tmux clients attached to the currently active session
: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))
return None
return None if attached_count < minimum else str(attached_count)

View File

@ -44,6 +44,14 @@ def urllib_read(query_url):
else:
raise NotImplementedError
class Process(object):
def __init__(self, output, err):
self.output = output
self.err = err
def communicate(self):
return self.output, self.err
class ModuleReplace(object):
def __init__(self, name, new):

View File

@ -2,13 +2,17 @@
from __future__ import unicode_literals
from powerline.segments import shell, common
from powerline.lib.vcs import get_fallback_create_watcher
import tests.vim as vim_module
import sys
import os
from functools import partial
from tests.lib import Args, urllib_read, replace_attr, new_module, replace_module_module, replace_env, Pl
from powerline.segments import shell, tmux, common
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 import TestCase, SkipTest
@ -279,6 +283,21 @@ class TestShell(TestCase):
self.assertEqual(common.date(pl=pl, format='%H:%M', istime=True), [{'contents': '%H:%M', 'highlight_group': ['time', 'date'], 'divider_highlight_group': 'time:divider'}])
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), "")
pl = Pl()
with replace_attr(tmux, 'Popen', popen_mock):
self.assertEqual(tmux.attached_clients(pl=pl, ), "2")
self.assertEqual(tmux.attached_clients(pl=pl, minimum=3), None)
class TestCommon(TestCase):
def test_hostname(self):
pl = Pl()