Skip to content

Commit

Permalink
[lscolors] Package improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Mar 3, 2023
1 parent 6c7414f commit 1ad7130
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .scripts/packages.list
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* + events
* + directio
* + fmtc
* + fmtc/lscolors
* + fmtutil
* + fmtutil/table
* + fsutil
Expand All @@ -24,8 +23,9 @@
* + knf/validators/network
* + knf/validators/regexp
* + knf/validators/system
* + log
* + lock
* + log
* + lscolors
* + mathutil
L + netutil
* + options
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### 12.61.0

* `[lscolors]` Sub-package moved from `fmtc` to root of the package
* `[lscolors]` `GetColor` returns colors for types of objects (_like directory, block device, link…_)
* `[lscolors]` Added flag `DisableColors` for disabling all colors in output

### 12.60.1

* `[initsystem]` Improved systemd support
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Currently we support Linux and macOS (_except some packages_). All packages have

### Installation

Make sure you have a working Go 1.17+ workspace (_[instructions](https://golang.org/doc/install)_), then:
Make sure you have a working Go 1.18+ workspace (_[instructions](https://golang.org/doc/install)_), then:

```
go get github.com/essentialkaos/ek/v12
Expand All @@ -53,7 +53,6 @@ go get -u github.com/essentialkaos/ek/v12
* [`events`](https://kaos.sh/g/ek.v12/events) — Package provides methods and structs for creating event-driven systems
* [`directio`](https://kaos.sh/g/ek.v12/directio) — Package provides methods for reading/writing files with direct io
* [`fmtc`](https://kaos.sh/g/ek.v12/fmtc) — Package provides methods similar to fmt for colored output
* [`fmtc/lscolors`](https://kaos.sh/g/ek.v12/fmtc/lscolors) — Package provides methods for colorizing file names based on colors from dircolors
* [`fmtutil`](https://kaos.sh/g/ek.v12/fmtutil) — Package provides methods for output formatting
* [`fmtutil/table`](https://kaos.sh/g/ek.v12/fmtutil/table) — Package contains methods and structs for rendering data in tabular format
* [`fsutil`](https://kaos.sh/g/ek.v12/fsutil) — Package provides methods for working with files on POSIX compatible systems (BSD/Linux/macOS)
Expand All @@ -64,6 +63,7 @@ go get -u github.com/essentialkaos/ek/v12
* [`knf`](https://kaos.sh/g/ek.v12/knf) — Package provides methods for working with configuration files in [KNF format](https://kaos.sh/knf-spec)
* [`log`](https://kaos.sh/g/ek.v12/log) — Package with an improved logger
* [`lock`](https://kaos.sh/g/ek.v12/lock) — Package provides methods for working with lock files
* [`lscolors`](https://kaos.sh/g/ek.v12/lscolors) — Package provides methods for colorizing file names based on colors from dircolors
* [`mathutil`](https://kaos.sh/g/ek.v12/mathutil) — Package provides some additional math methods
* [`netutil`](https://kaos.sh/g/ek.v12/netutil) — Package provides methods for working with network
* [`options`](https://kaos.sh/g/ek.v12/options) — Package provides methods for working with command-line options
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.60.1"
const VERSION = "12.61.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ExampleGetColor() {
colorSeq := GetColor(file)

fmt.Printf(
"%s/"+colorSeq+"%s"+RESET_SEQ+"\n",
"%s/"+colorSeq+"%s"+GetColor(RESET)+"\n",
dir, file,
)
}
Expand Down
36 changes: 29 additions & 7 deletions fmtc/lscolors/lscolors.go → lscolors/lscolors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// RESET_SEQ is ANSI reset sequence
const RESET_SEQ = "\033[0m"
const (
RESET = "rs" // Reset
DIR = "di" // Directory
LINK = "ln" // Symbolic link
FIFO = "pi" // Pipe
SOCK = "so" // Socket
BLK = "bd" // Block device driver
CHR = "cd" // Character device driver
STICKY = "st" // Dir with the sticky bit set (+t) and not other-writable
EXEC = "ex" // Executable files
)

// ////////////////////////////////////////////////////////////////////////////////// //

Expand All @@ -29,16 +38,25 @@ var initialized bool

// ////////////////////////////////////////////////////////////////////////////////// //

// DisableColors disables all colors in output
var DisableColors = os.Getenv("NO_COLOR") != ""

// ////////////////////////////////////////////////////////////////////////////////// //

// GetColor returns ANSI control sequence with color for given file
func GetColor(file string) string {
if !initialized {
initialize()
}

if len(colorMap) == 0 {
if DisableColors || len(colorMap) == 0 {
return ""
}

if colorMap[file] != "" {
return "\033[" + colorMap[file] + "m"
}

for glob, color := range colorMap {
isMatch, _ := path.Match(glob, file)

Expand All @@ -58,7 +76,7 @@ func Colorize(file string) string {
return file
}

return colorSeq + file + RESET_SEQ
return colorSeq + file + "\033[0m"
}

// Colorize return path with ANSI control sequences
Expand All @@ -70,7 +88,7 @@ func ColorizePath(fullPath string) string {
return fullPath
}

return colorSeq + fullPath + RESET_SEQ
return colorSeq + fullPath + "\033[0m"
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -79,16 +97,20 @@ func ColorizePath(fullPath string) string {
func initialize() {
initialized = true

if DisableColors {
return
}

lsColors := os.Getenv("LS_COLORS")

if lsColors == "" {
return
}

colorMap = make(map[string]string)
colorMap = map[string]string{RESET: "0"}

for _, key := range strings.Split(lsColors, ":") {
if !strings.HasPrefix(key, "*") || !strings.ContainsRune(key, '=') {
if !strings.ContainsRune(key, '=') || !strings.ContainsRune(key, ';') {
continue
}

Expand Down
9 changes: 9 additions & 0 deletions fmtc/lscolors/lscolors_test.go → lscolors/lscolors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (ls *LSCSuite) TestColorize(c *C) {

os.Setenv("LS_COLORS", "rs=0:di=01;38;5;75:ln=38;5;141:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=38;5;202:*.txt=38;5;178:*.bz=38;5;105:")

c.Assert(GetColor(RESET), Equals, "\x1b[0m")
c.Assert(GetColor(DIR), Equals, "\x1b[01;38;5;75m")

c.Assert(GetColor("test.log"), Equals, "")
c.Assert(GetColor("test.txt"), Equals, "\x1b[38;5;178m")
c.Assert(GetColor("test.tar.bz"), Equals, "\x1b[38;5;105m")
Expand All @@ -47,6 +50,12 @@ func (ls *LSCSuite) TestColorize(c *C) {

colorMap, initialized = nil, false

DisableColors = true
c.Assert(GetColor("test.txt"), Equals, "")
DisableColors = false

colorMap, initialized = nil, false

os.Setenv("LS_COLORS", "")

c.Assert(Colorize("test.log"), Equals, "test.log")
Expand Down

0 comments on commit 1ad7130

Please sign in to comment.