Verified Commit 93ede8d2 authored by ynerant's avatar ynerant
Browse files

Add progress bar to display

parent 8157dce7
......@@ -13,6 +13,7 @@ require (
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/caarlos0/sshmarshal v0.0.0-20220308164159-9ddb9f83c6b3 // indirect
github.com/charmbracelet/harmonica v0.1.0 // indirect
github.com/charmbracelet/keygen v0.3.0 // indirect
github.com/charmbracelet/lipgloss v0.4.0 // indirect
github.com/containerd/console v1.0.3 // indirect
......
......@@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/wish"
......@@ -10,8 +11,11 @@ import (
lm "github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
"log"
"math"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
......@@ -61,26 +65,35 @@ func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
ti := textinput.New()
ti.Placeholder = "Choisissez un mot ..."
ti.Focus()
ti.CharLimit = 256
ti.Width = 20
ti.CharLimit = 26
ti.Width = 26
m := model{
textInput: ti,
err: nil,
ip: s.RemoteAddr().String(),
words: []string{},
textInput: ti,
err: nil,
ip: s.RemoteAddr().String(),
words: []word{},
progressBar: progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")),
}
return m, []tea.ProgramOption{tea.WithAltScreen()}
}
type errMsg error
type word struct {
content string
distance float64
ranking int
}
type model struct {
textInput textinput.Model
err error
ip string
words []string
lastWord string
textInput textinput.Model
err error
ip string
words []word
progressBar progress.Model
maxLength int
lastWord string
}
func (m model) Init() tea.Cmd {
......@@ -110,17 +123,61 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m model) InputWord() (model, tea.Cmd) {
word := m.textInput.Value()
log.Printf("Chosen word: %s", word)
input := m.textInput.Value()
log.Printf("Chosen word: %s", input)
m.lastWord = word
m.words = append(m.words, word)
m.lastWord = input
if len(input) > m.maxLength {
m.maxLength = len(input)
}
// TODO: Get distance and ranking from a file
// TODO: Reorder the array
m.words = append(m.words, word{input, 200*rand.Float64() - 100, rand.Intn(1000)})
m.textInput.SetValue("")
m.textInput.CursorStart()
return m, nil
}
func (w word) View(m model) string {
var emoji string
if w.ranking == 1000 {
emoji = "\U0001F973"
} else if w.ranking == 999 {
emoji = "\U0001F631"
} else if w.ranking >= 990 {
emoji = "\U0001F525"
} else if w.ranking >= 900 {
emoji = "\U0001F975"
} else if w.ranking >= 1 {
emoji = "\U0001F60E"
} else {
emoji = "\U0001F976"
}
var progressBar string
if w.ranking > 0 {
progressBar = m.progressBar.ViewAs(float64(w.ranking) / 1000.0)
}
distStr := fmt.Sprintf("%.02f", w.distance)
if math.Abs(w.distance) < 10 {
distStr = " " + distStr
}
if w.distance >= 0 {
distStr = " " + distStr
}
return fmt.Sprintf(
"* %s\t%s %s\t%4d %s\n",
w.content+strings.Repeat(" ", m.maxLength-len(w.content)),
distStr,
emoji,
w.ranking,
progressBar,
)
}
func (m model) View() string {
msg := fmt.Sprintf(
"Veuillez choisir un mot :\n\n%s\n\n%s",
......@@ -134,8 +191,8 @@ func (m model) View() string {
if len(m.words) > 0 {
msg += fmt.Sprintf("Liste des derniers mots :\n")
for _, word := range m.words {
msg += fmt.Sprintf("* %s\n", word)
for _, w := range m.words {
msg += w.View(m)
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment