-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
161 lines (136 loc) · 2.94 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"os"
"sort"
"github.com/evilsocket/islazy/log"
"github.com/manifoldco/promptui"
"github.com/olekukonko/tablewriter"
)
func showTableWordSet(number string) {
phrases := numberToWordSet(number)
var rows [][]string
for _, p := range phrases {
newp := p
for i, _ := range newp {
newLine := 35
for len(newp[i]) > newLine {
newp[i] = newp[i][:newLine] + "\n" + newp[i][newLine:]
newLine += newLine
}
}
rows = append(rows, newp)
}
maxLen := 0
for _, r := range rows {
if len(r) > maxLen {
maxLen = len(r)
}
}
for i, _ := range rows {
for len(rows[i]) < maxLen {
rows[i] = append(rows[i], "--")
}
}
partialRows := rows
if len(rows) > 5 {
partialRows = rows[:5]
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoMergeCells(true)
table.SetRowLine(true)
table.AppendBulk(partialRows)
table.Render()
prompt := promptui.Select{
Label: "Seleziona",
Items: []string{MostraTabellaCompleta, Continua, MenuPrincipale},
}
if len(rows) == len(partialRows) {
prompt = promptui.Select{
Label: "Seleziona",
Items: []string{Continua, MenuPrincipale},
}
}
_, result, err := prompt.Run()
if err != nil {
log.Fatal("Error: %s", err)
return
}
switch result {
case MostraTabellaCompleta:
wordsCompatible(number)
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoMergeCells(true)
table.SetRowLine(true)
table.AppendBulk(rows)
table.Render()
case MenuPrincipale:
start()
default:
return
}
}
func wordsCompatible(number string) {
wordsCompatible := "Singole parole : "
words := completeDictionary[number]
if len(words) != 0 {
for i, w := range words {
wordsCompatible = wordsCompatible + w
if i < len(words)-1 {
wordsCompatible = wordsCompatible + ", "
}
}
fmt.Println(wordsCompatible)
}
}
func numberToWordSet(number string) [][]string {
SS := getAllCombinations(number)
var SSfiltered [][]string
for i := 0; i < len(SS); i++ {
ok := true
var wordSet []string
for j := 0; j < len(SS[i]) && ok; j++ {
if commonDictionary[SS[i][j]] != nil {
words := commonDictionary[SS[i][j]][0]
for k := 1; k < len(commonDictionary[SS[i][j]]); k++ {
words = words + ", " + commonDictionary[SS[i][j]][k]
}
wordSet = append(wordSet, words)
} else {
ok = false
}
}
if ok {
SSfiltered = append(SSfiltered, wordSet)
}
}
return SSfiltered
}
func getAllCombinations(s string) [][]string {
SS := [][]string{{s}}
SS = append(SS, getSplit(s)...)
sort.Slice(SS, func(i, j int) bool {
return len(SS[i]) < len(SS[j])
})
return SS
}
func getSplit(s string) [][]string {
var SS [][]string
if len(s) > 1 {
for i := 1; i < len(s); i++ {
s1 := s[:i]
s2 := s[i:]
SS = append(SS, []string{s1, s2})
SS2 := getSplit(s2)
if SS2 != nil {
for j := 0; j < len(SS2); j++ {
SS2[j] = append([]string{s1}, SS2[j]...)
}
SS = append(SS, SS2...)
}
}
return SS
} else {
return nil
}
}