Skip to content

Commit

Permalink
jjp: Add ability to suppress colors via $NO_COLOR environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronNGi committed Jul 31, 2021
1 parent ed1150e commit 32f1166
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ variables are also available to the called program:
|----------------|-------------|
| `JJ_SERVERLOG` | When not empty, `jjp(1)` will print status messages using the default color.
| `JJ_DEBUG` | When not emtpy, `jjc(1)` will print debug output.
| `NO_COLOR` | Prevent `jjp(1)` from printing colors.


Log Format
Expand Down
91 changes: 50 additions & 41 deletions jjp
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#!/usr/bin/awk -f

BEGIN {
color["red"] = 31
color["white"] = 37
color["default"] = 0
color["dark_gray"] = "38;5;8"

color["header"] = color["default"]
color["time"] = color["dark_gray"]
color["time_highlight"] = color["red"]
color["others_msg"] = color["default"]
color["self_nick"] = color["white"]
color["self_msg"] = color["default"]
color["status_nick"] = color["dark_gray"]
color["status_msg"] = color["dark_gray"]
color["status_msg_srv"] = color["default"]
no_color = "NO_COLOR" in ENVIRON

if (!no_color) {
color["red"] = "\033[31m"
color["white"] = "\033[37m"
color["default"] = "\033[m"
color["dark_gray"] = "\033[38;5;8m"
color["reset"] = "\033[m"

color["header"] = color["default"]
color["time"] = color["dark_gray"]
color["time_highlight"] = color["red"]
color["others_msg"] = color["default"]
color["self_nick"] = color["white"]
color["self_msg"] = color["default"]
color["status_nick"] = color["dark_gray"]
color["status_msg"] = color["dark_gray"]
color["status_msg_srv"] = color["default"]
}
}

function isleap(year)
Expand Down Expand Up @@ -82,8 +87,8 @@ function tz( cmd, date, h, m)

function print_header(text)
{
printf "%s\033[%sm%s\033[0m",
(NR != 1 ? "\n\n" : ""), color["header"], text
printf "%s%s%s%s",
(NR != 1 ? "\n\n" : ""), color["header"], text, color["reset"]
}

BEGIN {
Expand Down Expand Up @@ -137,34 +142,36 @@ BEGIN {
# When JJ_SERVERLOG is set, print messages without nick and in a
# different color.
if (ENVIRON["JJ_SERVERLOG"] != "") {
printf "\n\033[%sm%s\033[%sm %s",
time_color, time, color["status_msg_srv"], msg
printf "\n%s%s%s %s%s",
time_color, time, color["status_msg_srv"], msg, color["reset"]
fflush("/dev/stdout")
next
}

# Status message.
if (nick == "-") {
nick_color = color["status_nick"]
msg_color = color["status_msg"]

# My message.
} else if (index(type, "*")) {
nick_color = color["self_nick"]
msg_color = color["self_msg"]

# Other message.
} else {
# Calculate nick color if necessary.
if ((nick_color = nick_colors[nick]) == "") {
i = 1
sum = 0
while (char = substr(nick, i++, 1))
sum = sum + char_to_num[char]

nick_color = nick_colors[nick] = "38;5;" sum % 232
if (!no_color) {
# Status message.
if (nick == "-") {
nick_color = color["status_nick"]
msg_color = color["status_msg"]

# My message.
} else if (index(type, "*")) {
nick_color = color["self_nick"]
msg_color = color["self_msg"]

# Other message.
} else {
# Calculate nick color if necessary.
if ((nick_color = nick_colors[nick]) == "") {
i = 1
sum = 0
while (char = substr(nick, i++, 1))
sum = sum + char_to_num[char]

nick_color = nick_colors[nick] = "38;5;" sum % 232
}
msg_color = color["others_msg"]
}
msg_color = color["others_msg"]
}

# Message grouping.
Expand All @@ -180,7 +187,9 @@ BEGIN {
nick = "*"
}

printf "\n\033[%sm%s\033[0m \033[%sm%10s\033[0m \033[%sm%s\033[0m",
time_color, time, nick_color, nick, msg_color, msg
printf "\n%s%s%s %s%10s%s %s%s%s",
time_color, time, color["reset"],
nick_color, nick, color["reset"],
msg_color, msg, color["reset"]
fflush("/dev/stdout")
} END { print "" }

0 comments on commit 32f1166

Please sign in to comment.