Skip to content

Commit

Permalink
listen as tea cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
somnek committed Apr 6, 2023
1 parent ae6b4e1 commit 1d727a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"fmt"
"log"
"net/http"
"os"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -69,7 +71,8 @@ var (
Align(lipgloss.Center).
Faint(true).
MarginLeft(5).
Border(border).Blink(true)
Border(border).
Blink(true)
)

func initialModel() model {
Expand All @@ -91,13 +94,37 @@ func initialModel() model {
}
}

func checkServer() tea.Msg {
c := &http.Client{Timeout: 10 * time.Second}
res, err := c.Get("https://www.google.com")

if err != nil {
// There was an error making our request. Wrap the error we received
// in a message and return it.
return errMsg{err}
}
// We received a response from the server. Return the HTTP status code
// as a message. int is a valid message type.
return statusMsg(res.StatusCode)
}

type statusMsg int

type errMsg struct{ err error }

func (e errMsg) Error() string { return e.err.Error() }

func (m model) Init() tea.Cmd {
return nil
return checkServer
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {

case statusMsg:
fmt.Println("Server is up! HTTP status code:", msg)
return m, nil

case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
Expand Down
5 changes: 4 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
- created, restarting, running, removing, paused, exited, or dead
* src: [link](https://docs.docker.com/engine/reference/commandline/ps/#filter)


# REF
> --------
- [command tutorial](https://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands/)
- [p.Send](https://github.com/charmbracelet/bubbletea/issues/25#issuecomment-872331380)

0 comments on commit 1d727a1

Please sign in to comment.