| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #!/bin/zsh
- function _package_manager {
- # Set up completion on first call
- if [[ -z "$_package_manager_completion_setup" ]]; then
- compdef _package_manager_completion _package_manager p 2>/dev/null
- _package_manager_completion_setup=1
- fi
- local pkg_manager=""
- if [[ -f bun.lockb ]]; then
- pkg_manager="bun"
- elif [[ -f pnpm-lock.yaml ]]; then
- pkg_manager="pnpm"
- elif [[ -f yarn.lock ]]; then
- pkg_manager="yarn"
- elif [[ -f package-lock.json ]]; then
- pkg_manager="npm"
- else
- pkg_manager="pnpm"
- fi
- if [[ -f package.json ]] && [[ $1 != "run" ]] && [[ $1 != "install" ]] && [[ $1 != "add" ]] && [[ $1 != "remove" ]] && [[ $1 != "i" ]] && [[ $1 != "rm" ]]; then
- if which jq >/dev/null 2>&1 && jq -e ".scripts[\"$1\"] != null" package.json >/dev/null 2>&1; then
- set -- "run" "$@"
- fi
- fi
- if which corepack >/dev/null 2>&1; then
- corepack ${pkg_manager} "$@"
- else
- command ${pkg_manager} "$@"
- fi
- }
- function vimrc {
- if [[ -z "$XDG_CONFIG_HOME" ]]; then
- echo "Error: XDG_CONFIG_HOME not set" >&2
- return 1
- fi
- local original_dir=$(pwd)
- cd "$XDG_CONFIG_HOME/nvim" || {
- echo "Error: Cannot access $XDG_CONFIG_HOME/nvim" >&2
- return 1
- }
- nvim
- cd "$original_dir"
- }
- function nixrc {
- if [[ -z "$XDG_CONFIG_HOME" ]]; then
- echo "Error: XDG_CONFIG_HOME not set" >&2
- return 1
- fi
- local original_dir=$(pwd)
- cd "$XDG_CONFIG_HOME/nixos" || {
- echo "Error: Cannot access $XDG_CONFIG_HOME/nixos" >&2
- return 1
- }
- nvim
- cd "$original_dir"
- }
- function zshrc {
- if [[ -z "$XDG_CONFIG_HOME" ]]; then
- echo "Error: XDG_CONFIG_HOME not set" >&2
- return 1
- fi
- local original_dir=$(pwd)
- cd "$XDG_CONFIG_HOME/zsh" || {
- echo "Error: Cannot access $XDG_CONFIG_HOME/zsh" >&2
- return 1
- }
- nvim
- cd "$original_dir"
- }
- function sb {
- if [[ "$1" == "-h" || "$1" == "--help" ]]; then
- echo "Usage: sb"
- echo "Launch SilverBullet for notes and open in browser"
- echo "Notes directory: ~/Documents/Notes"
- echo "Port: 42069"
- return 0
- fi
- local notes_dir="$HOME/Documents/Notes"
- local port=42069
- # Check if silverbullet command exists
- if ! command -v silverbullet >/dev/null 2>&1; then
- echo "Error: silverbullet command not found" >&2
- echo "Please install SilverBullet first" >&2
- return 1
- fi
- # Check if notes directory exists
- if [[ ! -d "$notes_dir" ]]; then
- echo "Error: Notes directory not found at $notes_dir" >&2
- return 1
- fi
- # Check if SilverBullet is already running on port 42069
- if lsof -i ":$port" >/dev/null 2>&1 && pgrep -f "silverbullet.*$notes_dir" >/dev/null 2>&1; then
- echo "SilverBullet is already running for $notes_dir on port $port"
- echo "Opening browser at http://127.0.0.1:$port..."
- xdg-open "http://127.0.0.1:$port" 2>/dev/null || {
- echo "Error: Could not open browser. Please open http://127.0.0.1:$port manually" >&2
- return 1
- }
- return 0
- fi
- echo "Launching SilverBullet on port $port..."
- echo "Notes directory: $notes_dir"
- # Launch SilverBullet in the background
- silverbullet --port "$port" "$notes_dir" >/dev/null 2>&1 &
- local sb_pid=$!
- # Wait a moment for SilverBullet to start
- local wait_count=0
- local max_wait=10
- while [[ $wait_count -lt $max_wait ]]; do
- if lsof -i ":$port" >/dev/null 2>&1; then
- break
- fi
- sleep 0.5
- wait_count=$((wait_count + 1))
- done
- # Check if process is still running
- if ! kill -0 "$sb_pid" 2>/dev/null; then
- echo "Error: SilverBullet failed to start" >&2
- return 1
- fi
- # Open browser
- echo "Opening browser at http://127.0.0.1:$port..."
- xdg-open "http://127.0.0.1:$port" 2>/dev/null || {
- echo "Error: Could not open browser. Please open http://127.0.0.1:$port manually" >&2
- return 1
- }
- echo "SilverBullet is running (PID: $sb_pid) on port $port"
- }
- _package_manager_completion() {
- local -a commands
- commands=(
- 'run:Run npm script'
- 'install:Install dependencies'
- 'add:Add dependency'
- 'remove:Remove dependency'
- 'dev:Start dev server'
- 'build:Build project'
- 'test:Run tests'
- 'lint:Run linter'
- )
- _describe 'package manager commands' commands
- }
|