incluidas las librerias de transformacion de strings

master
DEV 3 years ago
parent 887463ff49
commit 3faea971c4

@ -1,6 +1,7 @@
package main
import (
"ansi"
"errs/stack"
"fmt"
)
@ -45,9 +46,9 @@ func main() {
/*
for c := ftext.ColorBlack; c <= ftext.ColorWhite; c++ {
fmt.Print(c.SprintfColor("Color %d %s ******\n",c,c.Name()))
fmt.Print(c.SprintfColor("Code %d %s ******\n",c,c.Name()))
}
*/
fmt.Printf("\n \x1b[32;43m sdasd \x1b[0m \n" )
fmt.Printf("\n \x1b[92;43m sdasd \x1b[0m \n" )
@ -55,23 +56,37 @@ func main() {
fmt.Printf("\n \x1b[1;4;92;103m sdasd \x1b[0m \n" )
fmt.Printf("\n \x1b[1;4;5;92;43m sdasd \x1b[0m \n" )
*/
c := ansi.ColorBlue.SetBgColor(ansi.ColorGreen).SetBlink(true)
//fg,bg := c.Colors()
fmt.Printf("Con attributos %s con klinkin con colores %s:%s\n",c.Atts(),c.FgColorName(),c.BgColorName())
fmt.Printf("%sEsto es una muestra%s\n",c.Escape(),c.Atts())
c = c.Clear()
fmt.Printf("Con attributos %s con klinkin con colores %s:%s\n",c.Atts(),c.FgColorName(),c.BgColorName())
/*
color := ansi.ColorBlue
fmt.Printf("Colores %d (%d:%d) escapes = %s\n",color,color.Foreground(),color.Background(),color.Escape())
fmt.Print(c.Escape())
fmt.Printf("Con attributos %s Con Underline \n",c.Atts())
c = c.SetUnderlineOff().SetBoldOn()
fmt.Print(c.Escape())
fmt.Printf("Con attributos %s Sin Underline \n",c.Atts())
c = c.SetBlinkOn().SetBackground(ansi.ColorCyan)
fmt.Print(c.Escape())
fmt.Printf("Con attributos %s con klinkin \n",c.Atts())
c = c.SetBlinkOff()
fmt.Print(c.Escape())
fmt.Printf("Con attributos %s con klinkin \n",c.Atts())
color = color.SetForeground(ansi.ColorGreen).SetBackground(ansi.ColorYellow)
fmt.Printf("Colores %d (%d:%d) escapes = %s\n",color,color.Foreground(),color.Background(),color.Escape())
color = color.SetForeground(ansi.ColorCyan)
fmt.Printf("Colores %d (%d:%d) escapes = %s\n",color,color.Foreground(),color.Background(),color.Escape())
c = c.SetBlinkOff()
fmt.Print(c.Escape())
color = color.SetColors(ansi.ColorWhite,ansi.ColorBlack)
fmt.Printf("Colores %d (%d:%d) escapes = %s\n",color,color.Foreground(),color.Background(),color.Escape())
*/
fmt.Printf("Con attributos %s con klinkin con colores %s:%s\n",c.Atts(),fg.ForegroundName(),bg.BackgroundName())
*/
/*
ercode := errs.ErrCode("error->err-code")
//fmt.Print(errs.ToFormat(ercode,nil))

@ -27,19 +27,50 @@ package ansi
var (
/*
G = struct {
fc string
fcolors [18]string
bcolors [18]string
beginEscape []byte
endEscape []byte
clearEscape []byte
ics []colorInfo
itx []textInfo
}{
fcolors : [18]string{"","30","31","32","33","34","35","36","37","90","91","92","93","94","95","96","97","39"},
bcolors : [18]string{"","30","31","32","33","34","35","36","37","90","91","92","93","94","95","96","97","39"},
fc : "3031323334353637909192939495969739",
beginEscape : []byte("\x1b["),
endEscape : []byte{'m'},
clearEscape : []byte("\x1b[0m"),
ics : []colorInfo{
{[]byte("none") ,[]byte{},[]byte{}},
{[]byte("black") ,[]byte{'3','0'},[]byte{'4','0'}},
{[]byte("red") ,[]byte{'3','1'},[]byte{'4','1'}},
{[]byte("green") ,[]byte{'3','2'},[]byte{'4','2'}},
{[]byte("yellow") ,[]byte{'3','3'},[]byte{'4','3'}},
{[]byte("blue") ,[]byte{'3','4'},[]byte{'4','4'}},
{[]byte("magenta"),[]byte{'3','5'},[]byte{'4','5'}},
{[]byte("cyan") ,[]byte{'3','6'},[]byte{'4','6'}},
{[]byte("white") ,[]byte{'3','7'},[]byte{'4','7'}},
{[]byte("hblack") ,[]byte{'9','0'},[]byte{'1','0','0'}},
{[]byte("hred") ,[]byte{'9','1'},[]byte{'1','0','1'}},
{[]byte("hgreen") ,[]byte{'9','2'},[]byte{'1','0','2'}},
{[]byte("hyellow"),[]byte{'9','3'},[]byte{'1','0','3'}},
{[]byte("hblue") ,[]byte{'9','4'},[]byte{'1','0','4'}},
{[]byte("hmagent"),[]byte{'9','5'},[]byte{'1','0','5'}},
{[]byte("hcyan") ,[]byte{'9','6'},[]byte{'1','0','6'}},
{[]byte("hwhite") ,[]byte{'9','7'},[]byte{'1','0','7'}},
{[]byte("default"),[]byte{'3','7'},[]byte{'4','9'}},
},
itx : []textInfo{
{[]byte("bold-on"), []byte{'1'}},
{[]byte("underline-on"), []byte{'4'}},
{[]byte("blink-on"), []byte{'5'}},
{[]byte("bold-off"), []byte{'2','1'}},
{[]byte("underline-off"), []byte{'2','4'}},
{[]byte("blink-off"), []byte{'2','5'}},
},
}
*/
)

@ -22,8 +22,15 @@
// ------------------------------------------------------------------------
package ansi
/*
import (
"fmt"
"strings"
)
const (
ColorNone Color = iota
ColorNone Code = iota
ColorBlack
ColorRed
ColorGreen
@ -45,93 +52,255 @@ const (
const (
TextBoldOn Color = 1024 << iota
TextBlinkOn
TextBoldOn Code = 1024 << iota
TextUnderlineOn
TextBlinkOn
TextBoldOff
TextBlinkOff
TextUnderlineOff
TextBlinkOff
//bgMask Color = 0b0000000000011111
bgMask Color = 0b1111110000011111
fgMask Color = 0b1111111111100000
clMask Color = 0b0000001111111111
//bgMask Code = 0b0000000000011111
bgMask Code = 0b1111110000011111
fgMask Code = 0b1111111111100000
clMask Code = 0b0000001111111111
txMask Code = 0b1111110000000000
bgShift int = 5
)
var (
fcolors = [18]string{"", "30", "31", "32", "33", "34", "35", "36", "37", "90", "91", "92", "93", "94", "95", "96", "97", "39"}
bcolors = [18]string{"", "40", "41", "42", "43", "44", "45", "46", "47", "100", "101", "102", "103", "104", "105", "106", "107", "49"}
fc = []byte("3031323334353637383930313233343536373839303931393239333433353637383939")
bc = []byte("3031323334353637383930313233343536373839303931393239333433353637383939")
tx = []string{"1","4","5","21","24","25"}
fc = [][]byte{
{},
{'3','0'},
{'3','1'},
{'3','2'},
{'3','3'},
{'3','4'},
{'3','5'},
{'3','6'},
{'3','7'},
{'9','0'},
{'9','1'},
{'9','2'},
{'9','3'},
{'9','4'},
{'9','5'},
{'9','6'},
{'9','7'},
{'3','9'},
}
bc = [][]byte{
{},
{'4','0'},
{'4','1'},
{'4','2'},
{'4','3'},
{'4','4'},
{'4','5'},
{'4','6'},
{'4','7'},
{'1','0','0'},
{'1','0','1'},
{'1','0','2'},
{'1','0','3'},
{'1','0','4'},
{'1','0','5'},
{'1','0','6'},
{'1','0','7'},
{'4','9'},
}
tx = [][]byte{
{'1'},
{'4'},
{'5'},
{'2','1'},
{'2','4'},
{'2','5'}}
colorNames = []string {
"none",
"black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
"hblack",
"hred",
"hgreen",
"hyellow",
"hblue",
"hmagenta",
"hcyan",
"hwhite",
"default",
}
attNames = []string {
"bold",
"underline",
"blink",
"not-bold",
"not-underline",
"not-blink",
}
)
type Color uint16
type Code uint16
func (c Color) SetColors(fg, bg Color) Color {
return fg.SetForeground(fg).SetBackground(bg)
}
func (c Color) Colors() (fg, bg Color) {
return c & fgMask >> bgShift, c & bgMask
}
func (c Code) IsBoldOn () bool {
return c & TextBoldOn != 0 }
func (c Color) Foreground() Color {
return c & bgMask
}
func (c Code) IsBoldOff() bool {
return c & TextBoldOff != 0 }
func (c Color) SetForeground(fg Color) Color {
return (c & fgMask) | fg & bgMask
}
func (c Code) IsBlinkOn() bool {
return c & TextBlinkOn != 0 }
func (c Color) Background() Color {
return (c & fgMask) >> bgShift
}
func (c Code) IsBlinkOff() bool {
return c & TextBlinkOff != 0 }
func (c Color) SetBackground(bg Color) Color {
return (c & bgMask) | (bg << bgShift) & fgMask
}
func (c Code) IsUnderlineOn () bool {
return c & TextUnderlineOn != 0 }
func (c Code) IsUnderlineOff() bool {
return c & TextUnderlineOff != 0 }
func (c Code) SetBoldOn () Code {
return (c &^ TextBoldOff) | TextBoldOn }
func (c Code) SetBoldOff() Code {
return (c &^ TextBoldOn) | TextBoldOff }
func (c Color) EscapeForeground() string {
f := c.Foreground()
if f == 0 {
func (c Code) SetBlinkOn () Code {
return (c &^ TextBlinkOff) | TextBlinkOn }
func (c Code) SetBlinkOff() Code {
return (c &^ TextBlinkOn) | TextBlinkOff }
func (c Code) SetUnderlineOn () Code {
return (c &^ TextUnderlineOff) | TextUnderlineOn }
func (c Code) SetUnderlineOff() Code {
return (c &^ TextUnderlineOn) | TextUnderlineOff }
func (c Code) Atts () string {
if c == 0 {
return ""
}
o := (f - 1) * 2
return string(fc[o:o+2])
var r []string
m := c & txMask
if m != 0 {
dc := TextBoldOn
for i:=0; i<6; i++ {
if m & dc != 0 {
r = append(r,attNames[i])
}
dc <<= 1
}
}
return strings.Join(append(r,c.ForegroundName(),c.BackgroundName()),";")
}
func (c Color) EscapeBackground() string {
f := c.Background()
if f == 0 {
return ""
func (c Code) SetColors(fg, bg Code) Code {
return fg.SetForeground(fg).SetBackground(bg) }
func (c Code) Colors() (fg, bg Code) {
return (c & clMask) & bgMask, ((c & clMask) & fgMask) >> bgShift }
func (c Code) FgColor() Code {
return (c & clMask) & bgMask }
func (c Code) SetForeground(fg Code) Code {
return (c & fgMask) | fg & bgMask }
func (c Code) ForegroundName () string {
return colorNames[c.FgColor()] }
func (c Code) Background() Code {
h := ((c & clMask) & fgMask) >> bgShift
return h
//return ((c & clMask) & fgMask) >> bgShift
}
func (c Code) SetBackground(bg Code) Code {
return (c & bgMask) | (bg << bgShift) & fgMask }
func (c Code) BackgroundName () string {
return colorNames[c.Background()] }
func (c Code) scapeText () []byte {
m := c & txMask
if m == 0 {
return nil
}
o := (f - 1) * 2
return string(fc[o:o+2])
var r []byte
dc := TextBoldOn
for i:=0; i<6; i++ {
if m & dc != 0 {
if r != nil {
r = append(r,';')
}
r = append(r,tx[i]...)
}
dc <<= 1
}
return r
}
func (c Color) Escape() string {
if c == 0 {
return ""
func (c Code) scapeColor () []byte {
m := c & clMask
if m == 0 {
return nil
}
var r []byte
f := c & bgMask
b := (c & fgMask) >> bgShift
f := m & bgMask
b := (m & fgMask) >> bgShift
if f != 0 {
f--
r = append(r,fc[f*2:(f*2)+2]...)
r = append(r,fc[f]...)
}
if b != 0 {
b--
if f > 0 {
r=append(r,';')
}
r = append(r,bc[b*2:(b*2)+2]...)
r = append(r,bc[b]...)
}
return string(r)
}
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 {
r := c.escape()
fmt.Printf("Escape all %s \n",string(r))
return string(append(G.beginEscape, append(r, G.endEscape...)...))
}
*/

@ -0,0 +1,39 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name color.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
type (
colorInfo struct {
name []byte
fgEsc []byte
bgEsc []byte
}
)

@ -0,0 +1,35 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name text-info.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
type (
textInfo struct {
name []byte
esc []byte
}
)

@ -0,0 +1,240 @@
// ------------------------------------------------------------------------
// 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) Colors () (Code,Code) {
return (c & clMask) & bgMask, ((c & clMask) & fgMask) >> bgShift }
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 string(G.clearEscape)
}
r := c.escape()
return string(append(G.beginEscape, append(r, G.endEscape...)...))
}
Loading…
Cancel
Save

Powered by TurnKey Linux.