Skip to content

Commit

Permalink
Render the output in a table or inline
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscasalar committed Aug 9, 2024
1 parent 7e15d29 commit cdcb0c6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
61 changes: 56 additions & 5 deletions cmd/extract-pattern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fmt"
"math/rand"
"os"
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"

"github.com/carloscasalar/fantasyname-pattern-extractor/internal/tokenizer"
"github.com/carloscasalar/fantasyname-pattern-extractor/internal/transformer"
Expand All @@ -23,23 +27,70 @@ func main() {
}

pattern := transformer.NewNaiveTransformer().Transform(*tokenizedSample)

fmt.Printf("Pattern: !%v\n", pattern.String())
capitalizedPattern := fmt.Sprintf("!%s", pattern.String())

if opts.NumberOfOutputsToGenerate == 0 {
titleBox := lipgloss.NewStyle().
Bold(true).
PaddingLeft(1).
Foreground(lipgloss.AdaptiveColor{Light: "202", Dark: "252"})
patternBox := lipgloss.NewStyle().
MarginLeft(1).
Foreground(lipgloss.AdaptiveColor{Light: "#3C3C3C", Dark: "#04B575"})
fmt.Println(lipgloss.JoinHorizontal(lipgloss.Left, titleBox.Render("PATTERN:"), patternBox.Render(capitalizedPattern)))
return
}
fmt.Println("Outputs:")
capitalizedPattern := fmt.Sprintf("!%s", pattern.String())
examples := make([]string, opts.NumberOfOutputsToGenerate)

for i := 0; i < int(opts.NumberOfOutputsToGenerate); i++ {
gen, err := fantasyname.Compile(capitalizedPattern, fantasyname.Collapse(true), fantasyname.RandFn(rand.Intn))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(gen.String())
examples[i] = gen.String()
}

rows := [][]string{
{capitalizedPattern, commaSeparated(examples)},
}

re := lipgloss.NewRenderer(os.Stdout)
baseStyle := re.NewStyle().Padding(0, 1)
headerStyle := baseStyle.Foreground(lipgloss.AdaptiveColor{Light: "202", Dark: "252"}).Bold(true)
patternRow := baseStyle.Foreground(lipgloss.AdaptiveColor{Light: "#3C3C3C", Dark: "#04B575"})
columnExample := patternRow.MaxWidth(120).Italic(true)
t := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
return headerStyle
}
if col == 0 {
return patternRow
}
return columnExample
}).
Headers("PATTERN", "EXAMPLES").
Rows(rows...)
fmt.Println(t)
}

func commaSeparated(examples []string) string {
if len(examples) == 0 {
return ""
}

joinedString := new(strings.Builder)
for i, example := range examples {
joinedString.WriteString(example)
if i < len(examples)-1 {
joinedString.WriteString(", ")
}
}

return joinedString.String()
}

func readOptionsOrFail() Ops {
Expand Down
4 changes: 2 additions & 2 deletions cmd/extract-pattern/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestMainFunctionSnapshot(t *testing.T) {
options []string
expectedOutput string
}{
{[]string{"-n", "Tanis"}, "Pattern: !cvcvc\n"},
{[]string{"-n", "Iöwen"}, "Pattern: !v(<v>|(ä|ë|ï|ö|ü))cvc\n"},
{[]string{"-n", "Tanis"}, " PATTERN: !cvcvc\n"},
{[]string{"-n", "Iöwen"}, " PATTERN: !v(<v>|(ä|ë|ï|ö|ü))cvc\n"},
}

for _, inputAndExpectedOutput := range inputsAndExpectedOutputs {
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ module github.com/carloscasalar/fantasyname-pattern-extractor
go 1.22

require (
github.com/charmbracelet/lipgloss v0.12.1
github.com/jessevdk/go-flags v1.6.1
github.com/s0rg/fantasyname v1.3.4
github.com/stretchr/testify v1.9.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs=
github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8=
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/s0rg/fantasyname v1.3.4 h1:zqmKri+2MGVxgRfTm0XViZPtoy0wm9BBc6oqOCuALZw=
github.com/s0rg/fantasyname v1.3.4/go.mod h1:XqTfA5mtZAxnjcSkAwijm1FGoI9FkjRyUOWEEbjsRd0=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down

0 comments on commit cdcb0c6

Please sign in to comment.