details.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package logs
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/charmbracelet/bubbles/key"
  9. "github.com/charmbracelet/bubbles/viewport"
  10. tea "github.com/charmbracelet/bubbletea"
  11. "github.com/charmbracelet/lipgloss"
  12. "github.com/sst/opencode/internal/logging"
  13. "github.com/sst/opencode/internal/tui/layout"
  14. "github.com/sst/opencode/internal/tui/styles"
  15. "github.com/sst/opencode/internal/tui/theme"
  16. )
  17. type DetailComponent interface {
  18. tea.Model
  19. layout.Sizeable
  20. layout.Bindings
  21. }
  22. type detailCmp struct {
  23. width, height int
  24. currentLog logging.Log
  25. viewport viewport.Model
  26. focused bool
  27. }
  28. func (i *detailCmp) Init() tea.Cmd {
  29. return nil
  30. }
  31. func (i *detailCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  32. var cmd tea.Cmd
  33. switch msg := msg.(type) {
  34. case selectedLogMsg:
  35. if msg.ID != i.currentLog.ID {
  36. i.currentLog = logging.Log(msg)
  37. // Defer content update to avoid blocking the UI
  38. cmd = tea.Tick(time.Millisecond*1, func(time.Time) tea.Msg {
  39. i.updateContent()
  40. return nil
  41. })
  42. }
  43. case tea.KeyMsg:
  44. // Only process keyboard input when focused
  45. if !i.focused {
  46. return i, nil
  47. }
  48. // Handle keyboard input for scrolling
  49. i.viewport, cmd = i.viewport.Update(msg)
  50. return i, cmd
  51. }
  52. return i, cmd
  53. }
  54. func (i *detailCmp) updateContent() {
  55. var content strings.Builder
  56. t := theme.CurrentTheme()
  57. // Format the header with timestamp and level
  58. timeStyle := lipgloss.NewStyle().Foreground(t.TextMuted())
  59. levelStyle := getLevelStyle(i.currentLog.Level)
  60. // Format timestamp
  61. timeStr := i.currentLog.Timestamp.Format(time.RFC3339)
  62. header := lipgloss.JoinHorizontal(
  63. lipgloss.Center,
  64. timeStyle.Render(timeStr),
  65. " ",
  66. levelStyle.Render(i.currentLog.Level),
  67. )
  68. content.WriteString(lipgloss.NewStyle().Bold(true).Render(header))
  69. content.WriteString("\n\n")
  70. // Message with styling
  71. messageStyle := lipgloss.NewStyle().Bold(true).Foreground(t.Text())
  72. content.WriteString(messageStyle.Render("Message:"))
  73. content.WriteString("\n")
  74. content.WriteString(lipgloss.NewStyle().Padding(0, 2).Width(i.width).Render(i.currentLog.Message))
  75. content.WriteString("\n\n")
  76. // Attributes section
  77. if len(i.currentLog.Attributes) > 0 {
  78. attrHeaderStyle := lipgloss.NewStyle().Bold(true).Foreground(t.Text())
  79. content.WriteString(attrHeaderStyle.Render("Attributes:"))
  80. content.WriteString("\n")
  81. // Create a table-like display for attributes
  82. keyStyle := lipgloss.NewStyle().Foreground(t.Primary()).Bold(true)
  83. valueStyle := lipgloss.NewStyle().Foreground(t.Text())
  84. for key, value := range i.currentLog.Attributes {
  85. // if value is JSON, render it with indentation
  86. if strings.HasPrefix(value, "{") {
  87. var indented bytes.Buffer
  88. if err := json.Indent(&indented, []byte(value), "", " "); err != nil {
  89. indented.WriteString(value)
  90. }
  91. value = indented.String()
  92. }
  93. attrLine := fmt.Sprintf("%s: %s",
  94. keyStyle.Render(key),
  95. valueStyle.Render(value),
  96. )
  97. content.WriteString(lipgloss.NewStyle().Padding(0, 2).Width(i.width).Render(attrLine))
  98. content.WriteString("\n")
  99. }
  100. }
  101. // Session ID if available
  102. if i.currentLog.SessionID != "" {
  103. sessionStyle := lipgloss.NewStyle().Bold(true).Foreground(t.Text())
  104. content.WriteString("\n")
  105. content.WriteString(sessionStyle.Render("Session:"))
  106. content.WriteString("\n")
  107. content.WriteString(lipgloss.NewStyle().Padding(0, 2).Width(i.width).Render(i.currentLog.SessionID))
  108. }
  109. i.viewport.SetContent(content.String())
  110. }
  111. func getLevelStyle(level string) lipgloss.Style {
  112. style := lipgloss.NewStyle().Bold(true)
  113. t := theme.CurrentTheme()
  114. switch strings.ToLower(level) {
  115. case "info":
  116. return style.Foreground(t.Info())
  117. case "warn", "warning":
  118. return style.Foreground(t.Warning())
  119. case "error", "err":
  120. return style.Foreground(t.Error())
  121. case "debug":
  122. return style.Foreground(t.Success())
  123. default:
  124. return style.Foreground(t.Text())
  125. }
  126. }
  127. func (i *detailCmp) View() string {
  128. t := theme.CurrentTheme()
  129. return styles.ForceReplaceBackgroundWithLipgloss(i.viewport.View(), t.Background())
  130. }
  131. func (i *detailCmp) GetSize() (int, int) {
  132. return i.width, i.height
  133. }
  134. func (i *detailCmp) SetSize(width int, height int) tea.Cmd {
  135. i.width = width
  136. i.height = height
  137. i.viewport.Width = i.width
  138. i.viewport.Height = i.height
  139. i.updateContent()
  140. return nil
  141. }
  142. func (i *detailCmp) BindingKeys() []key.Binding {
  143. return layout.KeyMapToSlice(i.viewport.KeyMap)
  144. }
  145. func NewLogsDetails() DetailComponent {
  146. return &detailCmp{
  147. viewport: viewport.New(0, 0),
  148. }
  149. }
  150. // Focus implements the focusable interface
  151. func (i *detailCmp) Focus() {
  152. i.focused = true
  153. i.viewport.SetYOffset(i.viewport.YOffset)
  154. }
  155. // Blur implements the blurable interface
  156. func (i *detailCmp) Blur() {
  157. i.focused = false
  158. }