Compare commits
4 Commits
d297530309
..
v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| c480e81c0a | |||
| 55821af498 | |||
| 01ec36c7b6 | |||
| ccdd5c5cdb |
@@ -0,0 +1,29 @@
|
||||
Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Celestino Amoroso, nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// dict-set-context.go
|
||||
package text
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// dict-set-context_test.go
|
||||
package text
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expander-base-funcs.go
|
||||
package text
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expander-context.go
|
||||
package text
|
||||
|
||||
@@ -10,8 +13,6 @@ import (
|
||||
"git.portale-stac.it/go-pkg/golang"
|
||||
)
|
||||
|
||||
const varIntro = '$'
|
||||
|
||||
type ExpanderFunc func(ctx ExpanderContext, varValue string, args []string) (result string, err error)
|
||||
|
||||
type ExpanderFuncBlock struct {
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expander-context_test.go
|
||||
package text
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expander-time-funcs.go
|
||||
package text
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expander.go
|
||||
package text
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// scanner_test.go
|
||||
package text
|
||||
|
||||
|
||||
+10
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// scanner.go
|
||||
package text
|
||||
|
||||
@@ -17,11 +20,18 @@ const (
|
||||
// Flags symbols in the abova iota order
|
||||
const flagSymbols = "!=*"
|
||||
|
||||
const defaultVarIntro = '$'
|
||||
|
||||
|
||||
type VariableHandler interface {
|
||||
Handle(varSpec string, flags ScannerFlag) (value string, err error)
|
||||
}
|
||||
|
||||
func Scan(handler VariableHandler, source string) (result string, err error) {
|
||||
return ScanExt(defaultVarIntro, handler, source)
|
||||
}
|
||||
|
||||
func ScanExt(varIntro byte, handler VariableHandler, source string) (result string, err error) {
|
||||
var spec, value string
|
||||
var flags ScannerFlag
|
||||
var backSlash bool
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// scanner_test.go
|
||||
package text
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// tty.go
|
||||
package text
|
||||
|
||||
@@ -9,6 +12,24 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type caseMode uint8
|
||||
|
||||
const (
|
||||
normal = caseMode(iota)
|
||||
upper
|
||||
lower
|
||||
title
|
||||
firstOnly
|
||||
)
|
||||
|
||||
type alignMode uint8
|
||||
|
||||
const (
|
||||
noAlign = alignMode(iota)
|
||||
alignLeft
|
||||
alignRight
|
||||
)
|
||||
|
||||
type ctxFmt struct {
|
||||
isTTY bool
|
||||
ansiLen int
|
||||
@@ -26,12 +47,18 @@ func translateColor(name string) (c int) {
|
||||
c = 30
|
||||
case strings.HasPrefix("blue", norm):
|
||||
c = 34
|
||||
case strings.HasPrefix("cyan", norm):
|
||||
c = 36
|
||||
case strings.HasPrefix("green", norm):
|
||||
c = 32
|
||||
case strings.HasPrefix("magenta", norm):
|
||||
c = 35
|
||||
case strings.HasPrefix("red", norm):
|
||||
c = 31
|
||||
case strings.HasPrefix("white", norm):
|
||||
c = 37
|
||||
case strings.HasPrefix("yellow", norm):
|
||||
c = 33
|
||||
default:
|
||||
c = -1
|
||||
}
|
||||
@@ -108,24 +135,6 @@ func (ctx *ctxFmt) putAnsiReset(sb *strings.Builder) {
|
||||
}
|
||||
}
|
||||
|
||||
type caseMode uint8
|
||||
|
||||
const (
|
||||
normal = caseMode(iota)
|
||||
upper
|
||||
lower
|
||||
title
|
||||
firstOnly
|
||||
)
|
||||
|
||||
type alignMode uint8
|
||||
|
||||
const (
|
||||
noAlign = alignMode(iota)
|
||||
alignLeft
|
||||
alignRight
|
||||
)
|
||||
|
||||
func (ctx *ctxFmt) putText(sb *strings.Builder, text string, start int, mode caseMode, align alignMode, fieldSize int, filler byte) (offset int) {
|
||||
if t := text[start+1:]; len(t) > 0 {
|
||||
if align == alignRight {
|
||||
@@ -273,7 +282,6 @@ func Printf(templ string, args ...any) (int, error) {
|
||||
func Fprintf(w io.Writer, templ string, args ...any) (int, error) {
|
||||
templ, _, _ = ToTtyStream(w, templ)
|
||||
return fmt.Fprintf(w, templ, args...)
|
||||
|
||||
}
|
||||
|
||||
// func DebugSprintf() {
|
||||
|
||||
+27
-7
@@ -1,27 +1,47 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expander-context_test.go
|
||||
package text
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSprintf(t *testing.T) {
|
||||
list := []string{
|
||||
func testCases() []string {
|
||||
return []string{
|
||||
"#{<(.20)-Param}: #{>(06)-123}",
|
||||
"ciao #{c(r)i<(12)-GIAN Carlo}. #{ub-Come} #{c(G)b-Stai}?",
|
||||
"ciao #{c(r)i>(12)-GIAN Carlo}. #{ub-Come} #{c(G)b-Stai}?",
|
||||
"ciao #{c(red);i}Mario#. #{u;b}Come# #{GREEN;b}Stai#{.}?",
|
||||
"ciao #{c(red)i-Mario}. #{ub-Come} #{GREEN;b-Stai#}?",
|
||||
"ciao #{c(red)i-Mario}. #{ub-Come} #{GREEN;b-Stai}?",
|
||||
"ciao #{c(r)i-Mario}. #{ub-Come} #{Gr;b-Stai}?",
|
||||
"ciao #{c(r)i-Mario}. #{ub-Come} #{c(G)b-Stai}?",
|
||||
"ciao #{c(r)i'-gian carlo}. #{ub-Come} #{c(G)b-Stai}?",
|
||||
"ciao #{c(r)i\"-gian carlo}. #{ub-Come} #{c(G)b-Stai}?",
|
||||
"ciao #{c(r)i,-GIAN Carlo}. #{ub-Come} #{c(G)b-Stai}?",
|
||||
"ciao #{c(r)i^-GIAN Carlo}. #{ub-Come} #{c(G)b-Stai}?",
|
||||
}
|
||||
//s := Sprintf("ciao #{fg(139);i}Mario#. #{u;b}Come# #{GREEN;b}%s#{.}?", "Stai")
|
||||
|
||||
}
|
||||
|
||||
func TestSprintf(t *testing.T) {
|
||||
list := testCases()
|
||||
fmt.Println("--- Sprintf() ---")
|
||||
for i, s := range list {
|
||||
x := Sprintf(s)
|
||||
fmt.Printf("--- Test nr %d: %q\n", i+1, s)
|
||||
fmt.Println(x)
|
||||
}
|
||||
fmt.Println("\n--- Sprintf() ---")
|
||||
}
|
||||
|
||||
func TestPrintf(t *testing.T) {
|
||||
list := testCases()
|
||||
fmt.Println("\n--- Printf() ---")
|
||||
for i, s := range list {
|
||||
fmt.Printf("--- Test nr %d: %q\n", i+1, s)
|
||||
Fprintf(os.Stdout, s)
|
||||
Printf(s)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils.go
|
||||
package text
|
||||
|
||||
@@ -6,5 +9,5 @@ import (
|
||||
)
|
||||
|
||||
func Var(name string) string {
|
||||
return fmt.Sprintf("%c{%s}", varIntro, name)
|
||||
return fmt.Sprintf("%c{%s}", defaultVarIntro, name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user