-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (82 loc) · 2.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bufio"
"fmt"
"io"
"log"
"os/exec"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type process struct {
command string
arguments []string
view *tview.TextView
}
func startChildProcess(outputTextView *tview.TextView, program string, arguments ...string) {
cmd := exec.Command(program, arguments...)
stdout, err := cmd.StdoutPipe()
if err != nil { log.Fatal(err) }
_, err = cmd.StdinPipe()
if err != nil { log.Fatal(err) }
_, err = cmd.StderrPipe()
if err != nil { log.Fatal(err) }
err = cmd.Start()
if err != nil { log.Fatal(err) }
go func () {
reader := bufio.NewReader(stdout)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF { break }
fmt.Printf("Error reading process stdout: %v\n", err)
break
}
fmt.Fprintf(outputTextView, "%s ", line)
outputTextView.ScrollToEnd()
}
}()
}
func main() {
// spawn application
app := tview.NewApplication()
inputField := tview.NewInputField().
SetLabel("Enter Input (Press Enter to send): ").
SetFieldWidth(40)
// start subprocesses
processes := []process {
{ command: "fetus/fetus", arguments: []string{"1", "1"}, view: tview.NewTextView()},
{ command: "fetus/fetus", arguments: []string{"2", "3"}, view: tview.NewTextView()},
{ command: "fetus/fetus", arguments: []string{"3", "5"}, view: tview.NewTextView()},
{ command: "ping", arguments: []string{"www.google.com"}, view: tview.NewTextView()},
}
for _, p := range processes {
if p.view != nil {
p.view.SetChangedFunc(func() { app.Draw() })
p.view.SetBorder(true)
p.view.SetScrollable(true)
}
go startChildProcess(p.view, p.command, p.arguments...)
}
inputField.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
inputText := inputField.GetText()
inputField.SetText("")
for _, p := range processes {
p.view.SetText(inputText)
}
}
})
// Create a flex layout to split the windows
flex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(inputField, 0, 1, false)
viewFlex := tview.NewFlex().SetDirection(tview.FlexRow)
for _, p := range processes {
viewFlex.AddItem(p.view, 0, 1, false)
}
flex.AddItem(viewFlex, 0, 10, false)
if err := app.SetRoot(flex, true).SetFocus(inputField).Run(); err != nil {
fmt.Println(err)
}
}