Skip to content

Commit

Permalink
Palette
Browse files Browse the repository at this point in the history
  • Loading branch information
devemlight committed Feb 7, 2024
1 parent ebbfe90 commit 772febf
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 37 deletions.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

## 👨‍💻 Installation

This package uses a pipeline and allows you to color the **Docker** output.
You can find all compiled binaries on the [releases](https://github.com/devemio/docker-color-output/releases) page.
This package uses a pipeline and allows you to color the **Docker** output. You can find all compiled binaries
on the [releases](https://github.com/devemio/docker-color-output/releases) page.

#### 🍏 Mac

Expand All @@ -34,6 +34,42 @@ sudo apt install docker-color-output

You can download the Windows build from the [releases](https://github.com/devemio/docker-color-output/releases) page.

## 😺 Configuration

You can customize the color palette to your liking, to do this you need to run `docker-color-output` with the `-c`
flag and specify the path to the configuration file. You can overwrite only the colors you want, the rest will be
used by default.

```shell
docker-color-output -c ~/.config/docker-color-output/config.json
```

Default configuration file:

```json
{
"colors": {
"reset": "\u001b[0m",
"black": "\u001b[0;30m",
"darkGray": "\u001b[1;30m",
"red": "\u001b[0;31m",
"lightRed": "\u001b[1;31m",
"green": "\u001b[0;32m",
"lightGreen": "\u001b[1;32m",
"brown": "\u001b[0;33m",
"yellow": "\u001b[1;33m",
"blue": "\u001b[0;34m",
"lightBlue": "\u001b[1;34m",
"purple": "\u001b[0;35m",
"lightPurple": "\u001b[1;35m",
"cyan": "\u001b[0;36m",
"lightCyan": "\u001b[1;36m",
"lightGray": "\u001b[0;37m",
"white": "\u001b[1;37m"
}
}
```

## 📚 Usage

### 🪄 Aliases
Expand Down
10 changes: 10 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
package main

import (
"fmt"
"os"

"github.com/devemio/docker-color-output/internal/app"
"github.com/devemio/docker-color-output/internal/config"
"github.com/devemio/docker-color-output/internal/stdin"
"github.com/devemio/docker-color-output/internal/stdout"
"github.com/devemio/docker-color-output/pkg/color"
)

func main() {
Expand All @@ -18,6 +21,13 @@ func main() {
}

func run() error {
cfg, err := config.Get()
if err != nil {
return fmt.Errorf("cfg: %w", err)
}

color.SetPalette(color.Palette(cfg.Colors))

in, err := stdin.Get()
if err != nil {
return err //nolint:wrapcheck
Expand Down
76 changes: 76 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package config

import (
"encoding/json"
"flag"
"fmt"
"os"
)

type Config struct {
Colors Colors `json:"colors"`
}

type Colors struct {
Reset string `json:"reset"`
Black string `json:"black"`
DarkGray string `json:"darkGray"`
Red string `json:"red"`
LightRed string `json:"lightRed"`
Green string `json:"green"`
LightGreen string `json:"lightGreen"`
Brown string `json:"brown"`
Yellow string `json:"yellow"`
Blue string `json:"blue"`
LightBlue string `json:"lightBlue"`
Purple string `json:"purple"`
LightPurple string `json:"lightPurple"`
Cyan string `json:"cyan"`
LightCyan string `json:"lightCyan"`
LightGray string `json:"lightGray"`
White string `json:"white"`
}

func Get() (Config, error) {
cfg := createDefault()

cfgPath := flag.String("c", "", "Config location")
flag.Parse()

if *cfgPath != "" {
data, err := os.ReadFile(*cfgPath)
if err != nil {
return Config{}, fmt.Errorf("read file: %w", err)
}

if err = json.Unmarshal(data, &cfg); err != nil {
return Config{}, fmt.Errorf("unmarshal: %w", err)
}
}

return cfg, nil
}

func createDefault() Config {
return Config{
Colors: Colors{
Reset: "\u001B[0m",
Black: "\u001B[0;30m",
DarkGray: "\u001B[1;30m",
Red: "\u001B[0;31m",
LightRed: "\u001B[1;31m",
Green: "\u001B[0;32m",
LightGreen: "\u001B[1;32m",
Brown: "\u001B[0;33m",
Yellow: "\u001B[1;33m",
Blue: "\u001B[0;34m",
LightBlue: "\u001B[1;34m",
Purple: "\u001B[0;35m",
LightPurple: "\u001B[1;35m",
Cyan: "\u001B[0;36m",
LightCyan: "\u001B[1;36m",
LightGray: "\u001B[0;37m",
White: "\u001B[1;37m",
},
}
}
95 changes: 60 additions & 35 deletions pkg/color/color.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,110 @@
package color

const (
reset = "\033[0m"
black = "\033[0;30m"
darkGray = "\033[1;30m"
red = "\033[0;31m"
lightRed = "\033[1;31m"
green = "\033[0;32m"
lightGreen = "\033[1;32m"
brown = "\033[0;33m"
yellow = "\033[1;33m"
blue = "\033[0;34m"
lightBlue = "\033[1;34m"
purple = "\033[0;35m"
lightPurple = "\033[1;35m"
cyan = "\033[0;36m"
lightCyan = "\033[1;36m"
lightGray = "\033[0;37m"
white = "\033[1;37m"
)
type Palette struct {
Reset string
Black string
DarkGray string
Red string
LightRed string
Green string
LightGreen string
Brown string
Yellow string
Blue string
LightBlue string
Purple string
LightPurple string
Cyan string
LightCyan string
LightGray string
White string
}

//nolint:gochecknoglobals
var palette = Palette{
Reset: "\u001B[0m",
Black: "\u001B[0;30m",
DarkGray: "\u001B[1;30m",
Red: "\u001B[0;31m",
LightRed: "\u001B[1;31m",
Green: "\u001B[0;32m",
LightGreen: "\u001B[1;32m",
Brown: "\u001B[0;33m",
Yellow: "\u001B[1;33m",
Blue: "\u001B[0;34m",
LightBlue: "\u001B[1;34m",
Purple: "\u001B[0;35m",
LightPurple: "\u001B[1;35m",
Cyan: "\u001B[0;36m",
LightCyan: "\u001B[1;36m",
LightGray: "\u001B[0;37m",
White: "\u001B[1;37m",
}

func SetPalette(p Palette) {
palette = p
}

func Black(value string) string {
return black + value + reset
return palette.Black + value + palette.Reset
}

func DarkGray(value string) string {
return darkGray + value + reset
return palette.DarkGray + value + palette.Reset
}

func Red(value string) string {
return red + value + reset
return palette.Red + value + palette.Reset
}

func LightRed(value string) string {
return lightRed + value + reset
return palette.LightRed + value + palette.Reset
}

func Green(value string) string {
return green + value + reset
return palette.Green + value + palette.Reset
}

func LightGreen(value string) string {
return lightGreen + value + reset
return palette.LightGreen + value + palette.Reset
}

func Brown(value string) string {
return brown + value + reset
return palette.Brown + value + palette.Reset
}

func Yellow(value string) string {
return yellow + value + reset
return palette.Yellow + value + palette.Reset
}

func Blue(value string) string {
return blue + value + reset
return palette.Blue + value + palette.Reset
}

func LightBlue(value string) string {
return lightBlue + value + reset
return palette.LightBlue + value + palette.Reset
}

func Purple(value string) string {
return purple + value + reset
return palette.Purple + value + palette.Reset
}

func LightPurple(value string) string {
return lightPurple + value + reset
return palette.LightPurple + value + palette.Reset
}

func Cyan(value string) string {
return cyan + value + reset
return palette.Cyan + value + palette.Reset
}

func LightCyan(value string) string {
return lightCyan + value + reset
return palette.LightCyan + value + palette.Reset
}

func LightGray(value string) string {
return lightGray + value + reset
return palette.LightGray + value + palette.Reset
}

func White(value string) string {
return white + value + reset
return palette.White + value + palette.Reset
}

0 comments on commit 772febf

Please sign in to comment.