Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh promt for custom segment #609

Closed
Roang-zero1 opened this issue Apr 4, 2020 · 2 comments
Closed

Refresh promt for custom segment #609

Roang-zero1 opened this issue Apr 4, 2020 · 2 comments

Comments

@Roang-zero1
Copy link

I've created a custom segment that asynchronously loads the current docker-compose status.

The segment works as expected with the cached data, but I am unable to refresh the promt.
I followed the suggestions in #503 and added zle reset-promt and zle -R, but the promt segment does not update.
If I output data in the callback before the zle commands it vanishes, so I think the reload works but I am missing something to make the prompt segment update.

The full .zshrc and .p10k.zsh can be found at Roang-zero1/dotfiles@85aea74.

PS: What way would be the default way to provide default values for variables (e.g. colors for container status)

#!/usr/bin/env zsh

function parse_compose_data() {
  local working_directory=$1
  local compose_out
  compose_out=$(cd $working_directory; docker-compose ps 2>&1)
  local compose_rc=$?
  local output
  output="None"
  if [ "$compose_rc" -eq 0 ]; then
    if [[ $DOCKER_MACHINE_NAME != "" && $DOCKER_MACHINE_NAME != "default" ]]; then
      DOCKER_MACHINE_NAME_LOCAL="%{$fg_bold[red]%}"$DOCKER_MACHINE_NAME"%{$fg_bold[blue]%}:"
    else
      DOCKER_MACHINE_NAME_LOCAL=""
    fi

    output="$DOCKER_MACHINE_NAME_LOCAL("

    echo $compose_out | tail -n+3 | while read line
    do
      CONTAINER_LETTER_POSITION=$(echo $line | awk 'match($0,"_"){print RSTART}')
      CONTAINER_LETTER=$(echo ${line:$CONTAINER_LETTER_POSITION:1} | tr '[:lower:]' '[:upper:]')
      if [[ $line == *"Up"* ]]; then
          output="$output%{$fg_bold[green]%}"$CONTAINER_LETTER"%{$fg_bold[blue]%}"
      else
          output="$output%{$fg_bold[red]%}"$CONTAINER_LETTER"%{$fg_bold[blue]%}"
      fi
    done

    output="$output)"
  fi

  echo "$working_directory"
  echo "$output"
}

function p10k_refresh() {
  local working_directory
  local docker_status
  return_values=(${(f)3})
  if [ "$return_values[2]" != "None" ]; then
    MY_P9K_DOCKER_OUTPUT[$return_values[1]]=$return_values[2]
    zle reset-prompt
    zle -R
  fi
}

async_init
async_start_worker docker_compose_status -n
async_register_callback docker_compose_status p10k_refresh
typeset -g -A MY_P9K_DOCKER_OUTPUT

function prompt_my_docker_compose() {
  if type "docker-compose" >> /dev/null; then
    local working_directory=$(pwd)
    async_job docker_compose_status parse_compose_data $working_directory
    if [ ! -z $MY_P9K_DOCKER_OUTPUT[$working_directory] ]; then
      p10k segment -i $'\uf308' -f blue -t "$MY_P9K_DOCKER_OUTPUT[$working_directory]"
    fi
  fi
}
@romkatv
Copy link
Owner

romkatv commented Apr 4, 2020

Please try replacing your prompt_my_docker_compose function with this:

function prompt_my_docker_compose() {
  (( $+commands[docker-compose] )) || return
  async_job docker_compose_status parse_compose_data $PWD
  local content='$MY_P9K_DOCKER_OUTPUT[$PWD]'
  p10k segment -i $'\uf308' -f blue -e -c $content -t $content
}

I've added -e so that content gets expanded when prompt is reset and -c to show this segment only when the expanded content is not empty.

I've also replaced $(pwd) with $PWD. This isn't strictly necessary but it's about 1000 times faster.

@Roang-zero1
Copy link
Author

Segment now works, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants