Files
pipelines/internal/tui/status.go
T

79 lines
2.1 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"
"net/url"
)
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 {
connection := "invalid URL"
if u, err := url.Parse(m.url); err == nil {
u.User = nil
u.RawQuery = ""
u.Fragment = ""
connection = u.String()
}
lines := []string{"Client connection: " + connection}
if m.statusRequest != "" {
lines = append(lines, "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 {
lines := m.statusLines()
page := max(1, height-5)
top := min(m.statusTop, max(0, len(lines)-page))
rows := []string{"Status · Pipelines & ntfy", "↑/↓ scroll · PgUp/PgDn · r refresh · Esc close", ""}
rows = append(rows, lines[top:min(len(lines), top+page)]...)
return popup(m.width, height, 108, rows)
}