Add progress bar to display

This commit is contained in:
Yohann D'ANELLO 2022-04-28 21:18:20 +02:00
parent 8157dce79d
commit 93ede8d260
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 76 additions and 17 deletions

1
go.mod
View File

@ -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

1
go.sum
View File

@ -15,6 +15,7 @@ github.com/charmbracelet/bubbles v0.10.3/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPAL
github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
github.com/charmbracelet/bubbletea v0.20.0 h1:/b8LEPgCbNr7WWZ2LuE/BV1/r4t5PyYJtDb+J3vpwxc=
github.com/charmbracelet/bubbletea v0.20.0/go.mod h1:zpkze1Rioo4rJELjRyGlm9T2YNou1Fm4LIJQSa5QMEM=
github.com/charmbracelet/harmonica v0.1.0 h1:lFKeSd6OAckQ/CEzPVd2mqj+YMEubQ/3FM2IYY3xNm0=
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/keygen v0.3.0 h1:mXpsQcH7DDlST5TddmXNXjS0L7ECk4/kLQYyBcsan2Y=
github.com/charmbracelet/keygen v0.3.0/go.mod h1:1ukgO8806O25lUZ5s0IrNur+RlwTBERlezdgW71F5rM=

91
main.go
View File

@ -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)
}
}