Skip to content

Instantly share code, notes, and snippets.

@sebastiancarlos
Last active October 19, 2023 04:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sebastiancarlos/f712954caa8914032f6ebc867e9f8e4f to your computer and use it in GitHub Desktop.
Save sebastiancarlos/f712954caa8914032f6ebc867e9f8e4f to your computer and use it in GitHub Desktop.
Cross-app URL aliases (like shell aliases) powered by Nginx.
#!/usr/bin/env bash
# All my gist code is licensed under the MIT license.
# Video demo: https://www.youtube.com/watch?v=y542zPAPeG4
# colorcat
# - cats a file, but if any line contains N hex colors, it appends the colors
# (rendered as ansi escape sequences) to the end of the line.
# - input can be stdin, a file, or a hex color in plain text
if [[ "$#" -eq 1 && ! -f "$1" ]]; then
echo "$1"
else
cat "$@"
fi | while read -r line; do
colors=""
for word in $line; do
if [[ "$word" =~ ^[^A-Fa-f0-9]*#?([A-Fa-f0-9]{6})[^A-Fa-f0-9]*$ ]]; then
hex=${BASH_REMATCH[1]}
r=$((16#${hex:0:2}))
g=$((16#${hex:2:2}))
b=$((16#${hex:4:2}))
truecolor="\033[48;2;${r};${g};${b}m"
reset="\033[0m"
colors="${colors}${truecolor} ${reset} "
fi
done
echo -e "$line $colors"
done
@marmalodak
Copy link

I like this script.

My editor complained though:

 ❯ shellcheck colorcat.bash

In colorcat.bash line 16:
  local colors=""
  ^---^ SC2168 (error): 'local' is only valid in functions.


In colorcat.bash line 20:
      local r=$((16#${hex:0:2}))
      ^---^ SC2168 (error): 'local' is only valid in functions.


In colorcat.bash line 21:
      local g=$((16#${hex:2:2}))
      ^---^ SC2168 (error): 'local' is only valid in functions.


In colorcat.bash line 22:
      local b=$((16#${hex:4:2}))
      ^---^ SC2168 (error): 'local' is only valid in functions.


In colorcat.bash line 23:
      local truecolor="\033[48;2;${r};${g};${b}m"
      ^---^ SC2168 (error): 'local' is only valid in functions.


In colorcat.bash line 24:
      local reset="\033[0m"
      ^---^ SC2168 (error): 'local' is only valid in functions.

For more information:
  https://www.shellcheck.net/wiki/SC2168 -- 'local' is only valid in functions. 

@sebastiancarlos
Copy link
Author

@marmalodak thanks, fixed

@hrdkmishra
Copy link

fish version , thank you for inspiration i just tried to rewrite it in fish

@sebastiancarlos
Copy link
Author

@hrdkmishra hey that looks amazing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment