Skip to content

Instantly share code, notes, and snippets.

@summatix
Last active May 8, 2020 21:01
Show Gist options
  • Save summatix/b9a5787f0915a61fd7e5efcfde95dda6 to your computer and use it in GitHub Desktop.
Save summatix/b9a5787f0915a61fd7e5efcfde95dda6 to your computer and use it in GitHub Desktop.
Helper functions I use for ZSH and WSL running on Windows 10
#!/usr/bin/env bash
#
# Helper functions that are intended to be sourced from ~/.zshrc inside WSL for
# Windows.In ~/.zshrc, execute with:
#
# emulate bash -c "source ~/.bash_functions.sh"
#
# Allow GUI apps inside WSL to display inside the X Server running in Windows
DISPLAY=$(grep nameserver < /etc/resolv.conf | awk '{print $2}')
export DISPLAY="$DISPLAY:0"
# Adjust GUI scaling so things look nice on my Macbook Pro display
export GDK_SCALE=2
export QT_SCALE_FACTOR=1.25
# cd to the last saved directory
if [ -f ~/.last_dir ]; then
cd $(cat ~/.last_dir)
fi
#
# Overrides the cd function to save the directory to ~/.last_dir. This allows
# the last directory to be restored when a new terminal session opens.
#
function cd_ {
"cd" "$@"
result=$?
if [ $result -eq 0 ]; then
pwd > ~/.last_dir
fi
return $result
}
alias cd=cd_
#
# Expands the given directory to the full Windows path.
#
# Arguments:
# The path to expand.
# The return variable to save the expanded path to.
#
function expand_to_windows_path {
local __path=$1
local __result_return=$2
if [ -z "$__path" ]; then
__path=$(pwd)
else
__path=$(/bin/readlink -f "$__path")
fi
__path="//wsl\$/$WSL_DISTRO_NAME$__path"
if [[ "$__result_return" ]]; then
__path=$(/bin/sed 's/\//\\\\/g' <<< "$__path")
eval "$__result_return=$__path"
else
__path=$(/bin/sed 's/\//\\/g' <<< "$__path")
echo "$__path"
fi
}
#
# Expands the given directory to the full Network path in Windows. Assumes
# network is mounted to "Z:\"
#
# Arguments:
# The path to expand.
# The return variable to save the expanded path to.
#
function expand_to_network_path {
local __path=$1
local __result_return=$2
if [ -z "$__path" ]; then
__path=$(pwd)
else
__path=$(/bin/readlink -f "$__path")
fi
__path="Z:$__path"
if [[ "$__result_return" ]]; then
__path=$(/bin/sed 's/\//\\\\/g' <<< "$__path")
eval "$__result_return=$__path"
else
__path=$(/bin/sed 's/\//\\/g' <<< "$__path")
echo "$__path"
fi
}
#
# Adds a "git discard" function that clears all uncommitted changes from the
# repository.
#
function git {
if [ "$1" = "discard" ]; then
git stash save --keep-index && git stash drop
else
command git "$@"
fi
}
# Add a mirror function to upload a file to s3
#
# Uploads a file to our mirror S3 bucket.
#
# Arguments:
# The path to the file to upload to the mirror.
#
function mirror {
local path=$1
if [ -z "$path" ]; then
echo "You must provide a path to a file to upload" >&2
return 1
fi
local name
name=$(basename "$path")
if s3 put -P "$1" "s3://$S3_MIRROR_BUCKET_NAME/$name"; then
echo "https://$S3_MIRROR_URL/$name"
fi
}
#
# Open WSL files inside Sublime Text running in Windows.
#
# Arguments:
# (Optional) The path to open. Defaults to the current directory.
#
function subl {
local path=$1
if [ -z "$path" ]; then
path="."
fi
expanded_path=
expand_to_windows_path "$path" expanded_path
"/mnt/c/Program Files/Sublime Text 3/subl.exe" "$expanded_path"
}
#
# Opens git-cola.
#
# Arguments:
# (Optional) The directory to open. Defaults to the current directory.
#
function cola {
local path=$1
if [ -z "$path" ]; then
path="."
fi
path=$(/bin/readlink -f "$path")
_=$(/usr/bin/nohup /usr/bin/git-cola -r "$path" &>/dev/null &)
# Maximize the window
local id=""
local name
name=$(/usr/bin/basename "$path")
while [ -z "$id" ]; do
id=$(/usr/bin/xwininfo -root -children | /bin/grep git-cola | /bin/grep "$name" | /usr/bin/tail -1 | /usr/bin/awk '{print $1}')
/bin/sleep 0.1
done
/usr/bin/wmctrl -ir "$id" -b add,maximized_vert,maximized_horz
}
#
# Opens a Visual Studio project in Windows.
#
# Arguments:
# (Optional) The directory of the project to open. Defaults to the current
# directory.
#
function vs {
local path=$1
if [ -z "$path" ]; then
path="."
fi
path=${path%/}
local files
local match=""
files=$(/bin/ls "$path")
match=$(/bin/grep '\.sln$' <<< "$files" | /usr/bin/tail -1)
if [ -z "$match" ]; then
echo "Cannot find Visual Studio solution file in $path" >&2
return 1
fi
path="$path/$match"
expanded_path=
expand_to_network_path "$path" expanded_path
export PATH="$PATH"
/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe Start "$expanded_path"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment