Skip to content

Commit 1e0174c

Browse files
committed
feat: tidy up, for showing color in printenv, ls
1 parent 042580b commit 1e0174c

1 file changed

Lines changed: 62 additions & 64 deletions

File tree

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
# shellcheck disable=SC2154,SC2298,SC2296
22

3+
# shellcheck disable=SC2154,SC2298,SC2296
4+
5+
# Fungsi: Tampilkan semua warna 256 di terminal
36
colors() {
4-
# Prints all tput colors to terminal
5-
for i in {0..255}; do print -Pn "%K{$i} %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$'\n'}; done
7+
for i in {0..255}; do
8+
printf "\e[48;5;%sm \e[0m\e[38;5;%sm%03d\e[0m " "$i" "$i" "$i"
9+
(( i % 6 == 3 )) && echo
10+
done
611
}
712

13+
# Fungsi: Tampilkan skema warna pribadi
814
mycolors() {
9-
# Prints my own color scheme to terminal
1015
printf "%s\n" "${underline} This is underlined text ${reset}"
1116
printf "%s\n" "${reverse} This is reversed text ${reset}"
12-
printf "%s\n" "${gray} This is gray text${reset}"
17+
printf "%s\n" "${gray} This is gray text ${reset}"
1318
printf "%s\n" "${white} This is white text ${reset}"
1419
printf "%s\n" "${bold} This is bold text ${reset}"
1520
printf "%s\n" "${blue} This is blue text ${reset}"
16-
printf "%s\n" "${yellow} This is yellow text"
17-
printf "%s\n" "${purple} This is purple text${reset}"
21+
printf "%s\n" "${yellow} This is yellow text ${reset}"
22+
printf "%s\n" "${purple} This is purple text ${reset}"
1823
printf "%s\n" "${red} This is red text ${reset}"
19-
printf "%s\n" "${green} This is green text${reset}"
24+
printf "%s\n" "${green} This is green text ${reset}"
2025

2126
printf "%s\n" "${white}${reverse} This is white reversed text ${reset}"
22-
printf "%s\n" "${gray}${reverse} This is gray reversed text${reset}"
27+
printf "%s\n" "${gray}${reverse} This is gray reversed text ${reset}"
2328
printf "%s\n" "${blue}${reverse} This is blue reversed text ${reset}"
24-
printf "%s\n" "${yellow}${reverse} This is yellow reversed text"
25-
printf "%s\n" "${purple}${reverse} This is purple reversed text${reset}"
29+
printf "%s\n" "${yellow}${reverse} This is yellow reversed text ${reset}"
30+
printf "%s\n" "${purple}${reverse} This is purple reversed text ${reset}"
2631
printf "%s\n" "${red}${reverse} This is red reversed text ${reset}"
27-
printf "%s\n" "${green}${reverse} This is green reversed text${reset}"
32+
printf "%s\n" "${green}${reverse} This is green reversed text ${reset}"
2833
}
2934

30-
# Add color to terminal
35+
# Aktifkan warna CLI
3136
export CLICOLOR=1
32-
#export LSCOLORS="ExDxCxDxCxegedabagacad"
33-
#export LSCOLORS="GxFxCxDxBxegedabagaced"
34-
#export LSCOLORS="ExFxBxDxCxegedabagacad"
35-
export LSCOLORS="GxFxCxDxCxegedabagaced"
36-
# LESS man page colors
37+
38+
# Warna `ls` (macOS pakai LSCOLORS, Linux pakai dircolors)
39+
if [[ "$OSTYPE" == darwin* ]]; then
40+
export LSCOLORS="GxFxCxDxCxegedabagaced"
41+
else
42+
# Gunakan default dircolors jika ada
43+
if command -v dircolors &>/dev/null; then
44+
eval "$(dircolors -b)"
45+
fi
46+
fi
47+
48+
# Warna untuk `less` (man page)
3749
export LESS_TERMCAP_mb=$'\E[01;31m'
3850
export LESS_TERMCAP_md=$'\E[01;31m'
3951
export LESS_TERMCAP_me=$'\E[0m'
@@ -42,69 +54,55 @@ export LESS_TERMCAP_so=$'\E[01;44;33m'
4254
export LESS_TERMCAP_ue=$'\E[0m'
4355
export LESS_TERMCAP_us=$'\E[01;32m'
4456

45-
if [[ $COLORTERM == gnome-* && $TERM == xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
57+
# Set TERM untuk dukungan 256 warna
58+
if [[ $COLORTERM == gnome-* && $TERM == xterm ]] && infocmp gnome-256color &>/dev/null; then
4659
export TERM=gnome-256color
47-
elif infocmp xterm-256color >/dev/null 2>&1; then
48-
export TERM=xterm-256color
4960
else
5061
export TERM=xterm-256color
5162
fi
5263

53-
# Set colors
64+
# Fungsi: Inisialisasi warna
5465
_setColors_() {
55-
# DESC: Sets colors use for alerts.
56-
# ARGS: None
57-
# OUTS: None
58-
# USAGE: echo "${blue}Some text${reset}"
59-
6066
if tput setaf 1 &>/dev/null; then
6167
bold=$(tput bold)
6268
underline=$(tput smul)
6369
reverse=$(tput rev)
6470
reset=$(tput sgr0)
6571

66-
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
67-
white=$(tput setaf 231)
68-
blue=$(tput setaf 38)
69-
yellow=$(tput setaf 11)
70-
green=$(tput setaf 82)
71-
red=$(tput setaf 9)
72-
purple=$(tput setaf 171)
73-
gray=$(tput setaf 250)
74-
else
75-
white=$(tput setaf 7)
76-
blue=$(tput setaf 38)
77-
yellow=$(tput setaf 3)
78-
green=$(tput setaf 2)
79-
red=$(tput setaf 9)
80-
purple=$(tput setaf 13)
81-
gray=$(tput setaf 7)
72+
# Mapping warna default
73+
white_code=7 blue_code=4 yellow_code=3 green_code=2 red_code=1 purple_code=5 gray_code=7
74+
75+
# Gunakan kode warna 256 jika tersedia
76+
if [[ $(tput colors 2>/dev/null) -ge 256 ]]; then
77+
white_code=231 blue_code=38 yellow_code=11 green_code=82 red_code=9 purple_code=171 gray_code=250
8278
fi
79+
80+
# Set warna
81+
white=$(tput setaf "$white_code")
82+
blue=$(tput setaf "$blue_code")
83+
yellow=$(tput setaf "$yellow_code")
84+
green=$(tput setaf "$green_code")
85+
red=$(tput setaf "$red_code")
86+
purple=$(tput setaf "$purple_code")
87+
gray=$(tput setaf "$gray_code")
8388
else
84-
bold="\033[4;37m"
89+
# Fallback escape ANSI manual
90+
bold="\033[1m"
91+
underline="\033[4m"
92+
reverse="\033[7m"
8593
reset="\033[0m"
86-
underline="\033[4;37m"
87-
reverse=""
88-
white="\033[0;37m"
89-
blue="\033[0;34m"
90-
yellow="\033[0;33m"
91-
green="\033[1;32m"
92-
red="\033[0;31m"
93-
purple="\033[0;35m"
94-
gray="\033[0;37m"
94+
white="\033[37m"
95+
blue="\033[34m"
96+
yellow="\033[33m"
97+
green="\033[32m"
98+
red="\033[31m"
99+
purple="\033[35m"
100+
gray="\033[37m"
95101
fi
96102
}
97-
_setColors_
98103

99-
export bold
100-
export underline
101-
export reverse
102-
export reset
103-
export white
104-
export blue
105-
export yellow
106-
export green
107-
export red
108-
export purple
109-
export gray
104+
# Panggil inisialisasi warna
105+
_setColors_
110106

107+
# Ekspor variabel warna
108+
export bold underline reverse reset white blue yellow green red purple gray

0 commit comments

Comments
 (0)