summaryrefslogtreecommitdiffstats
path: root/powerline/commands/config.py
blob: e9ca5f9103d14d9d9097e1fb7ea1eda671bb85a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# vim:fileencoding=utf-8:noet
from __future__ import (division, absolute_import, print_function)

import argparse

import powerline.bindings.config as config


class StrFunction(object):
	def __init__(self, function, name=None):
		self.name = name or function.__name__
		self.function = function

	def __call__(self, *args, **kwargs):
		self.function(*args, **kwargs)

	def __str__(self):
		return self.name


TMUX_ACTIONS = {
	'source': StrFunction(config.source_tmux_files, 'source'),
	'setenv': StrFunction(config.init_tmux_environment, 'setenv'),
	'setup': StrFunction(config.tmux_setup, 'setup'),
}


SHELL_ACTIONS = {
	'command': StrFunction(config.shell_command, 'command'),
	'uses': StrFunction(config.uses),
}


class ConfigArgParser(argparse.ArgumentParser):
	def parse_args(self, *args, **kwargs):
		ret = super(ConfigArgParser, self).parse_args(*args, **kwargs)
		if not hasattr(ret, 'function'):
			# In Python-3* `powerline-config` (without arguments) raises 
			# AttributeError. I have not found any standard way to display same 
			# error message as in Python-2*.
			self.error('too few arguments')
		return ret


def get_argparser(ArgumentParser=ConfigArgParser):
	parser = ArgumentParser(description='Script used to obtain powerline configuration.')
	parser.add_argument(
		'-p', '--config-path', action='append', metavar='PATH',
		help='Path to configuration directory. If it is present '
		     'then configuration files will only be sought in the provided path. '
		     'May be provided multiple times to search in a list of directories.'
	)
	subparsers = parser.add_subparsers()
	tmux_parser = subparsers.add_parser('tmux', help='Tmux-specific commands')
	tmux_parser.add_argument(
		'function',
		choices=tuple(TMUX_ACTIONS.values()),
		metavar='ACTION',
		type=(lambda v: TMUX_ACTIONS.get(v)),
		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. '
		     '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')
	shell_parser.add_argument(
		'function',
		choices=tuple(SHELL_ACTIONS.values()),
		type=(lambda v: SHELL_ACTIONS.get(v)),
		metavar='ACTION',
		help='If action is `command\' then preferred powerline command is '
		     'output, if it is `uses\' then powerline-config script will exit '
		     'with 1 if specified component is disabled and 0 otherwise.',
	)
	shell_parser.add_argument(
		'component',
		nargs='?',
		choices=('tmux', 'prompt'),
		metavar='COMPONENT',
		help='Only applicable for `uses\' subcommand: makes `powerline-config\' '
		     'exit with 0 if specific component is enabled and with 1 otherwise. '
		     '`tmux\' component stands for tmux bindings '
		     '(e.g. those that notify tmux about current directory changes), '
		     '`prompt\' component stands for shell prompt.'
	)
	shell_parser.add_argument(
		'-s', '--shell',
		metavar='SHELL',
		help='Shell for which query is run',
	)
	return parser