Merge pull request #1355 from ZyX-I/fix-1347

Fix tmux-2.0 support
This commit is contained in:
Nikolai Aleksandrovich Pavlov 2015-05-10 16:45:54 +03:00
commit 560627a6e7
6 changed files with 118 additions and 19 deletions

View File

@ -4,13 +4,16 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
import os
import re
import sys
import subprocess
import shlex
from powerline.config import POWERLINE_ROOT, TMUX_CONFIG_DIRECTORY
from powerline.lib.config import ConfigLoader
from powerline import generate_config_finder, load_config, create_logger, PowerlineLogger, finish_common_config
from powerline.shell import ShellPowerline
from powerline.lib.shell import which
from powerline.bindings.tmux import TmuxVersionInfo, run_tmux_command, set_tmux_environment, get_tmux_version
from powerline.bindings.tmux import (TmuxVersionInfo, run_tmux_command, set_tmux_environment, get_tmux_version,
source_tmux_file)
from powerline.lib.encoding import get_preferred_output_encoding
from powerline.renderers.tmux import attrs_to_tmux_attrs
from powerline.commands.main import finish_args
@ -59,7 +62,7 @@ def get_tmux_configs(version):
yield (fname, priority + file_version.minor * 10 + file_version.major * 10000)
def source_tmux_files(pl, args):
def source_tmux_files(pl, args, tmux_version=None, source_tmux_file=source_tmux_file):
'''Source relevant version-specific tmux configuration files
Files are sourced in the following order:
@ -67,15 +70,20 @@ def source_tmux_files(pl, args):
* If files for same versions are to be sourced then first _minus files are
sourced, then _plus files and then files without _minus or _plus suffixes.
'''
version = get_tmux_version(pl)
run_tmux_command('source', os.path.join(TMUX_CONFIG_DIRECTORY, 'powerline-base.conf'))
for fname, priority in sorted(get_tmux_configs(version), key=(lambda v: v[1])):
run_tmux_command('source', fname)
tmux_version = tmux_version or get_tmux_version(pl)
source_tmux_file(os.path.join(TMUX_CONFIG_DIRECTORY, 'powerline-base.conf'))
for fname, priority in sorted(get_tmux_configs(tmux_version), key=(lambda v: v[1])):
source_tmux_file(fname)
if not os.environ.get('POWERLINE_COMMAND'):
cmd = deduce_command()
if cmd:
set_tmux_environment('POWERLINE_COMMAND', deduce_command(), remove=False)
run_tmux_command('refresh-client')
try:
run_tmux_command('refresh-client')
except subprocess.CalledProcessError:
# On tmux-2.0 this command may fail for whatever reason. Since it is
# critical just ignore the failure.
pass
class EmptyArgs(object):
@ -87,7 +95,7 @@ class EmptyArgs(object):
return None
def init_tmux_environment(pl, args):
def init_tmux_environment(pl, args, set_tmux_environment=set_tmux_environment):
'''Initialize tmux environment from tmux configuration
'''
powerline = ShellPowerline(finish_args(os.environ, EmptyArgs('tmux', args.config_path)))
@ -164,9 +172,43 @@ def init_tmux_environment(pl, args):
' ' * powerline.renderer.strwidth(left_dividers['hard'])))
TMUX_VAR_RE = re.compile('\$(_POWERLINE_\w+)')
def tmux_setup(pl, args):
init_tmux_environment(pl, args)
source_tmux_files(pl, args)
tmux_environ = {}
tmux_version = get_tmux_version(pl)
def set_tmux_environment_nosource(varname, value, remove=True):
tmux_environ[varname] = value
def replace_cb(match):
return tmux_environ[match.group(1)]
def replace_env(s):
return TMUX_VAR_RE.subn(replace_cb, s)[0]
def source_tmux_file_nosource(fname):
with open(fname) as fd:
for line in fd:
if line.startswith('#') or line == '\n':
continue
args = shlex.split(line)
args = [args[0]] + [replace_env(arg) for arg in args[1:]]
run_tmux_command(*args)
if args.source is None:
args.source = tmux_version < (1, 9)
if args.source:
ste = set_tmux_environment
stf = source_tmux_file
else:
ste = set_tmux_environment_nosource
stf = source_tmux_file_nosource
init_tmux_environment(pl, args, set_tmux_environment=ste)
source_tmux_files(pl, args, tmux_version=tmux_version, source_tmux_file=stf)
def get_main_config(args):

