From 68d903d628e13d03c13db452595fe4651b38ff9c Mon Sep 17 00:00:00 2001 From: Tim McCarthy Date: Thu, 6 Jan 2022 16:50:56 -0800 Subject: [PATCH] Refactor xonsh config --- xonsh/alias.xsh | 11 +++++ xonsh/java.xsh | 4 ++ xonsh/linux.xsh | 4 ++ xonsh/nix.xsh | 11 +++++ xonsh/path.xsh | 10 ++++ xonsh/prompt.xsh | 25 ++++++++++ xonsh/python.xsh | 17 +++++++ xonsh/rc.xsh | 102 ++++++----------------------------------- xonsh/requirements.txt | 3 +- 9 files changed, 96 insertions(+), 91 deletions(-) create mode 100644 xonsh/alias.xsh create mode 100644 xonsh/java.xsh create mode 100644 xonsh/linux.xsh create mode 100644 xonsh/nix.xsh create mode 100644 xonsh/path.xsh create mode 100644 xonsh/prompt.xsh create mode 100644 xonsh/python.xsh diff --git a/xonsh/alias.xsh b/xonsh/alias.xsh new file mode 100644 index 0000000..d57f4c1 --- /dev/null +++ b/xonsh/alias.xsh @@ -0,0 +1,11 @@ +aliases["-"] = "cd -" +aliases[".."] = "cd .." +aliases["..."] = "cd ../.." + +aliases["ec"] = "emacsclient -n --alternate-editor=vim" + +aliases["src"] = "source ~/.xonshrc" + +aliases["pubip"] = "curl icanhazip.com" + +aliases["ll"] = lambda args: $[$LC_COLLATE='C' ls -lAh @(args)] diff --git a/xonsh/java.xsh b/xonsh/java.xsh new file mode 100644 index 0000000..8bb0eb2 --- /dev/null +++ b/xonsh/java.xsh @@ -0,0 +1,4 @@ +from xonsh.platform import ON_DARWIN + +if ON_DARWIN: + $JAVA_HOME = $(/usr/libexec/java_home).strip() diff --git a/xonsh/linux.xsh b/xonsh/linux.xsh new file mode 100644 index 0000000..d0c5b31 --- /dev/null +++ b/xonsh/linux.xsh @@ -0,0 +1,4 @@ +from xonsh.platform import ON_LINUX + +if ON_LINUX: + aliases["pulsefix"] = "pulseaudio --kill; pulseaudio --start" diff --git a/xonsh/nix.xsh b/xonsh/nix.xsh new file mode 100644 index 0000000..2be7eb0 --- /dev/null +++ b/xonsh/nix.xsh @@ -0,0 +1,11 @@ +from xonsh.platform import ON_DARWIN + +# Nix integration +if ON_DARWIN: + nix_daemon_path = p"/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" + if nix_daemon_path.exists(): + source-bash @(nix_daemon_path) + + nix_darwin_path = p"/etc/static/bashrc" + if nix_darwin_path.exists(): + source-bash @(nix_darwin_path) diff --git a/xonsh/path.xsh b/xonsh/path.xsh new file mode 100644 index 0000000..8366ae1 --- /dev/null +++ b/xonsh/path.xsh @@ -0,0 +1,10 @@ +from pathlib import Path +from typing import List + +def ensure_path(*paths: List[str]): + for p in paths: + abs_p = str(Path(p).expanduser().absolute()) + if abs_p not in $PATH: + $PATH.insert(0, abs_p) + +ensure_path("/usr/local/bin", "~/.dotfiles/bin", "~/.local/bin", "~/bin", "~/.emacs.d/bin") diff --git a/xonsh/prompt.xsh b/xonsh/prompt.xsh new file mode 100644 index 0000000..a769c9d --- /dev/null +++ b/xonsh/prompt.xsh @@ -0,0 +1,25 @@ +# Fix prompt for Emacs TRAMP +# https://github.com/oh-my-fish/theme-bobthefish/issues/148 +if $TERM == "dumb": + $PROMPT = "$ " + $RIGHT_PROMPT = None + $BOTTOM_TOOLBAR = None +else: + $PROMPT = \ + "{bg#2E3440}{RED}{localtime} " \ + "{#5E6470}\ue0b1 {YELLOW}{user}{DEFAULT}@{GREEN}{hostname} " \ + "{#5E6470}\ue0b1 {BLUE}{short_cwd} {DEFAULT}{env_name}{RESET}{#2E3440}\ue0b0\n" \ + "{RESET}{INTENSE_GREEN}{prompt_end}{RESET} " + +# Color style to fix unreadable greys +from xonsh.tools import register_custom_style +ttm_style = { + # Put custom overrides here + # Discover list of styles by running "xonfig colors default" + # Discover default mappings by cross referencing + # https://github.com/xonsh/xonsh/blob/main/xonsh/style_tools.py + # and + # https://github.com/pygments/pygments/blob/master/pygments/styles/native.py +} +register_custom_style("ttm-style", ttm_style, base="native") +$XONSH_COLOR_STYLE = "ttm-style" diff --git a/xonsh/python.xsh b/xonsh/python.xsh new file mode 100644 index 0000000..ed818d2 --- /dev/null +++ b/xonsh/python.xsh @@ -0,0 +1,17 @@ +def _vv(): + """Finds the nearest venv going upward in the directory hierarchy and activates it.""" + d = Path.cwd() + + while True: + if (d / "venv").exists(): + vox activate @(str(d / "venv")) + return True + + if d == d.parent or (d / ".git").exists(): + print("Could not find venv.") + return False + + d = d.parent + + +aliases["vv"] = _vv diff --git a/xonsh/rc.xsh b/xonsh/rc.xsh index 750901d..2796100 100644 --- a/xonsh/rc.xsh +++ b/xonsh/rc.xsh @@ -1,115 +1,39 @@ -import shutil -from pathlib import Path -from typing import List -from xonsh.platform import ON_DARWIN, ON_LINUX - # Pip dependencies listed in requirements.txt +_xontribs = [ + "fzf-widgets", + "direnv", + "vox", +] $fzf_history_binding = "c-r" -_xontribs = [ - "vox", - "fzf-widgets", - # "powerline_binding", -] - if _xontribs: xontrib load @(_xontribs) -$UPDATE_OS_ENVIRON = True +$UPDATE_OS_ENVIRON = False $XONSH_SHOW_TRACEBACK = False $XONSH_HISTORY_BACKEND = "sqlite" $HISTCONTROL = "ignoredups" $MULTILINE_PROMPT = " " $UPDATE_PROMPT_ON_KEYPRESS = False -aliases["-"] = "cd -" -aliases[".."] = "cd .." -aliases["..."] = "cd ../.." $AUTO_CD = True $MANPAGER = "less -X" $LESS = "--ignore-case --quit-if-one-screen --QUIET --RAW-CONTROL-CHARS" -# ============ -# Basics, etc… -# ============ - -$EDITOR = "emacsclient" +$EDITOR = "vim" $ALTERNATE_EDITOR = "vim" $TERMINAL = "kitty" -def ensure_path(*paths: List[str]): - for p in paths: - abs_p = str(Path(p).expanduser().absolute()) - if abs_p not in $PATH: - $PATH.append(abs_p) - -ensure_path("/usr/local/bin", "~/.dotfiles/bin", "~/.local/bin", "~/bin", "~/.emacs.d/bin") - config_dir = p"~/.dotfiles/xonsh" +xsh_modules = ["prompt", "nix", "path", "alias", "java", "linux", "python", "local"] -if (config_dir / "local.xsh").exists(): - source @(config_dir / "local.xsh") - -aliases["ec"] = "emacsclient -n --alternate-editor=emacs" -aliases["src"] = "source ~/.xonshrc" -aliases["pubip"] = "curl icanhazip.com" -aliases["ll"] = lambda args: $[$LC_COLLATE='C' ls -lAh @(args)] - -if ON_DARWIN: - $JAVA_HOME = $(/usr/libexec/java_home).strip() - -if ON_LINUX: - aliases["pulsefix"] = "pulseaudio --kill; pulseaudio --start" - -def _vv(): - """Finds the nearest venv going upward in the directory hierarchy and activates it.""" - d = Path.cwd() - - while True: - if (d / "venv").exists(): - vox activate @(str(d / "venv")) - return True - - if d == d.parent or (d / ".git").exists(): - print("Could not find venv.") - return False - - d = d.parent - - -aliases["vv"] = _vv - -# # Hybrid Vi Mode -# set -g fish_key_bindings hybrid_bindings - -# Fix prompt for Emacs TRAMP -# https://github.com/oh-my-fish/theme-bobthefish/issues/148 -if not ${...}.get("TERM") or $TERM == "dumb": - $PROMPT = "$ " - $RIGHT_PROMPT = None - $BOTTOM_TOOLBAR = None -else: - $PROMPT = \ - "{bg#2E3440}{RED}{localtime} " \ - "{#5E6470}\ue0b1 {YELLOW}{user}{DEFAULT}@{GREEN}{hostname} " \ - "{#5E6470}\ue0b1 {BLUE}{short_cwd} {DEFAULT}{env_name}{RESET}{#2E3440}\ue0b0\n" \ - "{RESET}{INTENSE_GREEN}{prompt_end}{RESET} " - -# Color style to fix unreadable greys -from xonsh.tools import register_custom_style -ttm_style = { - # Put custom overrides here - # Discover list of styles by running "xonfig colors default" - # Discover default mappings by cross referencing - # https://github.com/xonsh/xonsh/blob/main/xonsh/style_tools.py - # and - # https://github.com/pygments/pygments/blob/master/pygments/styles/native.py -} -register_custom_style("ttm-style", ttm_style, base="native") -$XONSH_COLOR_STYLE = "ttm-style" +for module in xsh_modules: + _p = config_dir / f"{module}.xsh" + if _p.exists(): + source @(_p) # autojump equivalent -if shutil.which("zoxide"): +if !(which zoxide): execx($(zoxide init xonsh --cmd j --hook prompt), 'exec', __xonsh__.ctx, filename='zoxide') diff --git a/xonsh/requirements.txt b/xonsh/requirements.txt index 75bc47f..e746c66 100644 --- a/xonsh/requirements.txt +++ b/xonsh/requirements.txt @@ -1,4 +1,3 @@ xonsh[full] -xontrib-powerline-binding xontrib-fzf-widgets -powerline_gitstatus +xonsh-direnv