sage/main.go

93 lines
1.7 KiB
Go
Raw Normal View History

2024-03-30 12:36:54 +00:00
package main
import (
2024-03-30 12:50:56 +00:00
"fmt"
"os"
2024-03-30 12:54:11 +00:00
"strings"
2024-03-30 12:36:54 +00:00
2024-03-30 12:50:56 +00:00
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2024-03-30 12:36:54 +00:00
2024-03-30 12:50:56 +00:00
"github.com/prometheus-community/pro-bing"
2024-03-30 12:36:54 +00:00
)
var (
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓")
2024-03-30 12:50:56 +00:00
docStyle = lipgloss.NewStyle().Margin(1, 2)
2024-03-30 12:54:11 +00:00
titleStyle = lipgloss.NewStyle().Bold(true)
2024-03-30 12:36:54 +00:00
)
2024-03-30 12:45:22 +00:00
type keyMap struct {
2024-03-30 12:50:56 +00:00
Quit key.Binding
2024-03-30 12:45:22 +00:00
}
2024-03-30 12:50:56 +00:00
2024-03-30 12:45:22 +00:00
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Quit}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Quit},
}
}
2024-03-30 12:50:56 +00:00
2024-03-30 12:45:22 +00:00
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:50:56 +00:00
keys keyMap
help help.Model
addresses []string
2024-03-30 12:36:54 +00:00
}
func initialModel() model {
return model{
2024-03-30 12:50:56 +00:00
keys: keys,
help: help.New(),
addresses: []string{"100.64.0.1", "100.64.0.2"},
2024-03-30 12:36:54 +00:00
}
}
func (m model) Init() tea.Cmd {
2024-03-30 12:50:56 +00:00
return nil
2024-03-30 12:36:54 +00:00
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2024-03-30 12:50:56 +00:00
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.Quit):
return m, tea.Quit
}
}
return m, nil
2024-03-30 12:36:54 +00:00
}
func (m model) View() string {
2024-03-30 12:54:11 +00:00
s := titleStyle.Render("Pinging hosts...") + strings.Repeat("\n", 2)
2024-03-30 12:50:56 +00:00
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
2024-03-30 12:50:56 +00:00
helpView := m.help.View(m.keys)
return docStyle.Render(s + "\n" + helpView)
2024-03-30 12:36:54 +00:00
}
func main() {
2024-03-30 12:50:56 +00:00
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
2024-03-30 12:36:54 +00:00
}