Skip to content

Instantly share code, notes, and snippets.

@OneOfOne
Last active July 30, 2020 02:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OneOfOne/97b03541ddee193407c9445c2d77689a to your computer and use it in GitHub Desktop.
Save OneOfOne/97b03541ddee193407c9445c2d77689a to your computer and use it in GitHub Desktop.
my ~/.xonshrc
from builtins import __xonsh_env__
from subprocess import check_output, PIPE, CalledProcessError
from xonsh.tools import get_sep, XonshError
from os import listdir
from os.path import exists as pexists, join as pjoin, isfile
from collections import defaultdict, OrderedDict
def _branch_status(cwd=None):
branch = None
dirty = 0
try:
cmd = !(git status --porcelain -b)
if cmd.stderr and cmd.stderr.getvalue(): return ''
s = cmd.stdout.getvalue().decode('utf-8')
s = s.strip().split('\n')
if len(s) == 0 or not s[0]:
return ''
branch = s[0].split()[1]
dirty = len(s) - 1
except (CalledProcessError, FileNotFoundError):
return
bc = ('{BOLD_INTENSE_RED}' if dirty else '{BOLD_INTENSE_GREEN}')
return '❨{NO_COLOR}' + bc + branch + '{NO_COLOR}|' + bc + ('+' + str(dirty) if dirty else '✔') + '{NO_COLOR}❩ '
def _shorter_cwd():
pwd = $PWD
try:
gopath = pjoin($GOPATH[0], 'src')
ghpath = pjoin(gopath, 'github.com')
if pwd.startswith(ghpath):
pwd = pwd.replace(ghpath, '✪', 1)
elif pwd.startswith(gopath):
pwd = pwd.replace(gopath, '☯', 1)
except:
pass
if pwd.startswith($HOME):
pwd = pwd.replace($HOME, '~', 1)
sep = get_sep()
pwd = pwd.split(sep)
l = len(pwd)
leader = sep if l>0 and len(pwd[0])==0 else ''
base = [i[0] if ix != l-1 else i for ix,i in enumerate(pwd) if len(i) > 0]
return leader + sep.join(base)
def _go_init():
gopath = pjoin($HOME, 'code/go')
if not pexists(gopath):
return
gobin = pjoin(gopath, 'bin')
if not gobin in $PATH:
$PATH.append(gobin)
$GOPATH = gopath
aliases['gotip'] = '/usr/src/go/bin/go'
aliases['go18'] = '/usr/src/go1.8/bin/go'
aliases['go17'] = '/usr/src/go1.7/bin/go'
aliases['go16'] = '/usr/src/go1.6/bin/go'
aliases['go15'] = '/usr/src/go1.5/bin/go'
aliases['go14'] = '/usr/src/go1.4/bin/go'
aliases['gow64'] = 'env GOOS="windows" GOARCH="amd64" CGO_ENABLED="1" CC="x86_64-w64-mingw32-gcc" CXX="x86_64-w64-mingw32-g++" go'
aliases['gow32'] = 'env GOOS="windows" GOARCH="386" CGO_ENABLED="1" CC="i686-w64-mingw32-gcc" CXX="i686-w64-mingw32-g++" go'
aliases['cdgh'] = cder(pjoin(gopath, 'src/github.com'), "cdgh")
aliases['cdooo'] = cder(pjoin(gopath, 'src/github.com/OneOfOne'), "cdooo")
aliases['setgo'] = _set_go
return True
def _set_go(v, args = None):
if len(v) == 0:
go version
return
v = v[0]
p = '/usr/src/go' + (v if v != 'tip' else '') + '/bin/go'
if not pexists(p):
print(p, 'does not exist')
return
ln -svf @(p) '$HOME/bin/go'
go version
def _rebuildgo(args, stdin):
ver = args[0] if len(args) == 1 else ''
pushd @('/usr/src/go' + ver + '/src')
try:
rm -rf ../pkg/ ../bin/ '$GOPATH/pkg/' a> /dev/null
git reset --hard || return
git clean -fdx || return
git pull -v || return
env GOROOT_BOOTSTRAP=/usr/src/go1.4 CC=clang GOGC=off bash make.bash || return
except (XonshError):
pass
finally:
popd
def _rebuildgotools(args, stdin):
killall -9 gocode a> /dev/null
gorepos = ['golang.org/x/{}/...'.format(n) for n in $(ls @($GOPATH[0] + '/src/golang.org/x/')).strip().split('\n')]
#parallel --will-cite -v -j8 go get -u ::: @(gorepos) github.com/nsf/gocode github.com/derekparker/delve/... github.com/lukehoban/... github.com/rogpeppe/godef
go get -u @(' '.join(args)) @(gorepos) github.com/nsf/gocode github.com/derekparker/delve/... github.com/lukehoban/... github.com/rogpeppe/godef
class adict(dict):
def __init__(self, *args, **kw):
super(adict, self).__init__(*args, **kw)
def __setattr__(self, key, val):
return dict.__setitem__(self, key, val)
def __getattr__(self, key):
return self.__getitem__(key)
def __getitem__(self, key):
try:
val = dict.__getitem__(self, key)
if callable(val):
val = val()
return ('' if val is None else val)
except (KeyError):
return '{' + key + '}'
def _userPrompt():
out = '{ret_code_color}┏━{NO_COLOR}{env_name}'
out = out + ('{BOLD_RED}' if $PROMPT_FIELDS['user'] == 'root' else '{BOLD_YELLOW}') + '{user}'
out = out + '{NO_COLOR}@{BOLD_CYAN}{hostname}{NO_COLOR} '
#out = out + '❨{BOLD_GREEN}{shorter_cwd}{NO_COLOR}❩ {branch_status}'
out = out + '❨{BOLD_GREEN}{shorter_cwd}{NO_COLOR}❩{gitstatus: ❨{}❩ }'
out = out + '{NO_COLOR}{ret_code_color}{ret_code}\n'
out = out + '{ret_code_color}┗━━{NO_COLOR}➤{NO_COLOR} '
$TITLE = '{current_job} ' + _shorter_cwd()
return out
def _pss(args, stdin):
import re
args = '|'.join(args)
for line in $(ps -eo pid:1,user,vsize,cmd).split('\n'):
p = re.split(r'\s+', line)
if len(p) < 4 or p[2] == '0' or p[3] == '/usr/bin/ps':
continue
p[0] = '\033[94m' + p[0] + '\033[0m'
p[1] = '\033[92m' + p[1] + '\033[0m'
p[3] = '\033[94m' + p[3] + '\033[0m'
line = ' '.join(p[3:])
if args:
ln = len(line)
line = re.sub(r'(' + args + r')', r'\033[1;31m\1\033[0m', line, re.I)
if len(line) != ln:
print(p[0], p[1], line)
else:
print(p[0], p[1], line)
def _path_completer(cmd, path):
def cb(prefix, line, begidx, endidx, ctx):
if not line.startswith(cmd):
return
args = prefix.split('/')
if len(args) > 1:
p = pjoin(path, args[0])
return {pjoin(args[0], f) for f in listdir(p) if not isfile(pjoin(p, f)) and f.startswith(args[1])}
return {f for f in listdir(path) if not isfile(pjoin(path, f)) and f.startswith(args[0])}
return cb
def cder(path, alias = None):
if alias:
__xonsh_completers__[alias] = _path_completer(alias, path)
__xonsh_completers__.move_to_end(alias, last=False)
#completer add @(path) _path_completer(@(path))
def cb(args, stdin=None):
echo @(pjoin(path, *args))
cd @(pjoin(path, *args))
return cb
def _alias(args, stdin):
if len(args) == 0:
return print(aliases)
if len(args) == 1:
return print(aliases.get(args[0], ['']))
aliases[args[0]] = args[1]
_go_init()
$PROMPT_FIELDS['shorter_cwd'] = _shorter_cwd
$XONSH_COLOR_STYLE='native'
$PATH = OrderedDict.fromkeys($PATH).keys()
$PROMPT = _userPrompt
$MULTILINE_PROMPT=''
$PUSHD_SILENT = True
$HISTCONTROL = set('ignoredups')
$AUTO_CD = True
$COMPLETIONS_CONFIRM = True
$UPDATE_COMPLETIONS_ON_KEYPRESS = True
$XONSH_CACHE_SCRIPTS = False
$AUTO_SUGGEST = False
# yaourt / arch aliases
aliases['y'] = 'nice -n20 ionice -c3 yaourt'
aliases['yup'] = 'y -Syu --aur'
aliases['yr'] = 'y -Rscd'
aliases['yi'] = 'y -S'
aliases['ys'] = 'y -Ss'
aliases['rm'] = 'rm -i'
aliases['mv'] = 'mv -i'
aliases['cp'] = 'cp --reflink=auto --sparse=always -ia'
aliases['pss'] = _pss
aliases['rebuildgo'] = _rebuildgo
aliases['rebuildgotools'] = _rebuildgotools
aliases['alias'] = _alias
del _pss
del _rebuildgo
del _rebuildgotools
del _go_init
del _alias
del _userPrompt
$LESS = '-Xr'
$LESSOPEN = '|/usr/bin/lesspipe.sh %s'
$LESS_ADVANCED_PREPROCESSOR = 1
$WINEDEBUG = '-all'
$WINEDLLOVERRIDES = "winemenubuilder.exe=d"
$SDL_AUDIODRIVER = 'pulse'
try:
$EDITOR = ('subl3 -n -w' if $DISPLAY else 'nvim')
except (Exception):
$EDITOR = 'nvim'
@OneOfOne
Copy link
Author

OneOfOne commented Jun 5, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment