Skip to content

Instantly share code, notes, and snippets.

@ishuah
Created March 10, 2021 06:47
Show Gist options
  • Save ishuah/6e39845670b156ffae429b986d283d13 to your computer and use it in GitHub Desktop.
Save ishuah/6e39845670b156ffae429b986d283d13 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"os/exec"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/creack/pty"
)
func main() {
a := app.New()
w := a.NewWindow("germ")
ui := widget.NewTextGrid() // Create a new TextGrid
ui.SetText("I'm on a terminal!") // Set text to display
c := exec.Command("/bin/bash")
p, err := pty.Start(c)
if err != nil {
fyne.LogError("Failed to open pty", err)
os.Exit(1)
}
defer c.Process.Kill()
onTypedKey := func(e *fyne.KeyEvent) {
if e.Name == fyne.KeyEnter || e.Name == fyne.KeyReturn {
_, _ = p.Write([]byte{'\r'})
}
}
onTypedRune := func(r rune) {
_, _ = p.WriteString(string(r))
}
w.Canvas().SetOnTypedKey(onTypedKey)
w.Canvas().SetOnTypedRune(onTypedRune)
go func() {
for {
time.Sleep(1 * time.Second)
b := make([]byte, 256)
_, err = p.Read(b)
if err != nil {
fyne.LogError("Failed to read pty", err)
}
ui.SetText(string(b))
}
}()
// Create a new container with a wrapped layout
// set the layout width to 420, height to 200
w.SetContent(
fyne.NewContainerWithLayout(
layout.NewGridWrapLayout(fyne.NewSize(420, 200)),
ui,
),
)
w.ShowAndRun()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment