You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
4.4 KiB
242 lines
4.4 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name code.go
|
|
// Created by DEV
|
|
// Modified 06/04/2024
|
|
//
|
|
// Copyright 2024 activething.com
|
|
// ------------------------------------------------------------------------
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package ansi
|
|
|
|
import "strings"
|
|
/*
|
|
const (
|
|
|
|
ColorNone Code = iota
|
|
ColorBlack
|
|
ColorRed
|
|
ColorGreen
|
|
ColorYellow
|
|
ColorBlue
|
|
ColorMagenta
|
|
ColorCyan
|
|
ColorWhite
|
|
ColorHBlack
|
|
ColorHRed
|
|
ColorHGreen
|
|
ColorHYellow
|
|
ColorHBlue
|
|
ColorHMagenta
|
|
ColorHCyan
|
|
ColorHWhite
|
|
ColorDefault
|
|
|
|
|
|
)
|
|
|
|
*/
|
|
|
|
const (
|
|
|
|
TextBoldOn Code = 1024 << iota
|
|
TextUnderlineOn
|
|
TextBlinkOn
|
|
TextBoldOff
|
|
TextUnderlineOff
|
|
TextBlinkOff
|
|
|
|
//bgMask Code = 0b0000000000011111
|
|
bgMask Code = 0b1111110000011111
|
|
fgMask Code = 0b1111111111100000
|
|
clMask Code = 0b0000001111111111
|
|
txMask Code = 0b1111110000000000
|
|
bgShift int = 5
|
|
)
|
|
|
|
|
|
type (
|
|
|
|
Code uint16
|
|
)
|
|
|
|
|
|
func (c Code) Atts () string {
|
|
if c == 0 {
|
|
return ""
|
|
}
|
|
var r []string
|
|
m := c & txMask
|
|
if m != 0 {
|
|
dc := TextBoldOn
|
|
for i:=0; i<6; i++ {
|
|
if m & dc != 0 {
|
|
r = append(r,string(g.itx[i].name))
|
|
}
|
|
dc <<= 1
|
|
}
|
|
}
|
|
r = append(r,strings.Join([]string{c.FgColorName(),c.BgColorName()},":"))
|
|
return strings.Join(r,";")
|
|
}
|
|
|
|
|
|
|
|
func (c Code) Clear () Code {
|
|
return 0 }
|
|
|
|
func (c Code) ClearColor () Code {
|
|
return c & txMask }
|
|
|
|
func (c Code) ClearText () Code {
|
|
return c & clMask }
|
|
|
|
|
|
func (c Code) FgColor() Code {
|
|
return (c & clMask) & bgMask }
|
|
|
|
func (c Code) SetFgColor(color Code) Code {
|
|
return (c & fgMask) | color& bgMask }
|
|
|
|
func (c Code) FgColorName () string {
|
|
return string(g.ics[(c & clMask) & bgMask].name) }
|
|
|
|
|
|
func (c Code) BgColor() Code {
|
|
return ((c & clMask) & fgMask) >> bgShift }
|
|
|
|
func (c Code) SetBgColor(color Code) Code {
|
|
return (c & bgMask) | (color<< bgShift) & fgMask }
|
|
|
|
func (c Code) BgColorName () string {
|
|
return string(g.ics[((c & clMask) & fgMask) >> bgShift].name) }
|
|
|
|
|
|
|
|
func (c Code) SetColors(fg, bg Code) Code {
|
|
return c.SetFgColor(fg).SetBgColor(bg) }
|
|
|
|
|
|
|
|
|
|
|
|
func (c Code) IsBoldOn () bool {
|
|
return c & TextBoldOn != 0 }
|
|
|
|
func (c Code) IsBoldOff() bool {
|
|
return c & TextBoldOff != 0 }
|
|
|
|
func (c Code) IsBlinkOn() bool {
|
|
return c & TextBlinkOn != 0 }
|
|
|
|
func (c Code) IsBlinkOff() bool {
|
|
return c & TextBlinkOff != 0 }
|
|
|
|
func (c Code) IsUnderlineOn () bool {
|
|
return c & TextUnderlineOn != 0 }
|
|
|
|
func (c Code) IsUnderlineOff() bool {
|
|
return c & TextUnderlineOff != 0 }
|
|
|
|
func (c Code) SetBold (on bool) Code {
|
|
if on {
|
|
return (c &^ TextBoldOff) | TextBoldOn
|
|
}
|
|
return (c &^ TextBoldOn) | TextBoldOff
|
|
}
|
|
|
|
func (c Code) SetBlink (on bool) Code {
|
|
if on {
|
|
return (c &^ TextBlinkOff) | TextBlinkOn
|
|
}
|
|
return (c &^ TextBlinkOn) | TextBlinkOff
|
|
}
|
|
|
|
func (c Code) SetUnderline (on bool) Code {
|
|
if on {
|
|
return (c &^ TextUnderlineOff) | TextUnderlineOn
|
|
}
|
|
return (c &^ TextUnderlineOn) | TextUnderlineOff
|
|
}
|
|
|
|
|
|
|
|
func (c Code) scapeText () []byte {
|
|
m := c & txMask
|
|
if m == 0 {
|
|
return nil
|
|
}
|
|
var r []byte
|
|
dc := TextBoldOn
|
|
for i:=0; i<6; i++ {
|
|
if m & dc != 0 {
|
|
if r != nil {
|
|
r = append(r,';')
|
|
}
|
|
r = append(r, g.itx[i].esc...)
|
|
}
|
|
dc <<= 1
|
|
}
|
|
return r
|
|
}
|
|
|
|
|
|
func (c Code) scapeColor () []byte {
|
|
m := c & clMask
|
|
if m == 0 {
|
|
return nil
|
|
}
|
|
var r []byte
|
|
f := m & bgMask
|
|
b := (m & fgMask) >> bgShift
|
|
if f != 0 {
|
|
r = append(r, g.ics[f].fgEsc...)
|
|
}
|
|
if b != 0 {
|
|
if f > 0 {
|
|
r=append(r,';')
|
|
}
|
|
r = append(r, g.ics[b].bgEsc...)
|
|
}
|
|
return r
|
|
}
|
|
|
|
|
|
func (c Code) escape () []byte {
|
|
if c == 0 {
|
|
return []byte{'0'}
|
|
}
|
|
st := c.scapeText ()
|
|
sc := c.scapeColor()
|
|
if len(sc) == 0 {
|
|
return st
|
|
}
|
|
if len(st) == 0 {
|
|
return sc
|
|
}
|
|
return append(st,append([]byte{';'},sc...)...)
|
|
}
|
|
|
|
|
|
func (c Code) Escape() string {
|
|
if c == 0 {
|
|
return ""
|
|
}
|
|
r := c.escape()
|
|
return string(append(g.beginEscIni, append(r, g.beginEscEnd...)...))
|
|
}
|