File: data.go

package info (click to toggle)
golang-pault-go-technicolor 0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 68 kB
  • ctags: 122
  • sloc: makefile: 5
file content (123 lines) | stat: -rw-r--r-- 2,712 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
package technicolor

import (
	"fmt"
	"io"
	"os"
	"strings"
	"syscall"
	"unsafe"
)

type ANSISequence []byte

type ANSISequences []ANSISequence

// "Flatten" the ANSI Sequences into a single byte stream
func (a ANSISequences) Sequence() []byte {
	out := []byte{}
	for _, seq := range a {
		out = append(out, seq...)
	}
	return out
}

func NewANSISequence(arguments []string, ansiType byte) ANSISequence {
	return ANSISequence(append(append([]byte{0x1b, 0x5b},
		[]byte(strings.Join(arguments, ";"))...,
	), ansiType))
}

func NewTerminalWriter(file *os.File) Writer {
	output := NewWriter(file)
	if IsFileTerminal(file) {
		output = output.EnableColor()
	} else {
		output = output.DisableColor()
	}
	return output
}

func NewWriter(o io.Writer) Writer {
	return Writer{
		aNSISequences: ANSISequences{},
		output:        o,
		enableColor:   false,
	}
}

type Writer struct {
	aNSISequences ANSISequences
	output        io.Writer
	enableColor   bool
}

func (w Writer) DisableColor() Writer {
	return Writer{
		aNSISequences: w.aNSISequences,
		output:        w.output,
		enableColor:   false,
	}
}

func (w Writer) EnableColor() Writer {
	return Writer{
		aNSISequences: w.aNSISequences,
		output:        w.output,
		enableColor:   true,
	}
}

func (w Writer) Bold() Writer {
	return w.Add(NewANSISequence([]string{"1"}, 'm'))
}

func (w Writer) Add(seqs ...ANSISequence) Writer {
	return Writer{
		aNSISequences: append(w.aNSISequences, seqs...),
		output:        w.output,
		enableColor:   w.enableColor,
	}
}

func (w Writer) Reset(seqs ...ANSISequence) Writer {
	return Writer{
		aNSISequences: ANSISequences{},
		output:        w.output,
		enableColor:   w.enableColor,
	}
}

func (w Writer) Printf(format string, args ...interface{}) (int, error) {
	return fmt.Fprintf(w, format, args...)
}

func (w Writer) Write(out []byte) (int, error) {
	if w.enableColor {
		return w.output.Write(append(w.aNSISequences.Sequence(), out...))
	}
	return w.output.Write(out)
}

// This is some yanked nasty shaz that will check to see if a file descriptor
// we have is a terminal or something else. This is handy for color stuff,
// since we want to be able to pipe things without seeing colors in the
// processing pipeline, but also make things sane for a user.
func IsTerminal(fd uintptr) bool {
	var termios syscall.Termios
	var ioctlReadTermios uintptr = 0x5401 // syscall.TCGETS
	_, _, err := syscall.Syscall6(
		syscall.SYS_IOCTL,
		fd,
		ioctlReadTermios,
		uintptr(unsafe.Pointer(&termios)),
		0, 0, 0,
	)
	return err == 0
}

// Check to see if a given os.File is a Terminal. Notibly, `os.Stdout` is
// one of these, as is `os.Stderr`.
func IsFileTerminal(file *os.File) bool {
	return IsTerminal(file.Fd())
}