Do not print colors if stdout isn't tty

This is previously offered in #18, #22, and #27 and I previously rejected
these PRs. My rationale was to prevent scripting around kubectx as it is
meant to be used as an interactive tool.

But clearly that will be a problem, when you're doing operations like:

    kubectx -d $(kubectx)

which I proposed in #39. Plus this change is harmless. I think this
implementation does a better check than the previous ones.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2018-04-10 21:45:28 -07:00
parent d906013970
commit 4520d3f54b
No known key found for this signature in database
GPG Key ID: 5C02521D7B216AD6
2 changed files with 2 additions and 2 deletions

View File

@ -59,7 +59,7 @@ list_contexts() {
normal=$(tput sgr0) normal=$(tput sgr0)
for c in $(get_contexts); do for c in $(get_contexts); do
if [[ "${c}" = "${cur}" ]]; then if [[ -t 1 && "${c}" = "${cur}" ]]; then
echo "${darkbg}${yellow}${c}${normal}" echo "${darkbg}${yellow}${c}${normal}"
else else
echo "${c}" echo "${c}"

2
kubens
View File

@ -113,7 +113,7 @@ list_namespaces() {
cur="$(current_namespace)" cur="$(current_namespace)"
ns_list=$(get_namespaces) ns_list=$(get_namespaces)
for c in $ns_list; do for c in $ns_list; do
if [[ "${c}" = "${cur}" ]]; then if [[ -t 1 && "${c}" = "${cur}" ]]; then
echo "${darkbg}${yellow}${c}${normal}" echo "${darkbg}${yellow}${c}${normal}"
else else
echo "${c}" echo "${c}"