incluidas las librerias de transformacion de strings

master
DEV 3 years ago
parent fc253bb41f
commit 20e637f68c

@ -2,6 +2,7 @@ package main
import (
"ansi"
"bytes"
"errs/stack"
"fmt"
)
@ -59,36 +60,16 @@ func (c MiCustomErr) Unwrap () error {
func main() {
c1 := ansi.Code(0).SetBold(true)
c2 := c1.SetBold(false)
//fmt.Printf("C1: %s C2: %s\n",c1.EscapeValues(),c2.EscapeValues())
fmt.Printf("%sCon Atributo Bold -> %ssin atributo Bold\n",c1.Esc(),c2.Esc())
c1 := ansi.Code(0).SetStyle(ansi.StyleBold|ansi.StyleUnderline)
fmt.Printf("%s TexTo con Atts %s\n",c1,c1.End())
c1 = c1.DelStyle(ansi.StyleUnderline)
s:= fmt.Sprintf("%s TexTo con Atts %s\n",c1,c1.End())
fmt.Print(s)
c1 = c1.Clear().SetUnderline(true)
c2 = c1.SetUnderline(false)
fmt.Printf("C1: %s C2: %s\n",c1.EscapeValues(),c2.EscapeValues())
fmt.Printf("%sCon Atributo -> %ssin atributo \n",c1.Esc(),c2.Esc())
c1 = c1.Clear().SetBlink(true).SetBgColor(ansi.ColorRed)
c1 = c1.Clear().SetBlink(false).SetFgColor(ansi.ColorGreen)
fmt.Printf("C1: %s \n",c1.EscapeValues())
fmt.Println(c1)
c1 = c1.SetStyle(ansi.StyleBold)
fmt.Printf("C1: %s \n",c1.EscapeValues())
fmt.Println(c1)
c1 = c1.SetStyle(ansi.StyleBlink).SetBgColor(ansi.ColorHWhite)
fmt.Printf("C1: %s \n",c1.EscapeValues())
fmt.Print(c1.Esc())
fmt.Println(c1)
c1 = c1.DelStyle(ansi.StyleBold)
fmt.Printf("C1: %s \n",c1.EscapeValues())
fmt.Print(c1.Esc())
fmt.Println(c1)
b := &bytes.Buffer{}
w := ansi.NewNonEscapeWriter(b)
w.Write([]byte(s))
fmt.Print(string(b.Bytes()))
}

@ -27,6 +27,22 @@ import (
"bytes"
)
const (
styleAttsStart Code = 1024
bgMask Code = 0b1111110000011111
fgMask Code = 0b1111111111100000
clMask Code = 0b0000001111111111
txMask Code = 0b1111110000000000
bgShift int = 5
stShift int = 10
styleNumOps int = 3
)
var (
g = struct {
@ -93,7 +109,7 @@ func genCharEscapes () []byte {
func Esc (code Code) string {
return code.Esc() }
return code.String() }
func EscEnd () string {
return g.endEscStr }

@ -24,28 +24,13 @@
package ansi
import (
"bytes"
"fmt"
"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
stOpts int = 3
)
type (
@ -61,7 +46,7 @@ func (c Code) Atts () string {
var r []string
m := c & txMask
if m != 0 {
dc := TextBoldOn
dc := styleAttsStart
for i:=0; i<6; i++ {
if m & dc != 0 {
r = append(r,string(g.itx[i].n))
@ -114,77 +99,35 @@ func (c Code) ResetBgColor() Code {
func (c Code) SetColors(fg, bg Color) Code {
return c.SetFgColor(fg).SetBgColor(bg) }
func (c Code) Style () Style {
return Style(c >> 10) }
func (c Code) SetStyle (sty Style) Code {
/*
v := Code(sty)
v = ((((c >> 10) >> stOpts) & ^ v) << stOpts) | v
return v << 10 | (c & clMask)
*/
if !sty.IsValid() {
return c
}
v := Code(sty) << stShift
return ((((c & txMask) >> stOpts) &^ v) << stOpts | v) | c & clMask
return ((((c & txMask) >>styleNumOps) &^ v) <<styleNumOps | v) | c & clMask
}
func (c Code) DelStyle (sty Style) Code {
v := Code(sty) << (stOpts + stShift)
return ((((c & txMask) << stOpts) &^ v) >> stOpts | v) | 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
if !sty.IsValid() {
return c
}
return (c &^ TextUnderlineOn) | TextUnderlineOff
v := Code(sty) << (styleNumOps + stShift)
return ((((c & txMask) <<styleNumOps) &^ v) >>styleNumOps | v) | c & clMask
}
func (c Code) scapeText () []byte {
func (c Code) scapeStyle () []byte {
m := c & txMask
if m == 0 {
return nil
}
var r []byte
dc := TextBoldOn
dc := styleAttsStart
for i:=0; i<6; i++ {
if m & dc != 0 {
if r != nil {
@ -225,7 +168,7 @@ func (c Code) escape () []byte {
if c == 0 {
return []byte{'0'}
}
st := c.scapeText ()
st := c.scapeStyle()
sc := c.scapeColor()
if len(sc) == 0 {
return st
@ -236,15 +179,49 @@ func (c Code) escape () []byte {
return append(st,append([]byte{';'},sc...)...)
}
func (c Code) EscapeValues () string {
func (c Code) stringValues() string {
return string(c.escape())
}
func (c Code) Esc() string {
func (c Code) String() string {
if c == 0 {
return g.endEscStr
return ""
}
r := c.escape()
return string(append(g.beginEscIni, append(r, g.beginEscEnd...)...))
}
func (_ Code) End () string {
return g.endEscStr }
func (c Code) Sprintf (format string, params ...any) string {
if len(format) == 0 && len(params) == 0 {
return ""
}
s := fmt.Sprintf(format,params...)
if c == 0 {
return s
}
b := bytes.Buffer{}
b.WriteString(b.String())
b.WriteString(s)
b.WriteString(g.endEscStr)
return b.String()
}
func (c Code) Sprintln(format string, params ...any) string {
return fmt.Sprintln(c.Sprintf(format,params...)) }
func (c Code) Printf (format string, params ...any) {
fmt.Printf(c.Sprintf(format,params...))
}

@ -1,99 +0,0 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File n non_escape_reader.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 (
"bufio"
"io"
)
type (
NonEscapeReader struct {
pos int
unescaped []byte
reader io.Reader
}
)
func NewNonEscapeReader (reader io.Reader) *NonEscapeReader {
r := bufio.NewReader(reader)
return &NonEscapeReader{
reader : r,
unescaped : make([]byte,0,r.Size()),
}
}
/*
func (r *NonEscapeReader) Read (p []byte) (n int, e error) {
n := 0
for {
// Read a byte from the underlying reader
b, err := f.Reader.ReadByte()
if err == io.EOF {
break
}
if err != nil {
return n, err
}
// If not an escape character, write it to the output buffer
if !escapeMap[b] {
p[n] = b
n++
continue
}
// If escape character, skip the next byte
next, err := SkipEscape(f.Reader, escapeMap)
if err != nil {
return n, err
}
// Update reader position to reflect skipped bytes
f.pos += next - f.pos
}
return n, nil
}
func skipEscape(r *bufio.Reader, escapeMap [255]byte) (int, error) {
for {
b, err := r.ReadByte()
if err == io.EOF {
return 0, errors.New("Incomplete escape sequence")
}
if err != nil {
return 0, err
}
if !escapeMap[b] {
return r.Pos(), nil
}
}
}
*/

@ -41,7 +41,7 @@ func NewNonEscapeWriter(w io.Writer) *NonEscapeWriter {
return &NonEscapeWriter{ writer: w } }
/*
func (w *NonEscapeWriter) Write(data []byte) (n int, e error) {
var pt bytes.Buffer
var ef bool
@ -58,7 +58,7 @@ func (w *NonEscapeWriter) Write(data []byte) (n int, e error) {
}
if ef {
ef = g.charsEscs[b] == 0
ef = g.charsEscs[b] == 1
continue
}
@ -72,3 +72,46 @@ func (w *NonEscapeWriter) Write(data []byte) (n int, e error) {
return len(data), nil
}
*/
func (w *NonEscapeWriter) Write(data []byte) (n int, err error) {
er := bytes.NewReader(data)
var plaintext bytes.Buffer
loop:
for {
c1, err := er.ReadByte()
if err != nil {
plaintext.WriteTo(w.writer)
break loop
}
if c1 != 0x1b {
plaintext.WriteByte(c1)
continue
}
_, err = plaintext.WriteTo(w.writer)
if err != nil {
break loop
}
c2, err := er.ReadByte()
if err != nil {
break loop
}
if c2 != 0x5b {
continue
}
for {
c, err := er.ReadByte()
if err != nil {
break loop
}
if g.charsEscs[c] == 1 {
//if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
break
}
}
}
return len(data), nil
}

@ -23,6 +23,11 @@
package ansi
import (
"math/bits"
"strings"
)
const (
@ -41,8 +46,31 @@ type (
)
func (s Style) Count () int {
return bits.OnesCount8(uint8(s)) }
func (s Style) IsValid () bool {
return s <= allStyles
return s <= allStyles }
func (s Style) Name () string {
var r []string
for i:=0; i< styleNumOps; i++ {
}
return strings.Join(r,",")
}
func (s Style) HasBold () bool {
return s&StyleBold != 0 }
func (s Style) HasUnderline () bool {
return s&StyleUnderline != 0 }
func (s Style) HasBlink () bool {
return s&StyleBlink != 0 }

Loading…
Cancel
Save

Powered by TurnKey Linux.