Skip to content

Commit

Permalink
wsl goodies for bash
Browse files Browse the repository at this point in the history
  • Loading branch information
gzagatti committed Sep 18, 2024
1 parent 3387174 commit b1f0b2b
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions shells/bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ if [[ $- == *i* ]]; then

# Determine OS type {{{
export OSTYPE="$(uname -s)"
if [[ "$OSTYPE" = Darwin* ]]; then
# macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST="${HOSTNAME/.*/}"
else
# https://superuser.com/questions/1749781/how-can-i-check-if-the-environment-is-wsl-from-a-shell-script
if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
export OSTYPE="Wsl"
fi
SHORT_HOST="${HOSTNAME/.*/}"
fi
# }}}

# History {{{
Expand All @@ -15,30 +25,9 @@ if [[ $- == *i* ]]; then
# }}}

# Theme {{{
# https://gist.github.com/thomd/7667642
# https://geoff.greer.fm/lscolors/
function get_termbg() {
if [[ "$TERM" == "xterm-"* ]] || [[ "$TERM" == "alacritty" ]]; then
oldstty=$(stty -g)
stty raw -echo min 0 time 1
# terminal escape sequence to get the terminal background color
# only works in xterms
# https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
printf '\033]11;?\033\\'
sleep 0.00000001
read -r termbg
stty $oldstty
local termbg=$(echo $termbg | sed 's/.*\(rgb:[0-9a-f/]*\).*/\1/')
if [[ $termbg == "rgb:f8f8/fbfb/f8f8" ]]; then
export THEME="leuven"
elif [[ $termbg == "rgb:2828/2a2a/3636" ]]; then
export THEME="dracula"
fi
fi
}
if [[ -z "$THEME" ]]; then export THEME=""; fi
get_termbg
if [[ -z "$THEME" ]]; then export THEME="leuven"; fi
export CLICOLOR=1
# https://geoff.greer.fm/lscolors/
export LSCOLORS="excxhxDxbxhxhxhxhxfxfx"
export LS_COLORS=""
if [[ $THEME == "leuven" ]]; then
Expand Down Expand Up @@ -274,15 +263,15 @@ if [[ $- == *i* ]]; then
alias ls='ls --color=auto'
## }}}

if [[ $OSTYPE == Linux* ]]; then
if [[ "$OSTYPE" = Linux* ]]; then

## More convenient xdg-open {{{
function open { setsid xdg-open "$1" &>/dev/null; }
## }}}

fi

if [[ $OSTYPE == Darwin* ]]; then
if [[ "$OSTYPE" = Darwin* ]]; then

## turn on/off hidden files visibility {{{
alias showFiles='defaults write com.apple.finder AppleShowAllFiles YES; killall Finder /System/Library/CoreServices/Finder.app'
Expand All @@ -299,6 +288,20 @@ if [[ $- == *i* ]]; then

fi

if [[ "$OSTYPE" = Wsl* ]]; then

## More convenient explorer.exe {{{
function open {
win_path=$(wslpath -w "$1")
explorer.exe "$win_path" &>/dev/null
(( $? > 1 )) && return 1 || return 0
}

export WINHOME=$(wslpath -u $(cmd.exe /c "echo %USERPROFILE%" 2>&1 | tail -n 1 | tr -d '\r'))
## }}}

fi

# }}}

# PATH {{{
Expand Down

0 comments on commit b1f0b2b

Please sign in to comment.