cemantix-charm/main.go

338 lines
7.6 KiB
Go
Raw Normal View History

2022-04-28 18:18:18 +00:00
package main
import (
"context"
"fmt"
2022-04-28 19:18:20 +00:00
"github.com/charmbracelet/bubbles/progress"
2022-04-28 18:18:18 +00:00
"github.com/charmbracelet/bubbles/textinput"
2022-04-28 20:06:54 +00:00
"github.com/charmbracelet/bubbles/viewport"
2022-04-28 18:18:18 +00:00
tea "github.com/charmbracelet/bubbletea"
2022-04-28 20:06:54 +00:00
"github.com/charmbracelet/lipgloss"
2022-04-28 18:18:18 +00:00
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
2022-05-04 20:11:02 +00:00
"github.com/muesli/termenv"
2022-04-28 20:57:20 +00:00
"io/ioutil"
2022-04-28 18:18:18 +00:00
"log"
2022-04-28 19:18:20 +00:00
"math"
2022-04-28 18:18:18 +00:00
"os"
"os/signal"
2022-04-28 20:57:20 +00:00
"strconv"
2022-04-28 19:18:20 +00:00
"strings"
2022-04-28 18:18:18 +00:00
"syscall"
"time"
2022-04-28 20:35:53 +00:00
"unicode/utf8"
2022-04-28 18:18:18 +00:00
)
2022-04-28 20:06:54 +00:00
var (
titleStyle = func() lipgloss.Style {
b := lipgloss.RoundedBorder()
b.Right = "├"
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)
}()
infoStyle = func() lipgloss.Style {
b := lipgloss.RoundedBorder()
b.Left = "┤"
return titleStyle.Copy().BorderStyle(b)
}()
)
2022-04-28 18:18:18 +00:00
func main() {
log.Println("Welcome on Cemantics!")
s, err := wish.NewServer(
wish.WithAddress(fmt.Sprintf("%s:%d", "0.0.0.0", 2200)),
wish.WithAddress(fmt.Sprintf("%s:%d", "[::]", 2200)),
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithMiddleware(
lm.Middleware(),
2022-05-04 20:11:02 +00:00
bm.MiddlewareWithColorProfile(teaHandler, termenv.TrueColor),
2022-04-28 18:18:18 +00:00
),
)
if err != nil {
log.Fatalln(err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Printf("Starting SSH server on %s:%d", "[::]", 2200)
go func() {
if err = s.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}()
<-done
log.Println("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil {
log.Fatalln(err)
}
}
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
_, _, active := s.Pty()
if !active {
fmt.Println("no active terminal, skipping")
return nil, nil
}
log.Println(s.RemoteAddr())
ti := textinput.New()
ti.Placeholder = "Choisissez un mot ..."
ti.Focus()
2022-04-28 19:18:20 +00:00
ti.CharLimit = 26
ti.Width = 26
2022-04-28 20:57:20 +00:00
content, err := ioutil.ReadFile("cemantix.txt")
2022-04-28 20:57:20 +00:00
if err != nil {
log.Printf("Error while opening cemantix.txt: %s", err)
2022-04-28 20:57:20 +00:00
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}
}
2022-04-28 18:18:18 +00:00
m := model{
2022-04-28 19:18:20 +00:00
err: nil,
2022-04-28 20:57:20 +00:00
dictionary: dictionary,
textInput: ti,
2022-05-04 20:11:02 +00:00
username: s.User(),
2022-04-28 19:18:20 +00:00
ip: s.RemoteAddr().String(),
words: []word{},
progressBar: progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")),
2022-04-28 18:18:18 +00:00
}
2022-04-28 20:06:54 +00:00
return m, []tea.ProgramOption{tea.WithAltScreen(), tea.WithMouseCellMotion()}
2022-04-28 18:18:18 +00:00
}
type errMsg error
2022-04-28 19:18:20 +00:00
type word struct {
content string
distance float64
ranking int
}
2022-04-28 18:18:18 +00:00
type model struct {
2022-04-28 20:06:54 +00:00
ready bool
err error
2022-04-28 20:57:20 +00:00
dictionary map[string]word
2022-04-28 20:06:54 +00:00
textInput textinput.Model
2022-05-04 20:11:02 +00:00
username string
2022-04-28 20:06:54 +00:00
ip string
words []word
wordsViewport viewport.Model
progressBar progress.Model
maxLength int
lastWord string
2022-04-28 18:18:18 +00:00
}
func (m model) Init() tea.Cmd {
return textinput.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
2022-04-28 20:06:54 +00:00
var cmds []tea.Cmd
2022-04-28 18:18:18 +00:00
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
return m.InputWord()
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
2022-04-28 20:06:54 +00:00
case tea.WindowSizeMsg:
headerHeight := lipgloss.Height(m.headerView())
footerHeight := lipgloss.Height(m.footerView())
verticalMarginHeight := headerHeight + footerHeight
if !m.ready {
m.wordsViewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
m.wordsViewport.YPosition = headerHeight
m.ready = true
} else {
m.wordsViewport.Width = msg.Width
m.wordsViewport.Height = msg.Height - verticalMarginHeight
}
2022-04-28 18:18:18 +00:00
2022-04-28 20:21:07 +00:00
lineWidth := lipgloss.Width(fmt.Sprintf(
2022-05-04 19:41:42 +00:00
"* %s %s %s %4d ",
2022-04-28 20:21:07 +00:00
strings.Repeat(" ", m.maxLength),
2022-04-28 20:57:20 +00:00
"-100.00",
2022-05-04 19:41:42 +00:00
" ",
2022-04-28 20:21:07 +00:00
42,
))
m.progressBar.Width = msg.Width - lineWidth
m.wordsViewport.SetContent(m.wordsView())
2022-04-28 18:18:18 +00:00
case errMsg:
m.err = msg
return m, nil
}
2022-04-28 20:06:54 +00:00
m.wordsViewport, cmd = m.wordsViewport.Update(msg)
cmds = append(cmds, cmd)
2022-04-28 18:18:18 +00:00
m.textInput, cmd = m.textInput.Update(msg)
2022-04-28 20:06:54 +00:00
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
2022-04-28 18:18:18 +00:00
}
2022-04-28 20:33:30 +00:00
func InsertWord(arr []word, w word) []word {
if len(arr) == 0 {
return append(arr, w)
}
w2 := arr[len(arr)-1]
2022-05-04 19:46:42 +00:00
if w.content == w2.content {
// The word is already present
return arr
}
2022-04-28 20:33:30 +00:00
if w.distance < w2.distance {
return append(arr, w)
}
return append(InsertWord(arr[:len(arr)-1], w), w2)
}
2022-04-28 18:18:18 +00:00
func (m model) InputWord() (model, tea.Cmd) {
2022-04-28 19:18:20 +00:00
input := m.textInput.Value()
2022-04-28 18:18:18 +00:00
2022-04-28 20:57:20 +00:00
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
}
2022-04-28 19:18:20 +00:00
m.lastWord = input
2022-04-28 20:35:53 +00:00
if utf8.RuneCountInString(input) > m.maxLength {
m.maxLength = utf8.RuneCountInString(input)
2022-04-28 19:18:20 +00:00
}
2022-04-28 20:33:30 +00:00
m.words = InsertWord(m.words, w)
2022-04-28 20:06:54 +00:00
2022-04-28 20:21:07 +00:00
lineWidth := lipgloss.Width(fmt.Sprintf(
2022-05-04 19:41:42 +00:00
"* %s %s %s %5d ",
2022-04-28 20:21:07 +00:00
strings.Repeat(" ", m.maxLength),
"-00.00",
2022-05-04 19:41:42 +00:00
" ",
2022-04-28 20:57:20 +00:00
1000,
2022-04-28 20:21:07 +00:00
))
m.progressBar.Width = m.wordsViewport.Width - lineWidth
m.wordsViewport.SetContent(m.wordsView())
2022-04-28 18:18:18 +00:00
m.textInput.SetValue("")
m.textInput.CursorStart()
2022-04-28 20:57:20 +00:00
m.err = nil
2022-04-28 18:18:18 +00:00
return m, nil
}
2022-04-28 19:18:20 +00:00
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
2022-05-04 19:41:42 +00:00
var ranking string = " "
2022-04-28 19:18:20 +00:00
if w.ranking > 0 {
progressBar = m.progressBar.ViewAs(float64(w.ranking) / 1000.0)
ranking = fmt.Sprintf("%4d", w.ranking)
2022-04-28 19:18:20 +00:00
}
distStr := fmt.Sprintf("%.02f", w.distance)
if math.Abs(w.distance) < 10 {
distStr = " " + distStr
}
if w.distance >= 0 {
distStr = " " + distStr
}
2022-04-28 20:57:20 +00:00
if math.Abs(w.distance) < 100 {
distStr = " " + distStr
}
2022-04-28 19:18:20 +00:00
return fmt.Sprintf(
2022-05-04 19:41:42 +00:00
"* %s %s %s %s %s\n",
2022-04-28 20:35:53 +00:00
w.content+strings.Repeat(" ", m.maxLength-utf8.RuneCountInString(w.content)),
2022-04-28 19:18:20 +00:00
distStr,
emoji,
ranking,
2022-04-28 19:18:20 +00:00
progressBar,
)
}
2022-04-28 20:06:54 +00:00
func (m model) headerView() string {
var err string
if m.err != nil {
err = fmt.Sprintf("Erreur : %s", m.err)
}
2022-04-28 18:18:18 +00:00
msg := fmt.Sprintf(
2022-04-28 20:06:54 +00:00
"Veuillez choisir un mot :\n\n%s\n\nDernier mot essayé : %s%s\n%s\n\n",
2022-04-28 18:18:18 +00:00
m.textInput.View(),
2022-04-28 20:06:54 +00:00
m.lastWord,
strings.Repeat(" ", 26),
err,
)
2022-04-28 18:18:18 +00:00
2022-04-28 20:06:54 +00:00
title := titleStyle.Render("Mots précédents")
line := strings.Repeat("─", int(math.Max(0, float64(m.wordsViewport.Width-lipgloss.Width(title)))))
msg += lipgloss.JoinHorizontal(lipgloss.Center, title, line)
2022-04-28 18:18:18 +00:00
2022-04-28 20:06:54 +00:00
return msg
}
2022-04-28 20:21:07 +00:00
func (m model) wordsView() string {
var content string
for _, w := range m.words {
content += w.View(m)
}
return content
}
2022-04-28 20:06:54 +00:00
func (m model) footerView() string {
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.wordsViewport.ScrollPercent()*100))
line := strings.Repeat("─", int(math.Max(0, float64(m.wordsViewport.Width-lipgloss.Width(info)))))
2022-05-04 19:29:52 +00:00
credits := "Inspiration : https://cemantix.herokuapp.com/ (@enigmathix)\n" +
"Sources : https://gitlab.crans.org/ynerant/cemantix-charm (@ynerant)\n" +
"Données : Wikipédia France (+ Word2Vec), Grammalecte"
return lipgloss.JoinVertical(lipgloss.Center, lipgloss.JoinHorizontal(lipgloss.Center, line, info), credits)
2022-04-28 20:06:54 +00:00
}
func (m model) View() string {
if !m.ready {
2022-04-28 20:21:07 +00:00
return "\n\tInitialisation…"
2022-04-28 18:18:18 +00:00
}
2022-04-28 20:06:54 +00:00
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.wordsViewport.View(), m.footerView())
2022-04-28 18:18:18 +00:00
}