wip: viewport page handling, better mouse support

This commit is contained in:
decentral1se 2023-07-24 10:12:19 +02:00
parent bf0507a765
commit bc554b2d19
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
4 changed files with 72 additions and 54 deletions

View File

@ -59,7 +59,7 @@ func main() {
p := tea.NewProgram(
newModel(user.Username, user.HomeDir),
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
tea.WithMouseAllMotion(),
)
if err := p.Start(); err != nil {

View File

@ -22,11 +22,10 @@ const (
\____/_/ |_/___/_/ |_/_____/_____/
Metadata resistant messaging
pre-alpha v0.1.0
Pre-alpha v0.1.0
Run /start for the getting started guide
Use the /help command to see the command listing
`
Use the /help command to see the command listing`
gettingStartedMessage = `
== Getting started guide ==
@ -63,7 +62,10 @@ type model struct {
showWelcomeMessage bool
statusViewport viewport.Model
statusViewport viewport.Model
statusViewportLines []string
statusViewportReady bool
viewportState int
profiles profiles
profileState int
@ -81,13 +83,18 @@ func newModel(username, homeDir string) model {
input.Focus()
return model{
username: username,
userDir: path.Join(homeDir, "/.cairde/"),
connState: offline,
menuBar: []string{"status"},
menuState: 0,
input: input,
statusBuffer: make(chan string),
username: username,
userDir: path.Join(homeDir, "/.cairde/"),
connState: offline,
menuBar: []string{"status"},
menuState: 0,
input: input,
statusBuffer: make(chan string),
showWelcomeMessage: true,
}
}
@ -127,31 +134,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds []tea.Cmd
)
m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd)
m.statusViewport, cmd = m.statusViewport.Update(msg)
cmds = append(cmds, cmd)
for _, p := range m.profiles {
p.statusViewport, cmd = p.statusViewport.Update(msg)
cmds = append(cmds, cmd)
}
if m.showWelcomeMessage {
cmds = append(cmds, m.sendStatusCmd(
strings.Split(welcomeMessage, "\n")...,
))
m.showWelcomeMessage = false
}
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width - 2
m.height = msg.Height - 2
m.statusViewport.Width = m.width
m.statusViewport.Height = m.height - 3
if !m.statusViewportReady {
m.statusViewport = newViewport(m.width, m.height-3)
m.statusViewportReady = true
} else {
m.statusViewport.Width = m.width
m.statusViewport.Height = m.height - 3
}
for _, p := range m.profiles {
p.statusViewport.Width = m.width
@ -228,13 +222,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
profiles := m.app.ListProfiles()
newProfile := profile{
name: msg.name,
onion: profiles[len(profiles)-1],
statusViewport: viewport.New(
m.width,
m.height-3,
),
name: msg.name,
onion: profiles[len(profiles)-1],
statusViewport: newViewport(m.width, m.height-3),
}
m.profiles = append(m.profiles, newProfile)
// TODO
@ -293,16 +285,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
})
case renderStatusMsg:
content := renderLines(msg.line)
existing := strings.TrimSpace(m.statusViewport.View())
if len(existing) != 0 {
content = existing + "\n" + renderLines(msg.line)
}
m.statusViewport.SetContent(content)
renderedLine := renderLines(msg.line)
m.statusViewportLines = append(m.statusViewportLines, renderedLine)
m.statusViewport.SetContent(strings.Join(m.statusViewportLines, "\n"))
m.statusViewport.GotoBottom()
cmds = append(cmds, m.receiveStatusCmd)
case clearMsg:
@ -326,6 +312,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, m.sendStatusCmd(msg.Error()))
}
m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd)
m.statusViewport, cmd = m.statusViewport.Update(msg)
cmds = append(cmds, cmd)
for _, p := range m.profiles {
p.statusViewport, cmd = p.statusViewport.Update(msg)
cmds = append(cmds, cmd)
}
if m.showWelcomeMessage {
cmds = append(cmds, m.sendStatusCmd(
strings.Split(welcomeMessage, "\n")...,
))
m.showWelcomeMessage = false
}
return m, tea.Batch(cmds...)
}
@ -345,12 +349,15 @@ func (m model) View() string {
switch m.menuState {
case 0:
chosenViewport = m.statusViewport
m.viewportState = 0
default:
if m.menuState > len(m.profiles) {
chosenViewport = m.statusViewport
m.viewportState = 0
break
}
chosenViewport = m.profiles[m.menuState-1].statusViewport
m.viewportState = m.menuState - 1
}
body.WriteString(

View File

@ -32,12 +32,9 @@ func unlockProfiles(m model, password string) profiles {
peer := m.app.GetPeer(onion)
name, _ := peer.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name)
unlocked = append(unlocked, profile{
name: name,
onion: onion,
statusViewport: viewport.New(
m.width,
m.height-3,
),
name: name,
onion: onion,
statusViewport: newViewport(m.width, m.height-3),
})
}

View File

@ -3,6 +3,9 @@ package main
import (
"fmt"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
)
const timePrefixFormat = "15:04:05"
@ -11,7 +14,18 @@ func renderLines(lines ...string) string {
var rendered string
for _, line := range lines {
now := time.Now().Format(timePrefixFormat)
rendered += fmt.Sprintf("%s | %s\n", now, line)
rendered += fmt.Sprintf("%s | %s", now, line)
}
return rendered
}
func newViewport(width, height int) viewport.Model {
viewp := viewport.New(width, height)
viewp.KeyMap = viewport.KeyMap{
PageDown: key.NewBinding(key.WithKeys("pgdown")),
PageUp: key.NewBinding(key.WithKeys("pgup")),
HalfPageUp: key.NewBinding(key.WithKeys("ctrl+u")),
HalfPageDown: key.NewBinding(key.WithKeys("ctrl+d")),
}
return viewp
}