1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
package tree
import (
"fmt"
"os"
"path/filepath"
"strings"
)
const Escape = "\x1b"
const (
Reset int = 0
// Not used, remove.
Bold int = 1
Black int = iota + 28
Red
Green
Yellow
Blue
Magenta
Cyan
White
)
// ANSIColor
func ANSIColor(node *Node, s string) string {
var style string
var mode = node.Mode()
var ext = filepath.Ext(node.Name())
switch {
case contains([]string{".bat", ".btm", ".cmd", ".com", ".dll", ".exe"}, ext):
style = "1;32"
case contains([]string{".arj", ".bz2", ".deb", ".gz", ".lzh", ".rpm",
".tar", ".taz", ".tb2", ".tbz2", ".tbz", ".tgz", ".tz", ".tz2", ".z",
".zip", ".zoo"}, ext):
style = "1;31"
case contains([]string{".asf", ".avi", ".bmp", ".flac", ".gif", ".jpg",
"jpeg", ".m2a", ".m2v", ".mov", ".mp3", ".mpeg", ".mpg", ".ogg", ".ppm",
".rm", ".tga", ".tif", ".wav", ".wmv",
".xbm", ".xpm"}, ext):
style = "1;35"
case node.IsDir() || mode&os.ModeDir != 0:
style = "1;34"
case mode&os.ModeNamedPipe != 0:
style = "40;33"
case mode&os.ModeSocket != 0:
style = "40;1;35"
case mode&os.ModeDevice != 0 || mode&os.ModeCharDevice != 0:
style = "40;1;33"
case mode&os.ModeSymlink != 0:
if _, err := filepath.EvalSymlinks(node.path); err != nil {
style = "40;1;31"
} else {
style = "1;36"
}
case mode&modeExecute != 0:
style = "1;32"
default:
return s
}
return fmt.Sprintf("%s[%sm%s%s[%dm", Escape, style, s, Escape, Reset)
}
// case-insensitive contains helper
func contains(slice []string, str string) bool {
for _, val := range slice {
if val == strings.ToLower(str) {
return true
}
}
return false
}
// TODO: HTMLColor
|