Giter Site home page Giter Site logo

Comments (2)

thomasvincent avatar thomasvincent commented on July 21, 2024

I was refactoring the code to be more idiomatic of Mac OS X and take advantage of zsh. Now it gets through most of the .zlogin before it just closes arbitrarily.

#!/bin/zsh

set -euo pipefail
setopt extended_glob

#!/bin/zsh

set -euo pipefail
setopt extended_glob

# --- Function Definitions ---

get_moon_icon() {
  local moon_phase="$1"
  if (( $(echo "$moon_phase < 0.0625 || $moon_phase > 0.9375" | bc -l) )); then
    printf 'πŸŒ‘ New Moon'
  elif (( $(echo "$moon_phase > 0.4375 && $moon_phase < 0.5625" | bc -l) )); then
    printf 'πŸŒ• Full Moon'
  elif (( $(echo "$moon_phase < 0.5" | bc -l) )); then
    printf 'πŸŒ’ Waxing'
  else
    printf '🌘 Waning'
  fi
}

get_weather_icon() {
  local icon_code="$1"
  case $icon_code in
    01*) printf 'β˜€οΈ' ;; # Clear sky
    02*) printf '🌀️' ;; # Few clouds
    03*) printf '☁️' ;; # Scattered clouds
    04*) printf 'πŸŒ₯️' ;; # Broken clouds
    09*) printf '🌧️' ;; # Shower rain
    10*) printf '🌦️' ;; # Rain
    11*) printf 'β›ˆοΈ' ;; # Thunderstorm
    13*) printf '❄️' ;; # Snow
    50*) printf '🌫️' ;; # Mist
    *)   printf '☁️' ;;  # Default to cloud
  esac
}

fetch_weather_data() {
  local api_url="$1"
  if ! curl -sf "$api_url"; then
    print -u2 "Failed to fetch data from API." >&2  # Redirect error message to stderr
    return 1
  fi
}

get_weather_and_moon() {
  local api_key="your_openweathermap_api_key"  # Replace with your actual API key
  local home_city=$(defaults read NSGlobalDomain AppleLocale | tr '_' ' ')
  local home_coords=$(
    corelocationcli get-location |
    plutil -extract latitude raw - | xargs printf "%.4f," &&
    plutil -extract longitude raw - | xargs printf "%.4f"
  )

  local weather_data_url="https://api.openweathermap.org/data/2.5/onecall?lat=$home_coords&exclude=minutely,hourly,daily,alerts&units=imperial&appid=$api_key"

  local weather_data=$(fetch_weather_data "$weather_data_url") || return

  local moon_phase=$(echo "$weather_data" | jq -r '.current.moon_phase')
  local icon_code=$(echo "$weather_data" | jq -r '.current.weather[0].icon')

  local moon_icon=$(get_moon_icon "$moon_phase")
  local weather_icon=$(get_weather_icon "$icon_code")

  printf "πŸŒ™ %s  %s\n" "$moon_icon" "$weather_icon"
}

get_uptime() {
    sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//' | xargs -I{} uptime -s {}
}
# ... (rest of the script is the same)

# Function to get system uptime
get_uptime() {
    local boot_time
    local current_time
    local uptime_seconds
    local days
    local hours
    local minutes
    local seconds

    boot_time=$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//')
    current_time=$(date +%s)
    uptime_seconds=$((current_time - boot_time))

    days=$((uptime_seconds / 86400))
    hours=$(( (uptime_seconds % 86400) / 3600 ))
    minutes=$(( (uptime_seconds % 3600) / 60 ))
    seconds=$((uptime_seconds % 60))

    printf "%d days, %d hours, %d minutes, %d seconds" "$days" "$hours" "$minutes" "$seconds"
}

# --- Session Login Commands ---

# Welcome Message
printf "\n$(tput bold)$(tput setaf 2)Welcome to your Zsh session, %s!$(tput sgr0)\n\n" "$(whoami)"

# Date & Time (Local and UTC)
printf "Local Time:  %s\n" "$(date +'%a, %b %d %Y, %I:%M %p %Z')"
printf "UTC Time:    %s\n\n" "$(date -u +'%a, %b %d %Y, %I:%M %p UTC')"

# Weather & Moon Phase
get_weather_and_moon

# System Information
printf "$(tput bold)System Info:$(tput sgr0)\n"
printf "Uptime:      %s\n" "$(get_uptime)"
printf "Disk Usage:  %s used of %s\n" "$(df -h / | awk 'NR==2{print $5}')" "$(df -h / | awk 'NR==2{print $2}')"
printf "Memory Usage: %s\n\n" "$(top -l 1 | awk '/PhysMem/{print $2}')"

# Random Quote/Tip
printf "$(tput bold)Thought for the day:$(tput sgr0)\n"
curl -s https://api.quotable.io/random | jq -r '.content'

# Update Checks (Optional)
if command -v brew &> /dev/null; then
    printf "\n$(tput bold)Checking for updates...$(tput sgr0)\n"
    brew update &>/dev/null
    brew outdated --quiet || brew upgrade --quiet
    printf "Done.\n"
fi

# Custom Messages/Reminders
printf "\n$(tput bold)Remember to review your tasks for the day!$(tput sgr0)\n"

from warp.

dannyneira avatar dannyneira commented on July 21, 2024

Hi @thomasvincent thanks for letting us know. I was able to isolate the line which is causing the issue.

# Enable strict mode
set -euo pipefail

There are a few options here that may be able to help with the issue.

  1. Move the line to the bottom of the script, this will allow the functions to run and not exit the session.
  2. Remove the -eu flag which appears to be causing the session quit.
  3. Wrap the code in a conditional to disable it just for Warp:
if [[ $TERM_PROGRAM != "WarpTerminal" ]]; then
  # Enable strict mode
  set -euo pipefail
fi

CleanShot 2024-06-12 at 11 43 33@2x

I'll notify the team of this issue and we'll post further updates on this thread.

from warp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.