mirror of
https://github.com/b-ryan/powerline-shell.git
synced 2026-09-10 07:26:28 -04:00
convert many arguments to config options
This commit is contained in:
parent
46f992d9f7
commit
5d23890c81
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -6,3 +6,4 @@ powerline_shell.egg-info/
|
|||
/dist/
|
||||
tags
|
||||
config.json
|
||||
powerline-shell.json
|
||||
|
|
|
|||
|
|
@ -10,6 +10,36 @@ from .themes.default import DefaultColor
|
|||
from .utils import warn, py3
|
||||
|
||||
|
||||
def get_valid_cwd():
|
||||
""" We check if the current working directory is valid or not. Typically
|
||||
happens when you checkout a different branch on git that doesn't have
|
||||
this directory.
|
||||
We return the original cwd because the shell still considers that to be
|
||||
the working directory, so returning our guess will confuse people
|
||||
"""
|
||||
# Prefer the PWD environment variable. Python's os.getcwd function follows
|
||||
# symbolic links, which is undesirable. But if PWD is not set then fall
|
||||
# back to this func
|
||||
try:
|
||||
cwd = os.getenv('PWD') or os.getcwd()
|
||||
except:
|
||||
warn("Your current directory is invalid. If you open a ticket at " +
|
||||
"https://github.com/milkbikis/powerline-shell/issues/new " +
|
||||
"we would love to help fix the issue.")
|
||||
sys.stdout.write("> ")
|
||||
sys.exit(1)
|
||||
|
||||
parts = cwd.split(os.sep)
|
||||
up = cwd
|
||||
while parts and not os.path.exists(up):
|
||||
parts.pop()
|
||||
up = os.sep.join(parts)
|
||||
if cwd != up:
|
||||
warn("Your current directory is invalid. Lowest valid directory: "
|
||||
+ up)
|
||||
return cwd
|
||||
|
||||
|
||||
class Powerline(object):
|
||||
symbols = {
|
||||
'compatible': {
|
||||
|
|
@ -38,11 +68,13 @@ class Powerline(object):
|
|||
'bare': '%s',
|
||||
}
|
||||
|
||||
def __init__(self, args, cwd, theme=None):
|
||||
def __init__(self, args, config, theme=None):
|
||||
self.args = args
|
||||
self.cwd = cwd
|
||||
self.config = config
|
||||
self.theme = theme or DefaultColor
|
||||
mode, shell = args.mode, args.shell
|
||||
self.cwd = get_valid_cwd()
|
||||
mode = config.get("mode", "patched")
|
||||
shell = config.get("shell", "bash")
|
||||
self.color_template = self.color_templates[shell]
|
||||
self.reset = self.color_template % '[0m'
|
||||
self.lock = Powerline.symbols[mode]['lock']
|
||||
|
|
@ -51,6 +83,9 @@ class Powerline(object):
|
|||
self.separator_thin = Powerline.symbols[mode]['separator_thin']
|
||||
self.segments = []
|
||||
|
||||
def segment_conf(self, seg_name, key, default=None):
|
||||
return self.config.get(seg_name, {}).get(key, default)
|
||||
|
||||
def color(self, prefix, code):
|
||||
if code is None:
|
||||
return ''
|
||||
|
|
@ -91,60 +126,33 @@ class Powerline(object):
|
|||
segment[3]))
|
||||
|
||||
|
||||
def get_valid_cwd():
|
||||
""" We check if the current working directory is valid or not. Typically
|
||||
happens when you checkout a different branch on git that doesn't have
|
||||
this directory.
|
||||
We return the original cwd because the shell still considers that to be
|
||||
the working directory, so returning our guess will confuse people
|
||||
"""
|
||||
# Prefer the PWD environment variable. Python's os.getcwd function follows
|
||||
# symbolic links, which is undesirable. But if PWD is not set then fall
|
||||
# back to this func
|
||||
try:
|
||||
cwd = os.getenv('PWD') or os.getcwd()
|
||||
except:
|
||||
warn("Your current directory is invalid. If you open a ticket at " +
|
||||
"https://github.com/milkbikis/powerline-shell/issues/new " +
|
||||
"we would love to help fix the issue.")
|
||||
sys.stdout.write("> ")
|
||||
sys.exit(1)
|
||||
|
||||
parts = cwd.split(os.sep)
|
||||
up = cwd
|
||||
while parts and not os.path.exists(up):
|
||||
parts.pop()
|
||||
up = os.sep.join(parts)
|
||||
if cwd != up:
|
||||
warn("Your current directory is invalid. Lowest valid directory: "
|
||||
+ up)
|
||||
return cwd
|
||||
def find_config():
|
||||
for location in [
|
||||
"powerline-shell.json",
|
||||
"~/.powerline-shell.json",
|
||||
]:
|
||||
full = os.path.expanduser(location)
|
||||
if os.path.exists(full):
|
||||
return full
|
||||
|
||||
|
||||
def main():
|
||||
arg_parser = argparse.ArgumentParser()
|
||||
arg_parser.add_argument('--cwd-mode', action='store',
|
||||
help='How to display the current directory', default='fancy',
|
||||
choices=['fancy', 'plain', 'dironly'])
|
||||
arg_parser.add_argument('--cwd-only', action='store_true',
|
||||
help='Deprecated. Use --cwd-mode=dironly')
|
||||
arg_parser.add_argument('--cwd-max-depth', action='store', type=int,
|
||||
default=5, help='Maximum number of directories to show in path')
|
||||
arg_parser.add_argument('--cwd-max-dir-size', action='store', type=int,
|
||||
help='Maximum number of letters displayed for each directory in the path')
|
||||
arg_parser.add_argument('--colorize-hostname', action='store_true',
|
||||
help='Colorize the hostname based on a hash of itself.')
|
||||
arg_parser.add_argument('--mode', action='store', default='patched',
|
||||
help='The characters used to make separators between segments',
|
||||
choices=['patched', 'compatible', 'flat'])
|
||||
arg_parser.add_argument('--shell', action='store', default='bash',
|
||||
help='Set this to your shell type', choices=['bash', 'zsh', 'bare'])
|
||||
help='Set this to your shell type',
|
||||
choices=['bash', 'zsh', 'bare'])
|
||||
arg_parser.add_argument('prev_error', nargs='?', type=int, default=0,
|
||||
help='Error code returned by the last command')
|
||||
help='Error code returned by the last command')
|
||||
args = arg_parser.parse_args()
|
||||
with open("config.json") as f:
|
||||
|
||||
config_path = find_config()
|
||||
if not config_path:
|
||||
warn("No config found")
|
||||
return 1
|
||||
with open(config_path) as f:
|
||||
config = json.loads(f.read())
|
||||
powerline = Powerline(args, get_valid_cwd())
|
||||
|
||||
powerline = Powerline(args, config)
|
||||
segments = []
|
||||
for seg_name in config["segments"]:
|
||||
mod = importlib.import_module("powerline_shell.segments." + seg_name)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ except ImportError:
|
|||
from hashlib import md5
|
||||
import sys
|
||||
from .colortrans import *
|
||||
from ..utils import py3
|
||||
from .utils import py3
|
||||
|
||||
|
||||
def getOppositeColor(r,g,b):
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ from ..utils import warn, py3, BasicSegment
|
|||
ELLIPSIS = u'\u2026'
|
||||
|
||||
|
||||
def _mode(powerline):
|
||||
return powerline.segment_conf("cwd", "mode", "fancy")
|
||||
|
||||
|
||||
def replace_home_dir(cwd):
|
||||
home = os.getenv('HOME')
|
||||
if cwd.startswith(home):
|
||||
|
|
@ -34,8 +38,9 @@ def maybe_shorten_name(powerline, name):
|
|||
"""If the user has asked for each directory name to be shortened, will
|
||||
return the name up to their specified length. Otherwise returns the full
|
||||
name."""
|
||||
if powerline.args.cwd_max_dir_size:
|
||||
return name[:powerline.args.cwd_max_dir_size]
|
||||
max_size = powerline.segment_conf("cwd", "max_dir_size")
|
||||
if max_size:
|
||||
return name[:max_size]
|
||||
return name
|
||||
|
||||
|
||||
|
|
@ -57,15 +62,15 @@ def add_cwd_segment(powerline):
|
|||
cwd = cwd.decode("utf-8")
|
||||
cwd = replace_home_dir(cwd)
|
||||
|
||||
if powerline.args.cwd_mode == 'plain':
|
||||
if _mode(powerline) == 'plain':
|
||||
powerline.append(' %s ' % (cwd,), powerline.theme.CWD_FG, powerline.theme.PATH_BG)
|
||||
return
|
||||
|
||||
names = split_path_into_names(cwd)
|
||||
|
||||
max_depth = powerline.args.cwd_max_depth
|
||||
max_depth = powerline.segment_conf("cwd", "max_depth", 5)
|
||||
if max_depth <= 0:
|
||||
warn("Ignoring --cwd-max-depth argument since it's not greater than 0")
|
||||
warn("Ignoring cwd.max_depth option since it's not greater than 0")
|
||||
elif len(names) > max_depth:
|
||||
# https://github.com/milkbikis/powerline-shell/issues/148
|
||||
# n_before is the number is the number of directories to put before the
|
||||
|
|
@ -77,7 +82,7 @@ def add_cwd_segment(powerline):
|
|||
n_before = 2 if max_depth > 2 else max_depth - 1
|
||||
names = names[:n_before] + [ELLIPSIS] + names[n_before - max_depth:]
|
||||
|
||||
if (powerline.args.cwd_mode == 'dironly' or powerline.args.cwd_only):
|
||||
if _mode(powerline) == "dironly":
|
||||
# The user has indicated they only want the current directory to be
|
||||
# displayed, so chop everything else off
|
||||
names = names[-1:]
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
from ..utils import BasicSegment
|
||||
from ..color_compliment import stringToHashToColorAndOpposite
|
||||
from ..colortrans import rgb2short
|
||||
from socket import gethostname
|
||||
|
||||
|
||||
class Segment(BasicSegment):
|
||||
def add_to_powerline(self):
|
||||
powerline = self.powerline
|
||||
if powerline.args.colorize_hostname:
|
||||
from lib.color_compliment import stringToHashToColorAndOpposite
|
||||
from lib.colortrans import rgb2short
|
||||
from socket import gethostname
|
||||
if powerline.segment_conf("hostname", "colorize"):
|
||||
hostname = gethostname()
|
||||
FG, BG = stringToHashToColorAndOpposite(hostname)
|
||||
FG, BG = (rgb2short(*color) for color in [FG, BG])
|
||||
|
|
|
|||
Loading…
Reference in a new issue