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.
243 lines
4.5 KiB
243 lines
4.5 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File n 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 (
|
|
|
|
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
|
|
stShift int = 10
|
|
)
|
|
|
|
|
|
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].n))
|
|
}
|
|
dc <<= 1
|
|
}
|
|
}
|
|
r = append(r,strings.Join([]string{c.FgColor().Name(),c.BgColor().Name()},":"))
|
|
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() Color {
|
|
return Color((c & clMask) & bgMask) }
|
|
|
|
func (c Code) SetFgColor(color Color) Code {
|
|
if !color.IsValid() {
|
|
return c
|
|
}
|
|
return (c & fgMask) | Code(color)& bgMask
|
|
}
|
|
|
|
func (c Code) ResetFgColor() Code {
|
|
return (c & fgMask) | Code(colorDefault)& bgMask }
|
|
|
|
|
|
func (c Code) BgColor() Color {
|
|
return Color(((c & clMask) & fgMask) >> bgShift) }
|
|
|
|
func (c Code) SetBgColor(color Color) Code {
|
|
if !color.IsValid() {
|
|
return c
|
|
}
|
|
return (c & bgMask) | (Code(color)<< bgShift) & fgMask
|
|
}
|
|
|
|
func (c Code) ResetBgColor() Code {
|
|
return (c & bgMask) | (Code(colorDefault)<< bgShift) & fgMask }
|
|
|
|
|
|
|
|
|
|
func (c Code) SetColors(fg, bg Color) Code {
|
|
return c.SetFgColor(fg).SetBgColor(bg) }
|
|
|
|
|
|
func (c Code) SetStyle (sty Style) Code {
|
|
v := Code(sty)
|
|
s := c >> 10
|
|
if s != 0 {
|
|
v = (((s >> 3) & ^ v) << 3) | v
|
|
}
|
|
|
|
return v << 10 | (c & clMask)
|
|
}
|
|
|
|
|
|
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,';')
|
|
} else {
|
|
r = append(r,'0',';')
|
|
}
|
|
r = append(r, g.itx[i].s...)
|
|
}
|
|
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].f...)
|
|
}
|
|
if b != 0 {
|
|
if f > 0 {
|
|
r=append(r,';')
|
|
}
|
|
r = append(r, g.ics[b].b...)
|
|
}
|
|
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) EscapeValues () string {
|
|
return string(c.escape())
|
|
}
|
|
|
|
func (c Code) Esc() string {
|
|
if c == 0 {
|
|
return g.endEscStr
|
|
}
|
|
r := c.escape()
|
|
return string(append(g.beginEscIni, append(r, g.beginEscEnd...)...))
|
|
}
|
|
|