Skip to content

How to show stdout of a command into a viewport #928

Answered by meowgorithm
nihal-innsof asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! Here’s a simple program to get you started:

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"os/exec"

	"github.com/charmbracelet/bubbles/viewport"
	tea "github.com/charmbracelet/bubbletea"
)

const viewportHeight = 8

type pingMsg string

type pingErrMsg struct{ err error }

type pingDoneMsg struct{}

func ping(domain string, sub chan string) tea.Cmd {
	return func() tea.Msg {
		// Setup the command
		cmd := exec.Command("ping", "-c", "10", domain)
		out, err := cmd.StdoutPipe()
		if err != nil {
			return pingErrMsg{err}
		}

		// Run the command.
		if err := cmd.Start(); err != nil {
			return pingErrMsg{err}
		}

		// Read command output as it arrives.
		buf := bufio.NewReader(o…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@nihal-innsof
Comment options

Answer selected by nihal-innsof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants