User : snow_mac

  • Created: 5687 days ago
  • Karma: 1047
  • # Keep the terminal awake by simulating a Shift+Up Arrow key press every 30 seconds # Useful for preventing sleep during long tasks like downloads or builds # Usage: coffee [-m minutes] # -m minutes : run for specified number of minutes, then stop # If no -m option is given, runs indefinitely coffee() { if [ -f ~/.bash_history ]; then sed -i.bak '/^coffee/d' ~/.bash_history fi if [ -f ~/.zsh_history ]; then sed -i.bak '/coffee/d' ~/.zsh_history fi history -d $(history 1 | awk '{print $1}') 2>/dev/null

      local duration=0
    
      if [[ "$1" == "-m" && -n "$2" ]]; then
        duration=$(($2 * 60))
      fi
    
      local count=0
      while true; do
        # shift arrow key down
        # osascript -e 'tell application "System Events" to key code 126 using {shift down}'
        # F-13 
        osascript -e 'tell application "System Events" to key code 105'
        sleep 30
    
        count=$((count + 30))
        if [[ $duration -gt 0 ]]; then
          if [[ $count -ge $duration ]]; then
            break
          fi
        fi
    
        # Print elapsed time every 60 seconds
        if (( count % 60 == 0 )); then
            echo -ne "\r$((count/60)) minutes elapsed\n"
        fi
      done
    }