mirror of
https://github.com/kemko/xc.git
synced 2026-01-01 15:55:43 +03:00
project move
This commit is contained in:
79
term/colors.go
Normal file
79
term/colors.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package term
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type colorValue int
|
||||
|
||||
// Color codes
|
||||
const (
|
||||
CBlack colorValue = 30
|
||||
CRed colorValue = 31
|
||||
CGreen colorValue = 32
|
||||
CYellow colorValue = 33
|
||||
CBlue colorValue = 34
|
||||
CMagenta colorValue = 35
|
||||
CCyan colorValue = 36
|
||||
CLightGray colorValue = 37
|
||||
CDarkGray colorValue = 90
|
||||
CLightRed colorValue = 91
|
||||
CLightGreen colorValue = 92
|
||||
CLightYellow colorValue = 93
|
||||
CLightBlue colorValue = 94
|
||||
CLightMagenta colorValue = 95
|
||||
CLightCyan colorValue = 96
|
||||
CWhite colorValue = 97
|
||||
)
|
||||
|
||||
// Colored wraps message into esc sequences to make it colored
|
||||
func Colored(message string, c colorValue, bold bool) string {
|
||||
bstr := ""
|
||||
if bold {
|
||||
bstr = ";1"
|
||||
}
|
||||
return fmt.Sprintf("\033[%d%sm%s\033[0m", c, bstr, message)
|
||||
}
|
||||
|
||||
// Blue returns message colored with light blue color
|
||||
func Blue(message string) string {
|
||||
return Colored(message, CLightBlue, false)
|
||||
}
|
||||
|
||||
// Red returns message colored with light red color
|
||||
func Red(message string) string {
|
||||
return Colored(message, CLightRed, false)
|
||||
}
|
||||
|
||||
// Green returns message colored with light green color
|
||||
func Green(message string) string {
|
||||
return Colored(message, CLightGreen, false)
|
||||
}
|
||||
|
||||
// Yellow returns message colored with light yellow color
|
||||
func Yellow(message string) string {
|
||||
return Colored(message, CLightYellow, false)
|
||||
}
|
||||
|
||||
// Cyan returns message colored with light cyan color
|
||||
func Cyan(message string) string {
|
||||
return Colored(message, CLightCyan, false)
|
||||
}
|
||||
|
||||
// Errorf prints a red-colored formatted error message
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
fmt.Print(Red(message))
|
||||
}
|
||||
|
||||
// Successf prints a green-colored formatted message
|
||||
func Successf(format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
fmt.Print(Green(message))
|
||||
}
|
||||
|
||||
// Warnf prints a yellow-colored formatted warning message
|
||||
func Warnf(format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
fmt.Print(Yellow(message))
|
||||
}
|
||||
34
term/util.go
Normal file
34
term/util.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package term
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type winsize struct {
|
||||
Row uint16
|
||||
Col uint16
|
||||
Xpixel uint16
|
||||
Ypixel uint16
|
||||
}
|
||||
|
||||
// HR returns a horizontal ruler of a given size
|
||||
func HR(len int) string {
|
||||
h := bytes.Repeat([]byte("="), len)
|
||||
return string(h)
|
||||
}
|
||||
|
||||
// GetTerminalWidth returns the current terminal width in symbols
|
||||
func GetTerminalWidth() int {
|
||||
ws := &winsize{}
|
||||
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
|
||||
uintptr(syscall.Stdin),
|
||||
uintptr(syscall.TIOCGWINSZ),
|
||||
uintptr(unsafe.Pointer(ws)))
|
||||
|
||||
if int(retCode) == -1 {
|
||||
panic(errno)
|
||||
}
|
||||
return int(ws.Col)
|
||||
}
|
||||
Reference in New Issue
Block a user