Skip to content

Commit

Permalink
support headless
Browse files Browse the repository at this point in the history
  • Loading branch information
osterman committed Feb 17, 2025
1 parent 7721fc7 commit 84f4b76
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
1 change: 0 additions & 1 deletion demo/recordings/mp4/.gitignore

This file was deleted.

4 changes: 4 additions & 0 deletions demo/recordings/studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func init() {
ReportCaller: false,
}))

log.SetLevel(log.DebugLevel)

repoRoot, err = getGitRoot()
if err != nil {
log.Fatal("Error detecting Git root", "error", err)
Expand All @@ -56,6 +58,8 @@ func init() {
mp4OutDir = filepath.Join(repoRoot, "demo", "recordings", "mp4")
gifOutDir = filepath.Join(repoRoot, "demo", "recordings", "gif")
audioFile = filepath.Join(repoRoot, "demo", "recordings", "background.mp3")

log.Info("Initialized", "repoRoot", ConvertToRelativeFromCWD(repoRoot), "tapesDir", ConvertToRelativeFromCWD(tapesDir), "scenesDir", ConvertToRelativeFromCWD(scenesDir), "mp4OutDir", ConvertToRelativeFromCWD(mp4OutDir), "gifOutDir", ConvertToRelativeFromCWD(gifOutDir), "audioFile", ConvertToRelativeFromCWD(audioFile))
}

func main() {
Expand Down
62 changes: 44 additions & 18 deletions internal/tui/viewport/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
Expand All @@ -14,6 +15,8 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"golang.org/x/term"
)

// Constants
Expand Down Expand Up @@ -143,6 +146,7 @@ type Model struct {
Start time.Time
width int
ExitCode int
hasTTY bool
done bool
}

Expand All @@ -152,12 +156,19 @@ func newModel(title string, fn func(chan string, *[]string) (int, error)) Model
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("12"))

vp := viewport.New(80, viewportHeight)
vp.SetContent("")

// ✅ Allocate logLines as a pointer
// Allocate logLines as a pointer
logLines := &[]string{}

tty := term.IsTerminal(int(os.Stdout.Fd())) && os.Getenv("TERM") != "dumb"

log.Debug("tty", "enabled", tty)

var vp viewport.Model
if tty {
vp = viewport.New(80, viewportHeight)
vp.SetContent("")
}

return Model{
title: title,
spinner: s,
Expand All @@ -167,6 +178,7 @@ func newModel(title string, fn func(chan string, *[]string) (int, error)) Model
viewport: vp,
Start: time.Now(),
ExitCode: -1,
hasTTY: tty,
width: 80,
}
}
Expand Down Expand Up @@ -198,9 +210,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width - (2 * outerMargin)
m.viewport.Width = m.width - 4
m.viewport.Height = viewportHeight
if m.hasTTY {
// Adjust the viewport width
m.width = msg.Width - (2 * outerMargin)
m.viewport.Width = m.width - 4
m.viewport.Height = viewportHeight
}
case tea.KeyMsg:
if msg.String() == "q" || msg.String() == "ctrl+c" || msg.String() == "esc" {
m.done = true
Expand All @@ -224,7 +239,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)
if m.hasTTY {
m.viewport, cmd = m.viewport.Update(msg)
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
Expand All @@ -237,16 +254,17 @@ func (m *Model) appendOutput(s string) {
}

// Update viewport content
if m.hasTTY {
// Manually wrap lines for viewport display
var wrappedLines []string
for _, line := range *m.LogLines {
wrappedLines = append(wrappedLines, wrapText(line, m.viewport.Width-4)...) // Adjust for borders
}

// Manually wrap lines for viewport display
var wrappedLines []string
for _, line := range *m.LogLines {
wrappedLines = append(wrappedLines, wrapText(line, m.viewport.Width-4)...) // Adjust for borders
// Set viewport content
m.viewport.SetContent(strings.Join(wrappedLines, "\n"))
m.viewport.GotoBottom()
}

// Set viewport content
m.viewport.SetContent(strings.Join(wrappedLines, "\n"))
m.viewport.GotoBottom()
}

func wrapText(text string, width int) []string {
Expand Down Expand Up @@ -281,10 +299,18 @@ func (m Model) View() string {
Render(timer)

// Header
header := lipgloss.NewStyle().
spinner := lipgloss.NewStyle().
Bold(true).
Render(m.spinner.View() + m.title + timerStyled)

// If no TTY, only show spinner or error logs
if !m.hasTTY {
if m.done && m.ExitCode != 0 {
return fmt.Sprintf("%s\nError output:\n%s", spinner, strings.Join(*m.LogLines, "\n"))
}
return spinner // Only show the spinner with elapsed time
}

// Viewport box
viewportBox := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
Expand All @@ -301,5 +327,5 @@ func (m Model) View() string {
Render("press `q` to quit")

return lipgloss.NewStyle().
Render(header + "\n" + viewportBox + "\n" + footer)
Render(spinner + "\n" + viewportBox + "\n" + footer)
}

0 comments on commit 84f4b76

Please sign in to comment.