From 1d26e2ab6f0b57ae8599d59e120df2aea1f125f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 20 Aug 2018 17:35:02 +0200 Subject: [PATCH 1/6] trapd00r: convert perl script to zsh Used color encodings from https://metacpan.org/source/WOLDRICH/Term-ExtendedColor-0.224/lib/Term/ExtendedColor.pm --- themes/trapd00r.zsh-theme | 86 ++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 47 deletions(-) mode change 100644 => 100755 themes/trapd00r.zsh-theme diff --git a/themes/trapd00r.zsh-theme b/themes/trapd00r.zsh-theme old mode 100644 new mode 100755 index fa8d21898..2feca08b9 --- a/themes/trapd00r.zsh-theme +++ b/themes/trapd00r.zsh-theme @@ -36,54 +36,46 @@ local c11=$(printf "\e[38;5;208m\e[1m") local c12=$(printf "\e[38;5;142m\e[1m") local c13=$(printf "\e[38;5;196m\e[1m") -local zsh_path_pl=' -use strict; -use Term::ExtendedColor "fg"; +zsh_path() { + local -A yellow + yellow=( + 1 '%F{228}' 2 '%F{222}' 3 '%F{192}' 4 '%F{186}' + 5 '%F{227}' 6 '%F{221}' 7 '%F{191}' 8 '%F{185}' + 9 '%F{226}' 10 '%F{220}' 11 '%F{190}' 12 '%F{184}' + 13 '%F{214}' 14 '%F{178}' 15 '%F{208}' 16 '%F{172}' + 17 '%F{202}' 18 '%F{166}' + ) -chomp(my $pwd = `pwd`); + local c i=1 + for c (${(s::)PWD}); do + if [[ $c = "/" ]]; then + if [[ $i -eq 1 ]]; then + if [[ -n "$DISPLAY" ]]; then + print -Pn '%F{065}%B /%b%f' + else + print -Pn '\e[31;1m /%f' + fi + (( i++ )) + continue + fi -my @chars = split//, $pwd; - -my $i = 1; - -for(@chars) { - if($_ eq "/") { - if(defined($ENV{DISPLAY})) { - if($i == 1) { - print fg("green28", fg("bold", " /")); - $i++; - next; - } - } - else { - if($i == 1) { - print "\e[31;1m /\e[0m"; - $i++; - next; - } - } - - if(defined($ENV{DISPLAY})) { - print fg("yellow$i", " » "); - $i += 6 - } - else { - print "\e[33m > \e[0m"; - $i += 6; - } - } - else { - if(defined($ENV{DISPLAY})) { - print fg("green28", $_); - } - else { - print "\e[34m$_\e[0m"; - } - } + if [[ -n "$DISPLAY" ]]; then + print -Pn "${yellow[$i]:-%f} » %f" + else + print -Pn "%F{yellow} > %f" + fi + (( i += 6 )) + else + if [[ -n "$DISPLAY" ]]; then + print -Pn "%F{065}$c%f" + else + print -Pn "%F{blue}$c%f" + fi + fi + done } -' # We don't want to use the extended colorset in the TTY / VC. if [ "$TERM" = "linux" ]; then @@ -116,24 +108,24 @@ add-zsh-hook precmd prompt_jnrowe_precmd prompt_jnrowe_precmd () { vcs_info if [ "${vcs_info_msg_0_}" = "" ]; then - dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(echo $zsh_path_pl | perl)%} %{$c0%}(%{$c5%}%?%{$c0%})" + dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(zsh_path)%} %{$c0%}(%{$c5%}%?%{$c0%})" PROMPT='%{$fg_bold[green]%}%p%{$reset_color%}${vcs_info_msg_0_}${dir_status} ${ret_status}%{$reset_color%} > ' # modified, to be committed elif [[ $(git diff --cached --name-status 2>/dev/null ) != "" ]]; then - dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(echo $zsh_path_pl | perl)%} %{$c0%}(%{$c5%}%?%{$c0%})" + dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(zsh_path)%} %{$c0%}(%{$c5%}%?%{$c0%})" PROMPT='${vcs_info_msg_0_}%{$30%} %{$bg_bold[red]%}%{$fg_bold[cyan]%}C%{$fg_bold[black]%}OMMIT%{$reset_color%} %{$fg_bold[green]%}%p%{$reset_color%}${dir_status}%{$reset_color%} > ' elif [[ $(git diff --name-status 2>/dev/null ) != "" ]]; then - dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(echo $zsh_path_pl | perl)%} %{$c0%}(%{$c5%}%?%{$c0%})" + dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(zsh_path)%} %{$c0%}(%{$c5%}%?%{$c0%})" PROMPT='${vcs_info_msg_0_}%{$bg_bold[red]%}%{$fg_bold[blue]%}D%{$fg_bold[black]%}IRTY%{$reset_color%} %{$fg_bold[green]%}%p%{$reset_color%}${dir_status}%{$reset_color%} %{$c13%}>%{$c0%} ' else - dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(echo $zsh_path_pl | perl)%} %{$c0%}(%{$c5%}%?%{$c0%})" + dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(zsh_path)%} %{$c0%}(%{$c5%}%?%{$c0%})" PROMPT='${vcs_info_msg_0_} %{$fg_bold[green]%}%p%{$reset_color%}${dir_status}%{$reset_color%} > ' From 4774bc62d5e1799ff531f2713f4a8cb493a406a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 20 Aug 2018 17:45:36 +0200 Subject: [PATCH 2/6] trapd00r: look for 256-color support, not $DISPLAY Checking if the terminal supports 256 colors is better suited for our purpose. Checking if `$DISPLAY` is set doesn't tell us if our colors will be displayed correctly. --- themes/trapd00r.zsh-theme | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/themes/trapd00r.zsh-theme b/themes/trapd00r.zsh-theme index 2feca08b9..51d5387e0 100755 --- a/themes/trapd00r.zsh-theme +++ b/themes/trapd00r.zsh-theme @@ -38,6 +38,9 @@ local c13=$(printf "\e[38;5;196m\e[1m") zsh_path() { + local colors + colors=$(echoti colors) + local -A yellow yellow=( 1 '%F{228}' 2 '%F{222}' 3 '%F{192}' 4 '%F{186}' @@ -51,7 +54,7 @@ zsh_path() { for c (${(s::)PWD}); do if [[ $c = "/" ]]; then if [[ $i -eq 1 ]]; then - if [[ -n "$DISPLAY" ]]; then + if [[ $colors -ge 256 ]]; then print -Pn '%F{065}%B /%b%f' else print -Pn '\e[31;1m /%f' @@ -60,14 +63,14 @@ zsh_path() { continue fi - if [[ -n "$DISPLAY" ]]; then + if [[ $colors -ge 256 ]]; then print -Pn "${yellow[$i]:-%f} » %f" else print -Pn "%F{yellow} > %f" fi (( i += 6 )) else - if [[ -n "$DISPLAY" ]]; then + if [[ $colors -ge 256 ]]; then print -Pn "%F{065}$c%f" else print -Pn "%F{blue}$c%f" From 3d1719c618e83b03ec7fae023444cdacf729f63a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 20 Aug 2018 17:50:11 +0200 Subject: [PATCH 3/6] trapd00r: optimize reset of foreground colors --- themes/trapd00r.zsh-theme | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/themes/trapd00r.zsh-theme b/themes/trapd00r.zsh-theme index 51d5387e0..a1db9945c 100755 --- a/themes/trapd00r.zsh-theme +++ b/themes/trapd00r.zsh-theme @@ -55,28 +55,29 @@ zsh_path() { if [[ $c = "/" ]]; then if [[ $i -eq 1 ]]; then if [[ $colors -ge 256 ]]; then - print -Pn '%F{065}%B /%b%f' + print -Pn "%F{065}%B /%b" else - print -Pn '\e[31;1m /%f' + print -Pn "\e[31;1m /" fi (( i++ )) continue fi if [[ $colors -ge 256 ]]; then - print -Pn "${yellow[$i]:-%f} » %f" + print -Pn "${yellow[$i]:-%f} » " else - print -Pn "%F{yellow} > %f" + print -Pn "%F{yellow} > " fi (( i += 6 )) else if [[ $colors -ge 256 ]]; then - print -Pn "%F{065}$c%f" + print -Pn "%F{065}$c" else - print -Pn "%F{blue}$c%f" + print -Pn "%F{blue}$c" fi fi done + print -Pn "%f" } From b4c8b60bb45f00f3cd68000ceda3c96b2ad852ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 20 Aug 2018 17:55:22 +0200 Subject: [PATCH 4/6] trapd00r: change more slowly between yellows Also refactor the logic --- themes/trapd00r.zsh-theme | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/themes/trapd00r.zsh-theme b/themes/trapd00r.zsh-theme index a1db9945c..33579e728 100755 --- a/themes/trapd00r.zsh-theme +++ b/themes/trapd00r.zsh-theme @@ -59,16 +59,15 @@ zsh_path() { else print -Pn "\e[31;1m /" fi - (( i++ )) - continue + else + if [[ $colors -ge 256 ]]; then + print -Pn "${yellow[$i]:-%f} » " + else + print -Pn "%F{yellow} > " + fi fi - if [[ $colors -ge 256 ]]; then - print -Pn "${yellow[$i]:-%f} » " - else - print -Pn "%F{yellow} > " - fi - (( i += 6 )) + (( i++ )) else if [[ $colors -ge 256 ]]; then print -Pn "%F{065}$c" From e97262499780caadf5388c3b612100b87da39548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 20 Aug 2018 18:03:41 +0200 Subject: [PATCH 5/6] trapd00r: simplify logic and optimize for loop This version splits the `$PWD` by the slashes and prints the path directory by directory, printing the separators as before. --- themes/trapd00r.zsh-theme | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/themes/trapd00r.zsh-theme b/themes/trapd00r.zsh-theme index 33579e728..7c36487b3 100755 --- a/themes/trapd00r.zsh-theme +++ b/themes/trapd00r.zsh-theme @@ -50,31 +50,29 @@ zsh_path() { 17 '%F{202}' 18 '%F{166}' ) - local c i=1 - for c (${(s::)PWD}); do - if [[ $c = "/" ]]; then - if [[ $i -eq 1 ]]; then - if [[ $colors -ge 256 ]]; then - print -Pn "%F{065}%B /%b" - else - print -Pn "\e[31;1m /" - fi + local dir i=1 + for dir (${(s:/:)PWD}); do + if [[ $i -eq 1 ]]; then + if [[ $colors -ge 256 ]]; then + print -Pn "%F{065}%B /%b" else - if [[ $colors -ge 256 ]]; then - print -Pn "${yellow[$i]:-%f} » " - else - print -Pn "%F{yellow} > " - fi + print -Pn "\e[31;1m /" fi - - (( i++ )) else if [[ $colors -ge 256 ]]; then - print -Pn "%F{065}$c" + print -Pn "${yellow[$i]:-%f} » " else - print -Pn "%F{blue}$c" + print -Pn "%F{yellow} > " fi fi + + (( i++ )) + + if [[ $colors -ge 256 ]]; then + print -Pn "%F{065}$dir" + else + print -Pn "%F{blue}$dir" + fi done print -Pn "%f" } From b70a703a09627e6d96fe9184e9a40395b625a0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 20 Aug 2018 18:15:49 +0200 Subject: [PATCH 6/6] trapd00r: clean up the script --- themes/trapd00r.zsh-theme | 68 ++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/themes/trapd00r.zsh-theme b/themes/trapd00r.zsh-theme index 7c36487b3..144d2549a 100755 --- a/themes/trapd00r.zsh-theme +++ b/themes/trapd00r.zsh-theme @@ -1,8 +1,8 @@ # trapd00r.zsh-theme # # This theme needs a terminal supporting 256 colors as well as unicode. -# In order to avoid external dependencies, it also embeds a (possibly old) -# copy of the perl script located at https://github.com/trapd00r/utils/blob/master/zsh_path, +# In order to avoid external dependencies, it also has a zsh version of +# the perl script at https://github.com/trapd00r/utils/blob/master/zsh_path, # which splits up the current path and makes it fancy. # # By default it spans over two lines like so: @@ -21,20 +21,20 @@ autoload -U add-zsh-hook autoload -Uz vcs_info -local c0=$( printf "\e[m") -local c1=$( printf "\e[38;5;245m") -local c2=$( printf "\e[38;5;250m") -local c3=$( printf "\e[38;5;242m") -local c4=$( printf "\e[38;5;197m") -local c5=$( printf "\e[38;5;225m") -local c6=$( printf "\e[38;5;240m") -local c7=$( printf "\e[38;5;242m") -local c8=$( printf "\e[38;5;244m") -local c9=$( printf "\e[38;5;162m") -local c10=$(printf "\e[1m") -local c11=$(printf "\e[38;5;208m\e[1m") -local c12=$(printf "\e[38;5;142m\e[1m") -local c13=$(printf "\e[38;5;196m\e[1m") +local c0=$'\e[m' +local c1=$'\e[38;5;245m' +local c2=$'\e[38;5;250m' +local c3=$'\e[38;5;242m' +local c4=$'\e[38;5;197m' +local c5=$'\e[38;5;225m' +local c6=$'\e[38;5;240m' +local c7=$'\e[38;5;242m' +local c8=$'\e[38;5;244m' +local c9=$'\e[38;5;162m' +local c10=$'\e[1m' +local c11=$'\e[38;5;208m\e[1m' +local c12=$'\e[38;5;142m\e[1m' +local c13=$'\e[38;5;196m\e[1m' zsh_path() { @@ -79,20 +79,19 @@ zsh_path() { # We don't want to use the extended colorset in the TTY / VC. -if [ "$TERM" = "linux" ]; then - c1=$( printf "\e[34;1m") - c2=$( printf "\e[35m") - c3=$( printf "\e[31m") - c4=$( printf "\e[31;1m") - c5=$( printf "\e[32m") - c6=$( printf "\e[32;1m") - c7=$( printf "\e[33m") - c8=$( printf "\e[33;1m") - c9=$( printf "\e[34m") - - c11=$(printf "\e[35;1m") - c12=$(printf "\e[36m") - c13=$(printf "\e[31;1m") +if [ "$TERM" = linux ]; then + c1=$'\e[34;1m' + c2=$'\e[35m' + c3=$'\e[31m' + c4=$'\e[31;1m' + c5=$'\e[32m' + c6=$'\e[32;1m' + c7=$'\e[33m' + c8=$'\e[33;1m' + c9=$'\e[34m' + c11=$'\e[35;1m' + c12=$'\e[36m' + c13=$'\e[31;1m' fi zstyle ':vcs_info:*' actionformats \ @@ -112,14 +111,12 @@ prompt_jnrowe_precmd () { dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(zsh_path)%} %{$c0%}(%{$c5%}%?%{$c0%})" PROMPT='%{$fg_bold[green]%}%p%{$reset_color%}${vcs_info_msg_0_}${dir_status} ${ret_status}%{$reset_color%} > ' - -# modified, to be committed + # modified, to be committed elif [[ $(git diff --cached --name-status 2>/dev/null ) != "" ]]; then dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(zsh_path)%} %{$c0%}(%{$c5%}%?%{$c0%})" PROMPT='${vcs_info_msg_0_}%{$30%} %{$bg_bold[red]%}%{$fg_bold[cyan]%}C%{$fg_bold[black]%}OMMIT%{$reset_color%} %{$fg_bold[green]%}%p%{$reset_color%}${dir_status}%{$reset_color%} > ' - elif [[ $(git diff --name-status 2>/dev/null ) != "" ]]; then dir_status="%{$c1%}%n%{$c4%}@%{$c2%}%m%{$c0%}:%{$c3%}%l%{$c6%}->%{$(zsh_path)%} %{$c0%}(%{$c5%}%?%{$c0%})" PROMPT='${vcs_info_msg_0_}%{$bg_bold[red]%}%{$fg_bold[blue]%}D%{$fg_bold[black]%}IRTY%{$reset_color%} @@ -130,8 +127,5 @@ prompt_jnrowe_precmd () { PROMPT='${vcs_info_msg_0_} %{$fg_bold[green]%}%p%{$reset_color%}${dir_status}%{$reset_color%} > ' -fi + fi } - - -# vim: set ft=zsh sw=2 et tw=0: