From 4b9c2cfcd6c3fa4f14a62faad97559e53c8ac971 Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Tue, 18 Jun 2019 21:43:20 +1000 Subject: [PATCH] add basic gcp-ps1 --- plugins/gcp-ps1/README.md | 34 +++++++++++++++++++++ plugins/gcp-ps1/gcp-ps1.plugin.zsh | 49 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 plugins/gcp-ps1/README.md create mode 100644 plugins/gcp-ps1/gcp-ps1.plugin.zsh diff --git a/plugins/gcp-ps1/README.md b/plugins/gcp-ps1/README.md new file mode 100644 index 000000000..eda90e85e --- /dev/null +++ b/plugins/gcp-ps1/README.md @@ -0,0 +1,34 @@ +# gcp prompt for zsh + +Prompt which displays current configuration + +## Current state + +Tested only on ubuntu. +May or may not work on FreeBSD or osX. Feel free to fix any issue! + +## Requirements + +[gcloud](https://cloud.google.com/sdk/docs/downloads-interactive) + + +## Enabling + +In order to use gcp-ps1 with Oh My Zsh, you'll need to enable them in the +.zshrc file. You'll find the zshrc file in your $HOME directory. + +```shell +vim $HOME/.zshrc +``` + +Add gcp-ps1 to the list of enabled plugins and enable it on the prompt: + +```shell +plugins=( + git + gcp-ps1 +) + +PROMPT=$PROMPT'$(gcp_ps1) ' +``` + diff --git a/plugins/gcp-ps1/gcp-ps1.plugin.zsh b/plugins/gcp-ps1/gcp-ps1.plugin.zsh new file mode 100644 index 000000000..32d538caf --- /dev/null +++ b/plugins/gcp-ps1/gcp-ps1.plugin.zsh @@ -0,0 +1,49 @@ +#!/bin/zsh + +# GCP prompt helper for zsh +# ported to oh-my-zsh +# Displays current configuration +# +# Author: Marcin Niemira +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Debug +[[ -n $DEBUG ]] && set -x + +setopt PROMPT_SUBST +autoload -U add-zsh-hook +add-zsh-hook precmd _gcp_ps1_update_cache + + +GCP_PS1_COLOR_SYMBOL="%{$fg[blue]%}" +GCP_PS1_COLOR_NS="%{$fg[cyan]%}" + +_gcp_ps1_symbol() { + echo "☁️ " +} + +_gcp_ps1_update_cache() { + CONTEXT=$(cat "$HOME/.config/gcloud/active_config") +} + +gcp_ps1 () { + local reset_color="%{$reset_color%}" + + GCP_PS1="${reset_color}(" + GCP_PS1+="$(_gcp_ps1_symbol)" + GCP_PS1+="${GCP_PS1_COLOR_SYMBOL}$CONTEXT${reset_color}" + GCP_PS1+=")${reset_color}" + + echo "${GCP_PS1}" +}