Read data from an input file
This commit is contained in:
parent
0ea390fcae
commit
4ecbe2540e
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
|
|
||||||
cemantix-charm
|
cemantix-charm
|
||||||
.ssh
|
.ssh
|
||||||
|
cemantics.txt
|
||||||
|
50
main.go
50
main.go
@ -12,11 +12,12 @@ import (
|
|||||||
bm "github.com/charmbracelet/wish/bubbletea"
|
bm "github.com/charmbracelet/wish/bubbletea"
|
||||||
lm "github.com/charmbracelet/wish/logging"
|
lm "github.com/charmbracelet/wish/logging"
|
||||||
"github.com/gliderlabs/ssh"
|
"github.com/gliderlabs/ssh"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@ -84,9 +85,31 @@ func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
|
|||||||
ti.Focus()
|
ti.Focus()
|
||||||
ti.CharLimit = 26
|
ti.CharLimit = 26
|
||||||
ti.Width = 26
|
ti.Width = 26
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile("cemantics.txt")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error while opening cemantics.txt: %s", err)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var dictionary = map[string]word{}
|
||||||
|
|
||||||
|
for i, line := range strings.Split(string(content), "\n") {
|
||||||
|
if line == "" || line[0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
s := strings.SplitN(line, " ", 2)
|
||||||
|
w := s[0]
|
||||||
|
dist, _ := strconv.ParseFloat(s[1], 64)
|
||||||
|
dictionary[w] = word{w, dist, 1000 - i}
|
||||||
|
}
|
||||||
|
|
||||||
m := model{
|
m := model{
|
||||||
textInput: ti,
|
|
||||||
err: nil,
|
err: nil,
|
||||||
|
dictionary: dictionary,
|
||||||
|
textInput: ti,
|
||||||
ip: s.RemoteAddr().String(),
|
ip: s.RemoteAddr().String(),
|
||||||
words: []word{},
|
words: []word{},
|
||||||
progressBar: progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")),
|
progressBar: progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")),
|
||||||
@ -105,6 +128,7 @@ type word struct {
|
|||||||
type model struct {
|
type model struct {
|
||||||
ready bool
|
ready bool
|
||||||
err error
|
err error
|
||||||
|
dictionary map[string]word
|
||||||
textInput textinput.Model
|
textInput textinput.Model
|
||||||
ip string
|
ip string
|
||||||
words []word
|
words []word
|
||||||
@ -147,7 +171,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
lineWidth := lipgloss.Width(fmt.Sprintf(
|
lineWidth := lipgloss.Width(fmt.Sprintf(
|
||||||
"* %s %s %s %4d ",
|
"* %s %s %s %4d ",
|
||||||
strings.Repeat(" ", m.maxLength),
|
strings.Repeat(" ", m.maxLength),
|
||||||
"-00.00",
|
"-100.00",
|
||||||
" ",
|
" ",
|
||||||
42,
|
42,
|
||||||
))
|
))
|
||||||
@ -181,20 +205,27 @@ func InsertWord(arr []word, w word) []word {
|
|||||||
func (m model) InputWord() (model, tea.Cmd) {
|
func (m model) InputWord() (model, tea.Cmd) {
|
||||||
input := m.textInput.Value()
|
input := m.textInput.Value()
|
||||||
|
|
||||||
|
w, found := m.dictionary[input]
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
m.err = fmt.Errorf("le mot %s n'existe pas", input)
|
||||||
|
m.textInput.SetValue("")
|
||||||
|
m.textInput.CursorStart()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
m.lastWord = input
|
m.lastWord = input
|
||||||
if utf8.RuneCountInString(input) > m.maxLength {
|
if utf8.RuneCountInString(input) > m.maxLength {
|
||||||
m.maxLength = utf8.RuneCountInString(input)
|
m.maxLength = utf8.RuneCountInString(input)
|
||||||
}
|
}
|
||||||
// TODO: Get distance and ranking from a file
|
|
||||||
w := word{input, 200*rand.Float64() - 100, rand.Intn(1000)}
|
|
||||||
m.words = InsertWord(m.words, w)
|
m.words = InsertWord(m.words, w)
|
||||||
|
|
||||||
lineWidth := lipgloss.Width(fmt.Sprintf(
|
lineWidth := lipgloss.Width(fmt.Sprintf(
|
||||||
"* %s %s %s %4d ",
|
"* %s %s %s %5d ",
|
||||||
strings.Repeat(" ", m.maxLength),
|
strings.Repeat(" ", m.maxLength),
|
||||||
"-00.00",
|
"-00.00",
|
||||||
" ",
|
" ",
|
||||||
42,
|
1000,
|
||||||
))
|
))
|
||||||
m.progressBar.Width = m.wordsViewport.Width - lineWidth
|
m.progressBar.Width = m.wordsViewport.Width - lineWidth
|
||||||
|
|
||||||
@ -202,6 +233,8 @@ func (m model) InputWord() (model, tea.Cmd) {
|
|||||||
|
|
||||||
m.textInput.SetValue("")
|
m.textInput.SetValue("")
|
||||||
m.textInput.CursorStart()
|
m.textInput.CursorStart()
|
||||||
|
m.err = nil
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,6 +266,9 @@ func (w word) View(m model) string {
|
|||||||
if w.distance >= 0 {
|
if w.distance >= 0 {
|
||||||
distStr = " " + distStr
|
distStr = " " + distStr
|
||||||
}
|
}
|
||||||
|
if math.Abs(w.distance) < 100 {
|
||||||
|
distStr = " " + distStr
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"* %s %s %s %4d %s\n",
|
"* %s %s %s %4d %s\n",
|
||||||
|
Loading…
Reference in New Issue
Block a user