Skip to content

Instantly share code, notes, and snippets.

@parkeyparker
Created January 27, 2022 12:33
Show Gist options
  • Save parkeyparker/fbc5a2592b1a1132eb9d709308b48a98 to your computer and use it in GitHub Desktop.
Save parkeyparker/fbc5a2592b1a1132eb9d709308b48a98 to your computer and use it in GitHub Desktop.
Awsume time remaining + oh-my-zsh prompt segment

Displaying Current Role & Time Remaining in oh-my-zsh Prompt Segment

If you are using awsume for AWS role switching then it's handy to be able to see how long you have left before the role expires.

Get Time Remaining

Put the following in your .zshrc so it can be available at any point:

alias timeremaining='_timeremaining'
_timeremaining() {
  secs_future=$(date -j -f "%Y-%m-%dT%H:%M:%S" $1 +%s)
  secs_now=$(date +%s)
  secs_remaining=$(( $secs_future - $secs_now ))
  if [ "$secs_remaining" -lt "0" ]; then
  	echo "00:00"
  elif [ "$secs_remaining" -lt "3600" ]; then
  	gdate -d@$secs_remaining -u +%M:%S
  else
    gdate -d@$secs_remaining -u +%H:%M:%S
  fi
}

alias awsenvtimeremain='_awsenvtimeremain'
_awsenvtimeremain() {
  if [[ -v AWSUME_PROFILE ]]; then
    time_remain=$(_timeremaining $AWSUME_EXPIRATION)
    echo "$time_remain"
  fi
}

Show current role and time remaining in prompt segment

To add a segment you need to edit your p10k.zsh and add the following:

  ##################################[ awsenv: current aws env ]#################################
  # Gets the current AWS environment (if set)
  function prompt_awsenv() {
    if (( ${+AWSUME_PROFILE} )); then
      local time_remain=$(_timeremaining $AWSUME_EXPIRATION)
      if [[ $time_remain == "00:00" ]]; then
        local state=EXPIRED
        #prompt_segment red white "$AWSUME_PROFILE $time_remain"
      else
        local state=VALID
        #prompt_segment white black "$AWSUME_PROFILE $time_remain"
      fi
      p10k segment -s $state -b 2 -f 8 -t "${AWSUME_PROFILE} ${time_remain}"
    fi
  }
  # typeset -g POWERLEVEL9K_AWSENV_FOREGROUND=0
  # typeset -g POWERLEVEL9K_AWSENV_BACKGROUND=7
  typeset -g POWERLEVEL9K_AWSENV_EXPIRED_FOREGROUND=white
  typeset -g POWERLEVEL9K_AWSENV_EXPIRED_BACKGROUND=red
  # Custom icon.
  # typeset -g POWERLEVEL9K_AWSENV_VISUAL_IDENTIFIER_EXPANSION='⭐'
  # Custom prefix.
  # typeset -g POWERLEVEL9K_AWSENV_PREFIX='env '

Finally add currnodeenv to typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=( in order to get it to show on the right prompt or wherever you wish for it to appear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment