Add bass and change theme to l #fishshell
This commit is contained in:
98
config/fish/functions/__bass.py
Normal file
98
config/fish/functions/__bass.py
Normal file
@ -0,0 +1,98 @@
|
||||
"""
|
||||
To be used with a companion fish function like this:
|
||||
|
||||
function refish
|
||||
set -l _x (python /tmp/bass.py source ~/.nvm/nvim.sh ';' nvm use iojs); source $_x; and rm -f $_x
|
||||
end
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
|
||||
BASH = 'bash'
|
||||
|
||||
def comment(string):
|
||||
return '\n'.join(['# ' + line for line in string.split('\n')])
|
||||
|
||||
def gen_script():
|
||||
divider = '-__-__-__bass___-env-output-__bass_-__-__-__-__'
|
||||
|
||||
# Use the following instead of /usr/bin/env to read environment so we can
|
||||
# deal with multi-line environment variables (and other odd cases).
|
||||
env_reader = "python -c 'import os,json; print(json.dumps({k:v for k,v in os.environ.items()}))'"
|
||||
args = [BASH, '-c', env_reader]
|
||||
output = subprocess.check_output(args, universal_newlines=True)
|
||||
old_env = output.strip()
|
||||
|
||||
command = '{} && (echo "{}"; {}; echo "{}"; alias)'.format(
|
||||
' '.join(sys.argv[1:]).rstrip().rstrip(';'),
|
||||
divider,
|
||||
env_reader,
|
||||
divider,
|
||||
)
|
||||
args = [BASH, '-c', command]
|
||||
output = subprocess.check_output(args, universal_newlines=True)
|
||||
stdout, new_env, alias = output.split(divider, 2)
|
||||
new_env = new_env.strip()
|
||||
|
||||
old_env = json.loads(old_env)
|
||||
new_env = json.loads(new_env)
|
||||
|
||||
script_lines = []
|
||||
|
||||
for line in stdout.splitlines():
|
||||
# some outputs might use documentation about the shell usage with dollar signs
|
||||
line = line.replace(r'$', r'\$')
|
||||
script_lines.append("printf %s;printf '\\n'" % json.dumps(line))
|
||||
for k, v in new_env.items():
|
||||
if k in ['PS1', 'SHLVL', 'XPC_SERVICE_NAME'] or k.startswith("BASH_FUNC"):
|
||||
continue
|
||||
v1 = old_env.get(k)
|
||||
if not v1:
|
||||
script_lines.append(comment('adding %s=%s' % (k, v)))
|
||||
elif v1 != v:
|
||||
script_lines.append(comment('updating %s=%s -> %s' % (k, v1, v)))
|
||||
# process special variables
|
||||
if k == 'PWD':
|
||||
script_lines.append('cd %s' % json.dumps(v))
|
||||
continue
|
||||
else:
|
||||
continue
|
||||
if k == 'PATH':
|
||||
# use json.dumps to reliably escape quotes and backslashes
|
||||
value = ' '.join([json.dumps(directory)
|
||||
for directory in v.split(':')])
|
||||
else:
|
||||
# use json.dumps to reliably escape quotes and backslashes
|
||||
value = json.dumps(v)
|
||||
script_lines.append('set -g -x %s %s' % (k, value))
|
||||
|
||||
for var in set(old_env.keys()) - set(new_env.keys()):
|
||||
script_lines.append(comment('removing %s' % var))
|
||||
script_lines.append('set -e %s' % var)
|
||||
|
||||
script = '\n'.join(script_lines)
|
||||
|
||||
return script + '\n' + alias
|
||||
|
||||
if not sys.argv[1:]:
|
||||
print('__usage', end='')
|
||||
sys.exit(0)
|
||||
|
||||
try:
|
||||
script = gen_script()
|
||||
except subprocess.CalledProcessError as e:
|
||||
print('exit code:', e.returncode, file=sys.stderr)
|
||||
print('__error', e.returncode, end='')
|
||||
except Exception as e:
|
||||
print('unknown error:', str(e), file=sys.stderr)
|
||||
traceback.print_exc(10, file=sys.stderr)
|
||||
print('__error', end='')
|
||||
else:
|
||||
print(script, end='')
|
||||
27
config/fish/functions/bass.fish
Normal file
27
config/fish/functions/bass.fish
Normal file
@ -0,0 +1,27 @@
|
||||
function bass
|
||||
set __bash_args $argv
|
||||
if test "$__bash_args[1]_" = '-d_'
|
||||
set __bass_debug
|
||||
set -e __bash_args[1]
|
||||
end
|
||||
|
||||
python (dirname (status -f))/__bass.py $__bash_args | read -z __script
|
||||
set __errorflag (string sub -s 1 -l 7 "$__script")
|
||||
if test "$__script" = '__usage'
|
||||
echo "Usage: bass [-d] <bash-command>"
|
||||
else if test "x$__errorflag" = 'x__error'
|
||||
echo "Bass encountered an error!"
|
||||
set __exitcode (string sub -s 9 "$__script")
|
||||
set __exitcode (string trim $__exitcode)
|
||||
if test -z $__exitcode
|
||||
return 1
|
||||
else
|
||||
return $__exitcode
|
||||
end
|
||||
else
|
||||
echo -e "$__script" | source -
|
||||
if set -q __bass_debug
|
||||
echo "$__script"
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,15 +0,0 @@
|
||||
function fish_mode_prompt
|
||||
if test "$fish_key_bindings" = "fish_vi_key_bindings"
|
||||
or test "$fish_key_bindings" = "fish_hybrid_key_bindings"
|
||||
switch $fish_bind_mode
|
||||
case default
|
||||
prompt_segment blue black "N"
|
||||
case insert
|
||||
prompt_segment green black "I"
|
||||
case replace_one
|
||||
prompt_segment red black "R"
|
||||
case visual
|
||||
prompt_segment magenta black "V"
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,29 +1,37 @@
|
||||
# To show the right prompt please set
|
||||
# set theme_display_rbenv 'yes' (config.fish)
|
||||
# set theme_display_rbenv_gemset 'yes' (config.fish)
|
||||
# set theme_display_rbenv_with_gemfile_only 'yes' (config.fish)
|
||||
|
||||
function get_git_status -d "Gets the current git status"
|
||||
if command git rev-parse --is-inside-work-tree >/dev/null 2>&1
|
||||
set -l dirty (command git status -s --ignore-submodules=dirty | wc -l | sed -e 's/^ *//' -e 's/ *$//' 2> /dev/null)
|
||||
set -l ref (command git describe --tags --exact-match ^/dev/null ; or command git symbolic-ref --short HEAD 2> /dev/null ; or command git rev-parse --short HEAD 2> /dev/null)
|
||||
|
||||
if [ "$dirty" != "0" ]
|
||||
set_color -b normal
|
||||
set_color red
|
||||
echo "$dirty changed file"
|
||||
if [ "$dirty" != "1" ]
|
||||
echo "s"
|
||||
end
|
||||
echo " "
|
||||
set_color -b red
|
||||
set_color white
|
||||
else
|
||||
set_color -b cyan
|
||||
set_color white
|
||||
end
|
||||
|
||||
echo " $ref "
|
||||
set_color normal
|
||||
end
|
||||
function _ruby_version
|
||||
echo (command rbenv version-name | sed 's/\n//')
|
||||
end
|
||||
|
||||
function fish_right_prompt -d "Prints right prompt"
|
||||
get_git_status
|
||||
end
|
||||
function _ruby_gemset
|
||||
echo (command rbenv gemset active ^/dev/null | sed -e 's| global||')
|
||||
end
|
||||
|
||||
function fish_right_prompt
|
||||
if [ "$theme_display_rbenv" = 'yes' ]
|
||||
set -l red (set_color red)
|
||||
set -l normal (set_color normal)
|
||||
set ruby_info $red(_ruby_version)
|
||||
|
||||
if [ "$theme_display_rbenv_gemset" = 'yes' ]
|
||||
if [ (_ruby_gemset) ]
|
||||
set -l ruby_gemset $red(_ruby_gemset)
|
||||
set ruby_info "$ruby_info@$ruby_gemset"
|
||||
end
|
||||
end
|
||||
|
||||
if [ "$theme_display_rbenv_with_gemfile_only" = 'yes' ]
|
||||
if test -f Gemfile
|
||||
echo -n -s $ruby_info $normal
|
||||
else
|
||||
echo -n -s $normal
|
||||
end
|
||||
else
|
||||
echo -n -s $ruby_info $normal
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user