Order output

This commit is contained in:
Yohann D'ANELLO 2022-04-28 22:33:30 +02:00
parent b3768042e4
commit ed64019555
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 12 additions and 2 deletions

14
main.go
View File

@ -166,6 +166,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}
func InsertWord(arr []word, w word) []word {
if len(arr) == 0 {
return append(arr, w)
}
w2 := arr[len(arr)-1]
if w.distance < w2.distance {
return append(arr, w)
}
return append(InsertWord(arr[:len(arr)-1], w), w2)
}
func (m model) InputWord() (model, tea.Cmd) {
input := m.textInput.Value()
@ -174,9 +185,8 @@ func (m model) InputWord() (model, tea.Cmd) {
m.maxLength = len(input)
}
// TODO: Get distance and ranking from a file
// TODO: Reorder the array
w := word{input, 200*rand.Float64() - 100, rand.Intn(1000)}
m.words = append(m.words, w)
m.words = InsertWord(m.words, w)
lineWidth := lipgloss.Width(fmt.Sprintf(
"* %s %s %s %4d ",