View File

@ -50,7 +50,21 @@ def set_tmux_environment(varname, value, remove=True):
'''
run_tmux_command('set-environment', '-g', varname, value)
if remove:
run_tmux_command('set-environment', '-r', varname)
try:
run_tmux_command('set-environment', '-r', varname)
except subprocess.CalledProcessError:
# On tmux-2.0 this command may fail for whatever reason. Since it is
# critical just ignore the failure.
pass
def source_tmux_file(fname):
'''Source tmux configuration file
:param str fname:
Full path to the sourced file.
'''
run_tmux_command('source', fname)
NON_DIGITS = re.compile('[^0-9]+')

View File

@ -8,5 +8,5 @@ set -g window-status-format "#[$_POWERLINE_WINDOW_COLOR]$_POWERLINE_LEFT_HARD_DI
set -g window-status-current-format "#[$_POWERLINE_WINDOW_CURRENT_HARD_DIVIDER_COLOR]$_POWERLINE_LEFT_HARD_DIVIDER#[$_POWERLINE_WINDOW_CURRENT_COLOR]#I $_POWERLINE_LEFT_SOFT_DIVIDER#[$_POWERLINE_WINDOW_NAME_COLOR]#W #[$_POWERLINE_WINDOW_CURRENT_HARD_DIVIDER_NEXT_COLOR]$_POWERLINE_LEFT_HARD_DIVIDER"
# Legacy status-left definition to be overwritten for tmux Versions 1.8+
set -g status-left "#[$_POWERLINE_SESSION_COLOR] #S #[$_POWERLINE_SESSION_HARD_DIVIDER_NEXT_COLOR]$_POWERLINE_LEFT_HARD_DIVIDER#(eval \$POWERLINE_COMMAND tmux left)"
set -g status-left "#[$_POWERLINE_SESSION_COLOR] #S #[$_POWERLINE_SESSION_HARD_DIVIDER_NEXT_COLOR]$_POWERLINE_LEFT_HARD_DIVIDER#(env \"\$POWERLINE_COMMAND\" tmux left)"
# vim: ft=tmux

View File

@ -60,7 +60,24 @@ def get_argparser(ArgumentParser=ConfigArgParser):
help='If action is `source\' then version-specific tmux configuration '
'files are sourced, if it is `setenv\' then special '
'(prefixed with `_POWERLINE\') tmux global environment variables '
'are filled with data from powerline configuration.'
'are filled with data from powerline configuration. '
'Action `setup\' is just doing `setenv\' then `source\'.'
)
tpg = tmux_parser.add_mutually_exclusive_group()
tpg.add_argument(
'-s', '--source', action='store_true', default=None,
help='When using `setup\': always use configuration file sourcing. '
'By default this is determined automatically based on tmux '
'version: this is the default for tmux 1.8 and below.',
)
tpg.add_argument(
'-n', '--no-source', action='store_false', dest='source', default=None,
help='When using `setup\': in place of sourcing directly execute '
'configuration files. That is, read each needed '
'powerline-specific configuration file, substitute '
'`$_POWERLINE_…\' variables with appropriate values and run '
'`tmux config line\'. This is the default behaviour for '
'tmux 1.9 and above.'
)
shell_parser = subparsers.add_parser('shell', help='Shell-specific commands')

View File

@ -36,7 +36,7 @@ def test_expected_result(p, expected_result, cols, rows, print_logs):
last_line = []
for col in range(cols):
last_line.append(p[rows - 1, col])
attempts = 10
attempts = 3
result = None
while attempts:
result = tuple((
@ -176,6 +176,26 @@ def main(attempts=3):
)
p.start()
sleep(2)
expected_result_2_0 = (
(((0, 0, 0), (243, 243, 243), 1, 0, 0), ' 0 '),
(((243, 243, 243), (11, 11, 11), 0, 0, 0), ' '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' S2 string here '),
(((133, 133, 133), (11, 11, 11), 0, 0, 0), ' 0 '),
(((88, 88, 88), (11, 11, 11), 0, 0, 0), '| '),
(((188, 188, 188), (11, 11, 11), 0, 0, 0), 'bash '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' '),
(((133, 133, 133), (11, 11, 11), 0, 0, 0), ' 1 '),
(((88, 88, 88), (11, 11, 11), 0, 0, 0), '| '),
(((0, 102, 153), (11, 11, 11), 0, 0, 0), 'bash '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' '),
(((11, 11, 11), (0, 102, 153), 0, 0, 0), ' '),
(((102, 204, 255), (0, 102, 153), 0, 0, 0), '2 | '),
(((255, 255, 255), (0, 102, 153), 1, 0, 0), 'bash '),
(((0, 102, 153), (11, 11, 11), 0, 0, 0), ' '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' ' * 128),
(((88, 88, 88), (11, 11, 11), 0, 0, 0), ' '),
(((199, 199, 199), (88, 88, 88), 0, 0, 0), ' S1 string here '),
)
expected_result_new = (
(((0, 0, 0), (243, 243, 243), 1, 0, 0), ' 0 '),
(((243, 243, 243), (11, 11, 11), 0, 0, 0), ' '),
@ -192,7 +212,7 @@ def main(attempts=3):
(((102, 204, 255), (0, 102, 153), 0, 0, 0), '2 | '),
(((255, 255, 255), (0, 102, 153), 1, 0, 0), 'bash '),
(((0, 102, 153), (11, 11, 11), 0, 0, 0), ' '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' ' * 127),
(((88, 88, 88), (11, 11, 11), 0, 0, 0), ' '),
(((199, 199, 199), (88, 88, 88), 0, 0, 0), ' S1 string here '),
)
@ -212,15 +232,17 @@ def main(attempts=3):
(((102, 204, 255), (0, 102, 153), 0, 0, 0), '2 | '),
(((255, 255, 255), (0, 102, 153), 1, 0, 0), 'bash '),
(((0, 102, 153), (11, 11, 11), 0, 0, 0), ' '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' '),
(((255, 255, 255), (11, 11, 11), 0, 0, 0), ' ' * 127),
(((88, 88, 88), (11, 11, 11), 0, 0, 0), ' '),
(((199, 199, 199), (88, 88, 88), 0, 0, 0), ' S1 string here '),
)
tmux_version = get_tmux_version(get_fallback_logger())
if tmux_version < (1, 8):
expected_result = expected_result_old
else:
if tmux_version >= (2, 0):
expected_result = expected_result_2_0
elif tmux_version >= (1, 8):
expected_result = expected_result_new
else:
expected_result = expected_result_old
if not test_expected_result(p, expected_result, cols, rows, not attempts):
if attempts:
pass

View File

@ -191,6 +191,7 @@ ln -s "$(which rm)" tests/shell/path
ln -s "$(which uname)" tests/shell/path
ln -s "$(which test)" tests/shell/path
ln -s "$(which pwd)" tests/shell/path
ln -s "$(which hostname)" tests/shell/path
ln -s ../../test_shells/bgscript.sh tests/shell/path
ln -s ../../test_shells/waitpid.sh tests/shell/path
if which socat ; then
@ -369,6 +370,9 @@ if test -z "${ONLY_SHELL}" || test "x${ONLY_SHELL%sh}" != "x${ONLY_SHELL}" || te
if test x$FAST$SH = x1dash ; then
continue
fi
if test x$FAST$SH = x1fish ; then
continue
fi
if test "x$ONLY_SHELL" != "x" && test "x$ONLY_SHELL" != "x$SH" ; then
continue
fi