sage/main.go

88 lines
1.7 KiB
Go
Raw Normal View History

2024-03-30 12:36:54 +00:00
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2024-03-30 12:45:22 +00:00
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/help"
2024-03-30 12:36:54 +00:00
"github.com/prometheus-community/pro-bing"
)
var (
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓")
)
2024-03-30 12:45:22 +00:00
type keyMap struct {
Quit key.Binding
}
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Quit}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Quit},
}
}
var keys = keyMap{
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
}
2024-03-30 12:36:54 +00:00
type model struct {
2024-03-30 12:45:22 +00:00
keys keyMap
help help.Model
2024-03-30 12:36:54 +00:00
addresses []string
}
func initialModel() model {
return model{
2024-03-30 12:45:22 +00:00
keys: keys,
help: help.New(),
2024-03-30 12:36:54 +00:00
addresses: []string{"100.64.0.1", "100.64.0.2"},
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
2024-03-30 12:45:22 +00:00
switch {
case key.Matches(msg, m.keys.Quit):
2024-03-30 12:36:54 +00:00
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
s := "Pinging hosts...\n\n"
for _, address := range m.addresses {
_, err := probing.NewPinger(fmt.Sprintf("%s", address))
if err != nil {
panic(err)
} else {
s += fmt.Sprintf("%s %s\n", checkMark, address)
}
}
2024-03-30 12:45:22 +00:00
helpView := m.help.View(m.keys)
return s + "\n" + helpView
2024-03-30 12:36:54 +00:00
}
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
}