File: formatting.go

package info (click to toggle)
hut 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,088 kB
  • sloc: makefile: 60; sh: 14
file content (134 lines) | stat: -rw-r--r-- 2,272 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package termfmt

import (
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
	"sync"

	"golang.org/x/term"
)

var initIsTerminal sync.Once
var isTerminal bool

type Style string

type RGB struct {
	Red, Green, Blue uint8
}

const (
	Bold Style = "bold"
	Dim  Style = "dim"

	Red    Style = "red"
	Green  Style = "green"
	Yellow Style = "yellow"
	Blue   Style = "blue"

	DarkYellow Style = "dark-yellow"
)

func (style Style) String(s string) string {
	if !IsTerminal() {
		return s
	}

	switch style {
	case Bold:
		return fmt.Sprintf("\033[01m%s\033[0m", s)
	case Dim:
		return fmt.Sprintf("\033[02m%s\033[0m", s)
	case Red:
		return fmt.Sprintf("\033[91m%s\033[0m", s)
	case Green:
		return fmt.Sprintf("\033[92m%s\033[0m", s)
	case Yellow:
		return fmt.Sprintf("\033[93m%s\033[0m", s)
	case Blue:
		return fmt.Sprintf("\033[94m%s\033[0m", s)
	case DarkYellow:
		return fmt.Sprintf("\033[33m%s\033[0m", s)
	default:
		return s
	}
}

func HexString(s string, fg string, bg string) string {
	if !IsTerminal() {
		return s
	}

	return RGBString(s, HexToRGB(fg), HexToRGB(bg))
}

func RGBString(s string, fg, bg RGB) string {
	if !IsTerminal() {
		return s
	}

	return fmt.Sprintf("\033[38;2;%d;%d;%dm\033[48;2;%d;%d;%dm%s\033[0m",
		fg.Red, fg.Green, fg.Blue, bg.Red, bg.Green, bg.Blue, s)
}

func (style Style) Sprint(args ...interface{}) string {
	return style.String(fmt.Sprint(args...))
}

func (style Style) Sprintf(format string, args ...interface{}) string {
	return style.String(fmt.Sprintf(format, args...))
}

func HexToRGB(hex string) RGB {
	var rgb RGB
	hex = strings.TrimPrefix(hex, "#")
	if len(hex) != 6 {
		log.Fatalf("not a valid hex color %q", hex)
	}

	for i := 0; i < 3; i++ {
		v, err := strconv.ParseUint(hex[i*2:i*2+2], 16, 8)
		if err != nil {
			log.Fatal(err)
		}

		switch i {
		case 0:
			rgb.Red = uint8(v)
		case 1:
			rgb.Green = uint8(v)
		case 2:
			rgb.Blue = uint8(v)
		}
	}
	return rgb
}

func ReplaceLine() string {
	if !IsTerminal() {
		return "\n"
	}
	return "\x1b[1K\r"
}

func InitIsTerminal(b bool) {
	initIsTerminal.Do(func() {
		isTerminal = b
	})
}

func IsTerminal() bool {
	initIsTerminal.Do(func() {
		isTerminal = term.IsTerminal(int(os.Stdout.Fd()))
	})
	return isTerminal
}

func Bell() {
	if IsTerminal() {
		fmt.Print("\a")
	}
}