Skip to content

Commit

Permalink
Shells: Revamp aliases and ENV system
Browse files Browse the repository at this point in the history
- Use Python for parsing `addpath` file
- Move the script to ~/dotscripts
- Support Nu for aliases
- Add notes on installing Nu and setting up bash
- Update setup script for fish
  • Loading branch information
hedyhli committed May 5, 2024
1 parent c1e5e63 commit bda9700
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 64 deletions.
11 changes: 5 additions & 6 deletions .aliases
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#!/usr/bin/env bash

# In addition to this file being valid POSIX, all RHS of `=` must use single
# quotes, and must be convertible to nu and fish (also see ~/dotscripts/gen/nu
# and check ~/.aliases.nu)

# Program aliases
alias x=exit
alias clr=clear
alias y=yadm
alias td=termdown
alias hdi=howdoi
alias bom=bombadillo
alias syscu='systemctl --user'
alias edoom='emacs --with-profile=doom'
alias g=git
alias acme='acme -f /mnt/font/FiraCode-Regular/15/font'
alias la='ls -Ah'
alias la='ls -a'

# Shortcuts
alias apt-up='sudo apt update && sudo apt upgrade -y && sudo apt-get dist-upgrade -y && sudo apt autoremove'
alias newvenv='python3 -m virtualenv venv && source ./venv/bin/activate.fish'
alias localusrlocal='rsync ~/local/usr/local/ ~/local/ -avr && rm -rf ~/local/usr/local'
alias localusr='rsync ~/local/usr/ ~/local/ -avr && rm -rf ~/local/usr'

12 changes: 11 additions & 1 deletion .startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ elif test -n "$KSH_VERSION"; then
CURSHELL=ksh
elif test -n "$FCEDIT"; then
CURSHELL=ksh
elif test -n "$NU_VERISON"; then
CURSHELL=nu
elif test -n "$PS3"; then
CURSHELL=unknown
else
CURSHELL=sh
fi

# Aliases
source ~/.aliases

# Add the paths
export PATH="$(~/bin/parse_addpath | sed 's/ /:/g'):$PATH"
export PATH="$(~/dotscripts/convert/addpath):$PATH"

# Hook (the) direnv
if ! command -v direnv &> /dev/null; then
Expand All @@ -36,3 +41,8 @@ fi
# Load (the) nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$HOME/.exportenvs" ] && \. "$HOME/.exportenvs"

# Pyenv
if ! command -v pyenv &> /dev/null; then
eval "$(pyenv init -)"
fi
24 changes: 0 additions & 24 deletions bin/parse_addpath

This file was deleted.

42 changes: 42 additions & 0 deletions dotscripts/convert/addpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
# read addpath file and return each path

import sys
from pathlib import Path

home = str(Path.home())
added = {}

def getpaths(file: str):
global added
addpaths = Path(file)
if not addpaths.exists():
return

lines = addpaths.read_text(encoding="utf-8").splitlines()
for line in lines:
path = line.strip()
if not path:
continue
if path.startswith("#"):
continue
if not path.startswith("/"):
path = f"{home}/{path}"
if not added.get(path, False):
added[path] = True
yield path


sep = ":"
if len(sys.argv) > 1:
sep = sys.argv[1]

######
file = f"{home}/.addpath"
print(
f"{home}/bin",
f"{home}/local/bin",
*getpaths(file),
*getpaths(file+"_local"),
sep=sep
)
18 changes: 18 additions & 0 deletions dotscripts/gen/fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# Stubbornly translates ~/.exportenvs to ~/.exportenvs.fish

input=~/.exportenvs
output="$input.fish"

# clear the file
rm $output
touch $output

while read fileline; do
line=$(echo "$fileline" | sed 's/#.*//' | sed 's/export //')
if [ -z "$line" ]; then
continue
fi
tline=$(echo "$line" | sed 's/=/ /')
echo "set -x $tline" >> $output
done < $input
25 changes: 0 additions & 25 deletions dotscripts/gen/fish-exportenvs

This file was deleted.

30 changes: 30 additions & 0 deletions dotscripts/gen/nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env nu

# Aliases
let file = $"($env.HOME)/.aliases.nu"
if ($"($env.HOME)/.aliases" | path exists) {
'' | save -f $file
for $line in (cat ~/.aliases | lines) {
if ($line | str starts-with "alias") {
let parts = ($line | str substring 6.. | split row '=' )
let name = $parts.0
mut cmd = ($parts.1 | str replace -a "'" '' | str replace -a " && " "; ")
mut stmt = ""
if ($cmd | str contains "; ") {
# RHS of alias command must be a valid command. If there are
# more commands, use a def instead.
if ($cmd | split words | $in.0) == $name {
$cmd = $"^($cmd)"
}
$stmt = $"
# Alias of '($cmd)'
def --env ($name) [] {
($cmd)
}"
} else {
$stmt = $"alias ($name) = ($cmd)"
}
$"($stmt)\n" | save -a $file
}
}
}
14 changes: 14 additions & 0 deletions dotscripts/install/nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

echo https://www.nushell.sh/book/installation.html
echo
echo '
sudo cat <<END
#!/bin/sh
XDG_CONFIG_HOME=~/.config /opt/homebrew/bin/nu "$@"
END > /usr/local/bin/nu
sudo chmod +x /usr/local/bin/nu
'

echo add it to /etc/shells
13 changes: 13 additions & 0 deletions dotscripts/setup/bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

echo Ensure .startup.sh is sourced, and make sure it is only sourced ONCE
echo '(Otherwise $PATH precedence will be messed up)'
echo
echo Run these yourself:
echo
echo sudo echo "'"'[ -r "$HOME/.startup.sh" ] && . "$HOME/.startup.sh"'"'" '| tee -a /etc/profile >> /etc/bashrc'
echo 'cat <<END
if ! grep "startup.sh" /etc/bashrc > /dev/null; then
source ~/.startup.sh
fi
END | tee -a ~/.profile >> ~/.bashrc'
13 changes: 5 additions & 8 deletions dotscripts/setup/fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

PATH=~/bin:$PATH

setpaths() {
fish -c 'set -Ux fish_user_paths $fish_user_paths '"$(parse_addpath)"
}

if ! command -v fish &> /dev/null; then
echo "I'm sorry but I can't help you install fish"
echo "Please install fish before running this script lol"
exit 1
fi

echo "Setting up fish_user_paths..."
fish -c 'set -Ux fish_user_paths' # clear the variable first
setpaths
fish -c "set -Ux fish_user_paths $(~/dotscripts/convert/addpath ' ')"
echo "done"

echo Maybe add ~/local/share/man to manpath but I forgot how as of writing

# env
echo "Generating ~/.exportenvs.fish"
~/dotscripts/gen/fish-exportenvs > /dev/null
echo "Refreshing generated fish configs"
~/dotscripts/gen/fish > /dev/null
echo -n source ~/.exportenvs.fish | pbcopy
echo PLEASE DO: source ~/.exportenvs.fish '(copied to clipboard)'

# TODO have a better check for omf existence (command -v doesnt work)
# Although omf does this same check lol
Expand Down
6 changes: 6 additions & 0 deletions dotscripts/setup/nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env nu

print "Re-generating config files"
~/dotscripts/gen/nu

print "Please reload the shell"

0 comments on commit bda9700

Please sign in to comment.