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

Add docker_context segment #1641

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

treuherz
Copy link

Adds docker_context segment to show current docker context, with
SHOW_ON_COMMAND and SHOW_DEFAULT parameters.

I hope submitting a PR for this is ok. I would have commented on the issue first but by the time I'd had a go to see if it seemed doable I was most of the way done!

I'm sure there are ways this could be more performant, any advice would be appreciated. docker context show is slow, ~100ms, so this is much faster when jq is available. I don't have a windows machine available to test whether it can find the config file ok on there, so that might need work.

Closes #1485

Adds docker_context segment to show current docker context, with
SHOW_ON_COMMAND and SHOW_DEFAULT parameters.

Closes romkatv#1485
@romkatv
Copy link
Owner

romkatv commented Nov 17, 2021

Thanks for submitting a PR.

This code is too slow for inclusion in powerlevel10k even when jq is available. If you want to use this segment, put it in your own config, rename prompt_docker_context to prompt_my_docker_context (to avoid clashes with future versions of p10k) and replace _p9k_prompt_segment with a call to p10k segment (see p10k help segment).

@treuherz
Copy link
Author

Understood, I appreciate you need to maintain high performance standards in here.

Even if it's not going to be merged, pointers on how to improve it would be great, but no worries if you don't have the bandwidth 👍

@romkatv
Copy link
Owner

romkatv commented Nov 17, 2021

I don't have the bandwidth. It'll take me less time to implement this segment than to explain how to implement it.

@realazizk
Copy link

i use this, it's fast.

  typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
  ...
  docker_context
  ...
  )

  typeset -g POWERLEVEL9K_DOCKER_CONTEXT_FOREGROUND=27

  typeset -g POWERLEVEL9K_DOCKER_CONTEXT_VISUAL_IDENTIFIER_EXPANSION='🐳'

  function prompt_docker_context() {
    local docker_context_cache="/tmp/p10k_docker_context"
    local docker_context_cache_timestamp="/tmp/p10k_docker_context_timestamp"
    local cache_ttl=60

    if [[ -f "$docker_context_cache" && -f "$docker_context_cache_timestamp" ]]; then
      local now=$(date +%s)
      local cache_time=$(cat "$docker_context_cache_timestamp")
      if (( now - cache_time < cache_ttl )); then
        local cached_context=$(cat "$docker_context_cache")
        if [[ -n "$cached_context" ]]; then
          p10k segment -f 27 -i '🐳' -t "$cached_context"
          return
        fi
      fi
    fi

    local docker_context
    docker_context=$(docker context show 2>/dev/null)
    if [[ -n "$docker_context" ]]; then
      echo "$docker_context" > "$docker_context_cache"
      date +%s > "$docker_context_cache_timestamp"
      p10k segment -f 27 -i '🐳' -t "$docker_context"
    fi
  }

  function docker_context_cache_clear() {
    rm -f "/tmp/p10k_docker_context" "/tmp/p10k_docker_context_timestamp"
  }

  if ! typeset -f docker > /dev/null; then
    function docker() {
      command docker "$@"
      local ret=$?
      if [[ "$1" == "context" && "$2" == "use" ]]; then
        docker_context_cache_clear
      fi
      return $ret
    }
  fi

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

Successfully merging this pull request may close these issues.

feat: docker context support
4 participants