File: pwsl.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # pwsl - Power Windows Subsystem for Linux (`power-weasel`)
  27 #
  28 # Add WSL-specific live-shell shortcuts/commands by sourcing this script.
  29 #
  30 # To use this script, you're supposed to `source` it, so its definitions
  31 # stay for your whole shell session: for that, you can run `source pwsl`
  32 # or `. pwsl` (no quotes either way) directly or at shell startup.
  33 
  34 
  35 case "$1" in
  36     -h|--h|-help|--help)
  37         awk '/^# +pwsl /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  38     ;;
  39 esac
  40 
  41 
  42 # dash doesn't support regex-matching syntax, forcing to use case statements
  43 case "$0" in
  44     -bash|-dash|-sh|bash|dash|sh|/bin/sh)
  45         # script is being sourced with bash, dash, or ash, which is good
  46         :
  47     ;;
  48 
  49     *)
  50         case "$ZSH_EVAL_CONTEXT" in
  51             *:file)
  52                 # script is being sourced with zsh, which is good
  53                 :
  54             ;;
  55 
  56             *)
  57                 # script is being run normally, which is a waste of time
  58     printf "\033[7mDon't run this script directly: instead source it\033[0m\n"
  59     printf "\033[7mby running '. pwsl' (without the single quotes).\033[0m\n"
  60     printf "\n"
  61     printf "\033[7mBefore doing that, you may want to see the help,\033[0m\n"
  62     printf "\033[7mby running 'pwsl -h' (without the single quotes).\033[0m\n"
  63                 # exiting during shell-startup may deny shell access, even if
  64                 # the script is being run, instead of being sourced directly
  65             ;;
  66         esac
  67     ;;
  68 esac
  69 
  70 
  71 if [ ! -e /usr/bin/wslinfo ]; then
  72     printf "\033[7mpwsl: WSL doesn't seem to be running.\033[0m\n"
  73 fi
  74 
  75 
  76 # Windows Subsystem for Linux
  77 alias wsl='/mnt/c/Windows/System32/wsl.exe'
  78 
  79 # Windows Subsystem for Linux OFF
  80 alias wsloff='wsl --shutdown'
  81 
  82 
  83 # Empty Clipboard
  84 ec() { /mnt/c/Windows/System32/clip.exe < /dev/null; }
  85 
  86 # find the LAN (local-area network) IP address for this device
  87 lanip() {
  88     /mnt/c/Windows/System32/ipconfig.exe \
  89     | awk '/192\.[0-9\.]+\r?$/ { print $NF }'
  90 }
  91 
  92 # open/pop-up files, GUI file-explorers for folders, or web-browsers for URIs
  93 open() {
  94     local a
  95     local s='s-/$--; s-/-\\-g; s-^/mnt/([A-Ia-i])/-\1:\\-'
  96     for a in "${@:-.}"; do
  97         /mnt/c/Windows/System32/cmd.exe /c start "$(echo "$a" | sed -E "$s")"
  98     done | cat
  99 }
 100 
 101 # voice-synthesize plain-text
 102 say() {
 103     local p
 104     local w='/mnt/c/Windows/System32/WindowsPowerShell/'
 105     p="$(find $w -name powershell.exe -type f)"
 106     awk 1 "$@" \
 107     | "${p}" -noprofile -command '
 108         Add-Type -AssemblyName System.Speech
 109         $s = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
 110         foreach ($line in $Input) { $s.Speak($line) }
 111     ' | cat
 112 }
 113 
 114 # Show It: fixes my `si` script to use win-python to open browser tabs
 115 si() {
 116     if [ $# -eq 0 ]; then
 117         wsl --cd "$(dirname "$(which si)")" "$(which python.exe)" si
 118     else
 119         open "$@"
 120     fi
 121 }
 122 
 123 # turn wsl/unix-style full-paths into WINdows-style full-PATHS
 124 winpaths() { awk 1 "$@" | sed -E 's-/mnt/(.)/-\u\1:/-'; }