functions.zsh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # Lazy load zoxide
  2. z() {
  3. # Remove the placeholder function
  4. unfunction "$0"
  5. # Load zoxide
  6. eval "$(zoxide init zsh)"
  7. # Execute the command
  8. $0 "$@"
  9. }
  10. # Ensure we have all the zoxide command variants covered
  11. zi() { z "$@" }
  12. za() { z "$@" }
  13. zrm() { zoxide remove "$@" }
  14. function _package_manager {
  15. local pkg_manager=""
  16. if [[ -f bun.lockb ]]; then
  17. pkg_manager="bun"
  18. elif [[ -f pnpm-lock.yaml ]]; then
  19. pkg_manager="pnpm"
  20. elif [[ -f yarn.lock ]]; then
  21. pkg_manager="yarn"
  22. elif [[ -f package-lock.json ]]; then
  23. pkg_manager="npm"
  24. else
  25. pkg_manager="pnpm"
  26. fi
  27. # Check if the first argument is a script in package.json
  28. if [[ -f package.json ]] && [[ $1 != "run" ]] && [[ $1 != "install" ]] && [[ $1 != "add" ]] && [[ $1 != "remove" ]] && [[ $1 != "i" ]] && [[ $1 != "rm" ]]; then
  29. # Check if the command exists in package.json scripts using jq
  30. if jq -e ".scripts[\"$1\"] != null" package.json >/dev/null 2>&1; then
  31. set -- "run" "$@"
  32. fi
  33. fi
  34. # Use corepack when available
  35. if command -v corepack >/dev/null 2>&1; then
  36. corepack ${pkg_manager} "$@"
  37. else
  38. command ${pkg_manager} "$@"
  39. fi
  40. }
  41. yarn() { echo 🖕; }
  42. yarnpkg() { echo 🖕; }
  43. pnpm() { echo 🖕; }
  44. pn() { echo 🖕; }
  45. pnpx() { echo 🖕; }
  46. npm() { echo 🖕; }
  47. alias p='_package_manager'
  48. alias unChonk="echo '🧹 Starting the great node_modules purge...' && find . -name 'node_modules' -type d -prune -exec sh -c 'echo \"💥 Deleting {}\" && rm -rf \"{}\"' \;"
  49. # Open files using system default application
  50. function open {
  51. for i in $*
  52. do
  53. setsid nohup xdg-open $i > /dev/null 2> /dev/null
  54. done
  55. }
  56. function fopen() {
  57. local selected
  58. selected=$(fd "$@" | fzf)
  59. [[ -n "$selected" ]] && setsid nohup xdg-open "$selected" >/dev/null 2>&1 &
  60. }
  61. alias fo='fopen'
  62. function upload_file() {
  63. if [[ -z "$1" ]]; then
  64. echo "Usage: $0 <file>"
  65. return 1
  66. fi
  67. if [[ ! -f "$1" ]]; then
  68. echo "Error: File not found: $1"
  69. return 1
  70. fi
  71. local url="https://dump.mz.uy"
  72. # local url="http://localhost:8080"
  73. echo "Uploading $1 to $url..."
  74. local full_response=$(curl -i -F"file=@$1" -F"one_time=" "$url")
  75. # local full_response=$(curl -i -F"file=@$1" "$url")
  76. local url_response=$(echo "$full_response" | tail -n 1)
  77. local token=$(echo "$full_response" | grep -i "X-Token:" | awk '{print $2}' | tr -d '\r')
  78. if [[ -n "$url_response" ]]; then
  79. echo -n "$url_response" | wl-copy # Wayland
  80. echo "✓ Uploaded: $url_response (copied to clipboard)"
  81. if [[ -n "$token" ]]; then
  82. echo "Management token: $token"
  83. fi
  84. else
  85. echo "× Failed to upload file"
  86. return 1
  87. fi
  88. }
  89. # Create both commands as aliases to the same function
  90. alias drop='upload_file'
  91. alias dump='upload_file'
  92. # Wacom tablet configuration
  93. function wacom {
  94. if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
  95. systemctl --user enable opentabletdriver --now
  96. otd loadsettings ~/Sync/wacom/wacom.json
  97. else
  98. # Reset area and map the stylus to DisplayPort-0, then rotate
  99. xsetwacom --set "Wacom One by Wacom S Pen stylus" ResetArea
  100. xsetwacom --set "Wacom One by Wacom S Pen stylus" MapToOutput DisplayPort-0
  101. xsetwacom --set "Wacom One by Wacom S Pen stylus" Rotate half
  102. # Reset area and map the eraser to DisplayPort-0
  103. xsetwacom --set "Wacom One by Wacom S Pen eraser" ResetArea
  104. xsetwacom --set "Wacom One by Wacom S Pen eraser" MapToOutput DisplayPort-0
  105. fi
  106. }
  107. # Enhanced git clone function
  108. function gc {
  109. if [[ "$1" == */* ]]
  110. then
  111. git clone "https://github.com/$1" "${@:2}"
  112. else
  113. git clone "https://github.com/marianozunino/$1" "${@:2}"
  114. fi
  115. }
  116. # Improved file sharing with croc
  117. function croc-send {
  118. croc send --code mzunino "$@"
  119. }
  120. function croc-receive {
  121. croc --yes mzunino
  122. }
  123. # Fix swaymsg when inside tmux
  124. if [[ -v TMUX ]]; then
  125. function swaymsg {
  126. export SWAYSOCK=$XDG_RUNTIME_DIR/sway-ipc.$UID.$(pgrep -x sway).sock
  127. command swaymsg "$@"
  128. }
  129. fi
  130. # Expose local port to internet
  131. function expose() {
  132. if [ -z "$1" ]; then
  133. echo "Usage: expose <port>"
  134. return
  135. fi
  136. ssh marianozunino@srv.us -R 1:localhost:$1
  137. }
  138. # Helper function for command existence check
  139. function _has {
  140. return $( whence $1 >/dev/null )
  141. }
  142. # Sublime merge shortcut
  143. function sm {
  144. /opt/sublime_merge/sublime_merge $1
  145. }
  146. # Edit nvim config
  147. function vimrc {
  148. cd $XDG_CONFIG_HOME/nvim
  149. nvim
  150. cd -
  151. }
  152. # Edit zsh config
  153. function zshrc {
  154. cd $XDG_CONFIG_HOME/zsh
  155. nvim
  156. cd -
  157. }
  158. # Find process using a port
  159. function ppid {
  160. lsof -i :$1
  161. }
  162. alias pport=ppid
  163. # Kubernetes port forwarding helper
  164. function kf {
  165. if [ -z "$1" ]; then
  166. echo "Usage: kf <cluster> [service-name]"
  167. return
  168. fi
  169. local namespace="oc-app"
  170. local cluster="oc-$1-eks-cluster"
  171. local svc_filter=""
  172. if [ ! -z "$2" ]; then
  173. svc_filter="-l app.kubernetes.io/instance=$2"
  174. fi
  175. sudo -E kubefwd svc -n ${namespace} -x ${cluster} ${svc_filter}
  176. }
  177. function cat {
  178. if [ -n "$SSH_TTY" ] || [ -n "$SSH_CLIENT" ] || [ -n "$SSH_CONNECTION" ]; then
  179. bat --plain --paging=never "$@"
  180. else
  181. bat "$@"
  182. fi
  183. }
  184. # Enhanced PrivateBin sharing function
  185. function pb {
  186. local usage="Usage: pb [-b] [-e <expiration>] <file>"
  187. local burn_after_reading=false
  188. local expiration="1week"
  189. while getopts ":be:" opt; do
  190. case ${opt} in
  191. b )
  192. burn_after_reading=true
  193. ;;
  194. e )
  195. expiration=$OPTARG
  196. ;;
  197. \? )
  198. echo "Invalid option: -$OPTARG" >&2
  199. echo $usage
  200. return 1
  201. ;;
  202. : )
  203. echo "Option -$OPTARG requires an argument." >&2
  204. echo $usage
  205. return 1
  206. ;;
  207. esac
  208. done
  209. shift $((OPTIND -1))
  210. if [ $# -eq 0 ]; then
  211. echo $usage
  212. return 1
  213. fi
  214. local file=$1
  215. if [ ! -f "$file" ]; then
  216. echo "Error: File '$file' not found." >&2
  217. return 1
  218. fi
  219. # Check for required commands
  220. for cmd in privatebin wl-copy; do
  221. if ! command -v $cmd &> /dev/null; then
  222. echo "Error: '$cmd' command not found. Please install it." >&2
  223. return 1
  224. fi
  225. done
  226. # Prepare the command
  227. local cmd="privatebin create --filename $file --expire $expiration"
  228. if $burn_after_reading; then
  229. cmd="$cmd --burn-after-reading"
  230. fi
  231. # Run command and handle result
  232. local result
  233. result=$(eval "$cmd" 2>&1)
  234. if [ $? -ne 0 ]; then
  235. echo "Error: Failed to upload file to PrivateBin. Details:" >&2
  236. echo "$result" >&2
  237. return 1
  238. fi
  239. echo $result
  240. echo "$result" | wl-copy
  241. echo "PrivateBin link copied to clipboard."
  242. notify-send "PrivateBin" "Link copied to clipboard" 2>/dev/null || true
  243. }
  244. # Load autocomplete files
  245. for completion_file in ~/.local/share/zsh/*-autocomplete.zsh; do
  246. if [ -f "$completion_file" ]; then
  247. source "$completion_file"
  248. fi
  249. done
  250. # Initialize completions for common tools
  251. if command -v rop &> /dev/null; then eval "$(rop completion zsh)"; fi
  252. if command -v goq &> /dev/null; then eval "$(goq completion zsh)"; fi
  253. if command -v kubefwd &> /dev/null; then eval "$(kubefwd completion zsh)"; fi
  254. if command -v bombadil &> /dev/null; then eval "$(bombadil generate-completions zsh)"; fi
  255. if command -v eza &> /dev/null; then compdef eza=ls; fi
  256. function dc() {
  257. git add .
  258. git commit -m 'automated dev commit' --no-gpg-sign
  259. git push
  260. }