utils/tty-util.go

209 lines
3.2 KiB
Go
Raw Normal View History

// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// tty-utils.go
2024-02-16 15:51:28 +01:00
package utils
import (
"os"
)
const STDIN uint = 0
const STDOUT uint = 1
const STDERR uint = 2
const (
NONE = iota
BLACK
RED
GREEN
BLUE
BROWN
// YELLOW
CYAN
MAGENTA
WHITE
)
type TTYContext struct {
isTTY [3]bool
}
func (self *TTYContext) Init() {
for i, _ := range self.isTTY {
self.isTTY[i] = StreamIsTerminal(getStream(uint(i)))
}
}
func (self *TTYContext) IsTTY(fd uint) bool {
if fd == STDERR || fd == STDOUT {
return self.isTTY[fd]
}
return false
}
func (self *TTYContext) Clean() {
for i, _ := range self.isTTY {
self.isTTY[i] = false
}
}
func (self *TTYContext) Bold(fd uint) string {
if self.isTTY[fd] {
return "\x1b[1m"
}
return ""
}
func (self *TTYContext) BoldOff(fd uint) string {
if self.isTTY[fd] {
return "\x1b[22m"
}
return ""
}
func (self *TTYContext) Underline(fd uint) string {
if self.isTTY[fd] {
return "\x1b[4m"
}
return ""
}
func (self *TTYContext) UnderlineOff(fd uint) string {
if self.isTTY[fd] {
return "\x1b[24m"
}
return ""
}
func (self *TTYContext) Italic(fd uint) string {
if self.isTTY[fd] {
return "\x1b[3m"
}
return ""
}
func (self *TTYContext) ItalicOff(fd uint) string {
if self.isTTY[fd] {
return "\x1b[23m"
}
return ""
}
func (self *TTYContext) BlackFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[30m"
}
return ""
}
func (self *TTYContext) RedFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[31m"
}
return ""
}
func (self *TTYContext) GreenFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[32m"
}
return ""
}
func (self *TTYContext) BrownFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[33m"
}
return ""
}
func (self *TTYContext) BlueFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[34m"
}
return ""
}
func (self *TTYContext) MagentaFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[35m"
}
return ""
}
func (self *TTYContext) CyanFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[36m"
}
return ""
}
func (self *TTYContext) WhiteFg(fd uint) string {
if self.isTTY[fd] {
return "\x1b[37m"
}
return ""
}
func (self *TTYContext) FgColor(fd uint, n uint) string {
var color string
switch n {
case BLACK:
color = self.BlackFg(fd)
case WHITE:
color = self.WhiteFg(fd)
case RED:
color = self.RedFg(fd)
case GREEN:
color = self.GreenFg(fd)
case BLUE:
color = self.BlueFg(fd)
case BROWN:
color = self.BrownFg(fd)
case CYAN:
color = self.CyanFg(fd)
case MAGENTA:
color = self.MagentaFg(fd)
}
return color
}
// func (self *TTYContext) BgColor(fd uint, n int) string {
// var color string
// switch n {
// case BLACK:
// color = self.BlackBg(fd)
// case WHITE:
// color = self.WhiteBg(fd)
// case RED:
// color = self.RedBg(fd)
// case GREEN:
// color = self.GreenBg(fd)
// case CYAN:
// color = self.CyanBg(fd)
// case MAGENTA:
// color = self.MagentaBg(fd)
// }
// return color
// }
func (self *TTYContext) Reset(fd uint) string {
if StreamIsTerminal(getStream(fd)) {
return "\x1b[0m"
}
return ""
}
func getStream(fd uint) *os.File {
var s *os.File = nil
switch fd {
case STDOUT:
s = os.Stdout
case STDERR:
s = os.Stderr
}
return s
}