Merge pull request #932 from luukvbaal/preview-tui

.iconlookup: refactor
This commit is contained in:
Mischievous Meerkat 2021-04-01 20:28:42 +05:30 committed by GitHub
commit df6dbbb84c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,24 +6,17 @@
# Usage # Usage
# 1. Set colors and/or icons to your liking # 1. Set colors and/or icons to your liking
# 2. preview-tui-ext uses the script to prepend icon to directory listings # 2. Pipe any directory listing to iconlookup and it will output prepended icons
# 3. Aditionally you can consider adding it to your PATH and/or FZF_DEFAULT_COMMAND to # 3. preview-tui-ext uses the script to prepend icon to directory listings
# 4. Aditionally you can consider adding it to your PATH and/or FZF_DEFAULT_COMMAND to
# make it work with various fzf plugins (make sure you also add --ansi to your FZF_DEFAULT_OPTS) # make it work with various fzf plugins (make sure you also add --ansi to your FZF_DEFAULT_OPTS)
# Shell: POSIX compliant # Shell: POSIX compliant
# Author: Luuk van Baal # Author: Luuk van Baal (https://github.com/luukvbaal/iconlookup)
icon_lookup() { icon_lookup() {
awk 'BEGIN { FS="." } awk 'BEGIN {
{
if ($0 ~/^\.$/)
next
ent[NR] = $0
ext[NR] = $NF
limit = ENVIRON["limit"]
}
END {
# Set your ANSI colorscheme below (https://en.wikipedia.org/wiki/ANSI_escape_code#Colors). # Set your ANSI colorscheme below (https://en.wikipedia.org/wiki/ANSI_escape_code#Colors).
# Default uses standard nnn icon colors, 8 and 24-bit nord themes are commented out. # Default uses standard nnn icon colors, 8 and 24-bit nord themes are commented out.
colordepth=8 #colordepth=8 #colordepth=24 colordepth=8 #colordepth=8 #colordepth=24
@ -48,18 +41,6 @@ color_ruby=160 #color_ruby=150 #color_ruby="163;190;140"
color_scala=196 #color_scala=139 #color_scala="143;188;187" color_scala=196 #color_scala=139 #color_scala="143;188;187"
color_vim=28 #color_vim=109 #color_vim="143;188;187" color_vim=28 #color_vim=109 #color_vim="143;188;187"
switch (colordepth) {
case "4":
escape="\033["
break;
case "8":
escape="\033[38;5;"
break;
case "24":
escape="\033[38;2;"
break;
}
# icons[][1] contains icon and icons[][2] contains color # icons[][1] contains icon and icons[][2] contains color
icons["directory"][1] = ""; icons["directory"][2] = color_default icons["directory"][1] = ""; icons["directory"][2] = color_default
icons["file"][1] = ""; icons["file"][2] = color_default icons["file"][1] = ""; icons["file"][2] = color_default
@ -303,35 +284,53 @@ color_vim=28 #color_vim=109 #color_vim="143;188;187"
# z # z
icons["zip"][1] = icons["archive"][1]; icons["zip"][2] = icons["archive"][2] icons["zip"][1] = icons["archive"][1]; icons["zip"][2] = icons["archive"][2]
# Print icons, set color and bold directories by using ansi escape codes FS = "."
for (i in ent) limit = ENVIRON["limit"]
if (ext[i] in icons) switch (colordepth) {
printcolor(icons[ext[i]][1], icons[ext[i]][2], color_filetxt, ent[i], "10") case "4":
else escape="\033["
switch (substr(ent[i], length(ent[i]), 1)) {
case "/":
printcolor(icons["directory"][1], color_default, color_dirtxt, ent[i], "1")
break; break;
case "*": case "8":
printcolor(icons["exe"][1], color_default, color_filetxt, ent[i], "10") escape="\033[38;5;"
break; break;
case "|": case "24":
printcolor(icons["pipe"][1], color_default, color_filetxt, ent[i], "10") escape="\033[38;2;"
break; break;
case "=":
printcolor(icons["socket"][1], color_default, color_filetxt, ent[i], "10")
break;
case ">":
printcolor(icons["door"][1], color_default, color_filetxt, ent[i], "10")
break;
default:
printcolor(icons["file"][1], color_default, color_filetxt, ent[i], "10")
} }
} }
{
# dont print cwd . and leading ./ from tree -f
if ($0 ~/^\.$/)
next
ent = ($0 ~/^\.\//) ? substr($0, 3, length($0) - 2) : $0
ext = $NF
# Print icons, set color and bold directories by using ansi escape codes
if (ext in icons)
printcolor(icons[ext][1], icons[ext][2], color_filetxt, ent, "10")
else
switch (substr(ent, length(ent), 1)) {
case "/":
printcolor(icons["directory"][1], color_default, color_dirtxt, ent, "1")
break;
case "*":
printcolor(icons["exe"][1], color_default, color_filetxt, ent, "10")
break;
case "|":
printcolor(icons["pipe"][1], color_default, color_filetxt, ent, "10")
break;
case "=":
printcolor(icons["socket"][1], color_default, color_filetxt, ent, "10")
break;
case ">":
printcolor(icons["door"][1], color_default, color_filetxt, ent, "10")
break;
default:
printcolor(icons["file"][1], color_default, color_filetxt, ent, "10")
}
}
function printcolor(i, c, d, n, b) { function printcolor(i, c, d, n, b) {
if (limit != "" && length(n) + 2 > limit) if (limit != "" && length(n) + 2 > limit)
n = substr(n, 1, ENVIRON["limit"] - 2) n = substr(n, 1, limit - 2)
printf "\033[0m" printf "\033[0m"
printf "%s%s;%sm%s %s%sm%s\n", escape, c, b, i, escape, d, n printf "%s%s;%sm%s %s%sm%s\n", escape, c, b, i, escape, d, n
}' }'