Skip to content

Commit 5f06cd3

Browse files
committed
allow logging frames (for debugging)
1 parent d298983 commit 5f06cd3

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

internal/ui/initial.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515
textInputWidth = 80
1616
)
1717

18-
func InitialModel(db *sql.DB, style Style, debug bool) Model {
18+
func InitialModel(db *sql.DB, style Style, debug bool, logFrames bool) Model {
1919
var activeTaskItems []list.Item
2020
var inactiveTaskItems []list.Item
2121
var tasklogListItems []list.Item
@@ -73,6 +73,7 @@ This can be used to record details about your work on this task.`
7373
tLCommentInput: tLCommentInput,
7474
taskInputs: taskInputs,
7575
debug: debug,
76+
logFrames: logFrames,
7677
}
7778
m.activeTasksList.Title = "Tasks"
7879
m.activeTasksList.SetStatusBarItemName("task", "tasks")

internal/ui/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type Model struct {
127127
terminalHeight int
128128
trackingActive bool
129129
debug bool
130+
logFrames bool
130131
}
131132

132133
func (m *Model) blurTLTrackingInputs() {

internal/ui/ui.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
tea "github.com/charmbracelet/bubbletea"
1010
)
1111

12-
var errFailedToConfigureDebugging = errors.New("failed to configure debugging")
12+
var (
13+
errFailedToConfigureDebugging = errors.New("failed to configure debugging")
14+
errCouldnCreateFramesDir = errors.New("couldn't create frames directory")
15+
)
1316

1417
func RenderUI(db *sql.DB, style Style) error {
1518
if len(os.Getenv("DEBUG")) > 0 {
@@ -21,8 +24,15 @@ func RenderUI(db *sql.DB, style Style) error {
2124
}
2225

2326
debug := os.Getenv("HOURS_DEBUG") == "1"
27+
logFrames := os.Getenv("HOURS_LOG_FRAMES") == "1"
28+
if logFrames {
29+
err := os.MkdirAll("frames", 0o755)
30+
if err != nil {
31+
return fmt.Errorf("%w: %s", errCouldnCreateFramesDir, err.Error())
32+
}
33+
}
2434

25-
p := tea.NewProgram(InitialModel(db, style, debug), tea.WithAltScreen())
35+
p := tea.NewProgram(InitialModel(db, style, debug, logFrames), tea.WithAltScreen())
2636
_, err := p.Run()
2737

2838
return err

internal/ui/view.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package ui
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
7+
"regexp"
58
"strings"
69
"time"
710

@@ -27,6 +30,8 @@ const (
2730
tlSubmitErr
2831
)
2932

33+
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
34+
3035
func (m Model) View() string {
3136
var content string
3237
var footer string
@@ -312,11 +317,17 @@ func (m Model) View() string {
312317
)
313318
}
314319

315-
return lipgloss.JoinVertical(lipgloss.Left,
320+
result := lipgloss.JoinVertical(lipgloss.Left,
316321
content,
317322
statusBar,
318323
footer,
319324
)
325+
326+
if m.logFrames {
327+
logFrame(result)
328+
}
329+
330+
return result
320331
}
321332

322333
func (m recordsModel) View() string {
@@ -397,3 +408,16 @@ func getDurationValidityContext(beginStr, endStr string) (string, tlFormValidity
397408

398409
return msg, tlSubmitOk
399410
}
411+
412+
func logFrame(content string) {
413+
cleanContent := stripANSI(content)
414+
415+
filename := fmt.Sprintf("frame-%d.txt", time.Now().Unix())
416+
filepath := filepath.Join("frames", filename)
417+
418+
_ = os.WriteFile(filepath, []byte(cleanContent), 0o644)
419+
}
420+
421+
func stripANSI(s string) string {
422+
return ansiRegex.ReplaceAllString(s, "")
423+
}

0 commit comments

Comments
 (0)