From 3eb39969a3f9c0d0c956f12e3a40febab1ac3835 Mon Sep 17 00:00:00 2001
From: Ayaz Badouraly <ayaz.badouraly@via.ecp.fr>
Date: Mon, 19 Dec 2016 15:52:03 +0100
Subject: [PATCH] Improving default values management

This commit is inspired by https://github.com/bhilburn/powerlevel9k/blob/1ff9da64d974265ce2f22bd1da4a47d0b8f7ca90/functions/utilities.zsh#L17-L27
---
 functions/utils.zsh | 54 +++++++++++++++++++++++++++++++++++++++++++++
 via.zsh-theme       | 13 ++++++-----
 2 files changed, 61 insertions(+), 6 deletions(-)
 create mode 100644 functions/utils.zsh

diff --git a/functions/utils.zsh b/functions/utils.zsh
new file mode 100644
index 0000000..d9a2c6b
--- /dev/null
+++ b/functions/utils.zsh
@@ -0,0 +1,54 @@
+#
+# functions/utils.zsh - provide useful functions
+#
+# This work is free.  You can redistribute it and/or modify it under the terms of
+# the Do What The Fuck You Want To Public License, Version 2, as published by Sam
+# Hocevar.  See  the COPYING file  or  http://www.wtfpl.net/  for  more  details.
+#
+
+# Given the name of a variable and a default value, sets the variable value to the
+# default if and only if it has not been defined.
+#
+# @param $1 : name of the variable to be filled
+# @param $2 : default value set if $1 is not already defined
+function set_default() {
+
+	# /!\ compute differently with color variables
+	# TODO make the regex case-insensitive
+	if [[ $1 =~ '_COLOR(_.*)?$' ]]; then
+		set_default_color $1 $2
+	else
+		# See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
+		[[ -z ${(P)1} ]] && typeset -g $1=$2
+	fi
+}
+
+# Given the name of a variable and a default value, overwrites the variable value
+# if and only if it has not been properly set as a visual effect.
+#
+# @param $1 : name of the variable to be filled
+# @param $2 : default value set if $1 is not already a visual effect or a color code
+function set_default_color() {
+
+	# See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects
+	if [[ ! ${(P)1} =~ '%' ]]; then
+
+		# If the variable whose name is $1 has already been overwritten,
+		# use this color instead of the default one provided in $2
+		if [[ -n ${(P)1} ]]; then
+			typeset -g $1="%F{${(P)1}}"
+		else
+
+			# Distinguish case in which $2 is already a visual effect
+			# and case in which it is only a color code
+			if [[ $2 =~ '%' ]]; then
+				typeset -g $1=$2
+			else
+				typeset -g $1="%F{$2}"
+			fi
+		fi
+	fi
+}
+
+# vim: ft=zsh fenc=utf-8
+
diff --git a/via.zsh-theme b/via.zsh-theme
index 175f5f6..f2808ec 100644
--- a/via.zsh-theme
+++ b/via.zsh-theme
@@ -13,6 +13,7 @@
 
 OHMYVIA_INSTALLATION_PATH="$(dirname "$0")"
 
+source $OHMYVIA_INSTALLATION_PATH/functions/utils.zsh
 source $OHMYVIA_INSTALLATION_PATH/functions/vcs_themes.zsh
 source $OHMYVIA_INSTALLATION_PATH/functions/vcs_utils.zsh
 
@@ -49,11 +50,11 @@ zstyle ':vcs_info:*' max-exports 1		# vcs_info only sets vcs_info_msg_0_
 zstyle ':vcs_info:*' enable git
 zstyle ':vcs_info:*' check-for-changes true	# enable %c and %u sequences usage
 
-OHMYVIA_VCS_COLOR_UNSTAGED=${OHMYVIA_VCS_COLOR_UNSTAGED:-"%F{red}"}
-OHMYVIA_VCS_COLOR_STAGED=${OHMYVIA_VCS_COLOR_STAGED:-"%F{yellow}"}
-OHMYVIA_VCS_COLOR_UNTRACKED=${OHMYVIA_VCS_COLOR_UNTRACKED:-"%F{blue}"}
-OHMYVIA_VCS_COLOR_STASH=${OHMYVIA_VCS_COLOR_STASH:-"%F{cyan}"}
-OHMYVIA_VCS_COLOR_CLEAN=${OHMYVIA_VCS_COLOR_CLEAN:-"%F{green}"}
+set_default OHMYVIA_VCS_COLOR_UNSTAGED  "red"
+set_default OHMYVIA_VCS_COLOR_STAGED    "yellow"
+set_default OHMYVIA_VCS_COLOR_UNTRACKED "blue"
+set_default OHMYVIA_VCS_COLOR_STASH     "cyan"
+set_default OHMYVIA_VCS_COLOR_CLEAN     "green"
 
 zstyle ':vcs_info:*' unstagedstr $OHMYVIA_VCS_COLOR_UNSTAGED
 zstyle ':vcs_info:*' stagedstr   $OHMYVIA_VCS_COLOR_STAGED
@@ -61,7 +62,7 @@ zstyle ':vcs_info:*' stagedstr   $OHMYVIA_VCS_COLOR_STAGED
 zstyle ':vcs_info:git*+set-message:*' hooks misc-init git-stash git-untracked
 
 # Load VCS theme
-OHMYVIA_VCS_THEME=${OHMYVIA_VCS_THEME:-"default"}
+set_default OHMYVIA_VCS_THEME "default"
 eval +vi-theme-$OHMYVIA_VCS_THEME
 
 zstyle ':vcs_info:*' formats       " $OHMYVIA_VCS_PROMPT_NORMAL"
-- 
GitLab