Files
pipelines/internal/tui/status.go
T

89 lines
2.8 KiB
Go

package tui
import (
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/statusview"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"strings"
)
func (m Model) openStatus() (tea.Model, tea.Cmd) {
m.statusOpen = true
m.statusTop = 0
m.systemStatus = nil
m.statusError = ""
return m.refreshStatus()
}
func (m Model) refreshStatus() (tea.Model, tea.Cmd) {
if m.conn == nil {
m.statusError = "Not connected to the Pipelines server."
return m, nil
}
m.nextID++
m.statusRequest = fmt.Sprintf("status-%d", m.nextID)
m.statusError = ""
return m, m.send(protocol.Message{Type: protocol.GetStatus, ID: m.statusRequest})
}
func (m Model) updateStatus(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc", "q":
m.statusOpen = false
return m, nil
case "r":
return m.refreshStatus()
case "j", "down":
m.statusTop++
case "k", "up":
m.statusTop--
case "pgdown":
m.statusTop += max(1, m.height-10)
case "pgup":
m.statusTop -= max(1, m.height-10)
}
m.statusTop = max(0, min(m.statusTop, max(0, len(m.statusLines())-max(1, m.height-9))))
return m, nil
}
func (m Model) statusLines() []string {
lines := []string{}
if m.statusRequest != "" {
lines = append(lines, m.loading("Checking server and ntfy…"))
}
if m.statusError != "" {
lines = append(lines, m.statusError)
if m.systemStatus == nil {
lines = append(lines, "ntfy: unknown (server information unavailable)")
}
}
if m.systemStatus != nil {
lines = append(lines, " ")
lines = append(lines, statusview.Lines(m.systemStatus)...)
}
return lines
}
func (m Model) statusView(height int) string {
server, ntfy := statusview.ServerLines(m.systemStatus), statusview.NtfyLines(m.systemStatus)
if m.systemStatus == nil && m.statusError != "" {
s := statusview.Offline(m.url)
server, ntfy = statusview.ServerLines(s), statusview.NtfyLines(s)
}
if m.statusError != "" {
server = append([]string{m.statusError}, server...)
}
if m.statusRequest != "" {
server = append([]string{m.loading("Checking…")}, server...)
}
width := m.width
if width >= 100 {
w := (width - 2) / 2
left := panel(w, max(4, height-2), []string{"Pipelines server"}, server, "", false)
right := panel(width-w-2, max(4, height-2), []string{"ntfy · phone connection"}, ntfy, "", false)
return fitBlock(append(strings.Split(lipgloss.JoinHorizontal(lipgloss.Top, left, " ", right), "\n"), "", "r refresh · Esc close"), width, height)
}
topHeight := max(4, (height-2)/2)
left := panel(width, topHeight, []string{"Pipelines server"}, server, "", false)
right := panel(width, max(4, height-topHeight-2), []string{"ntfy · phone connection"}, ntfy, "", false)
return fitBlock(append(strings.Split(left+"\n"+right, "\n"), "r refresh · Esc close"), width, height)
}