table.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package logs
  2. import (
  3. "encoding/json"
  4. "slices"
  5. "github.com/charmbracelet/bubbles/key"
  6. "github.com/charmbracelet/bubbles/table"
  7. tea "github.com/charmbracelet/bubbletea"
  8. "github.com/kujtimiihoxha/termai/internal/logging"
  9. "github.com/kujtimiihoxha/termai/internal/pubsub"
  10. "github.com/kujtimiihoxha/termai/internal/tui/layout"
  11. "github.com/kujtimiihoxha/termai/internal/tui/styles"
  12. "github.com/kujtimiihoxha/termai/internal/tui/util"
  13. )
  14. type TableComponent interface {
  15. tea.Model
  16. layout.Focusable
  17. layout.Sizeable
  18. layout.Bindings
  19. }
  20. var logger = logging.Get()
  21. type tableCmp struct {
  22. table table.Model
  23. }
  24. type selectedLogMsg logging.LogMessage
  25. func (i *tableCmp) Init() tea.Cmd {
  26. i.setRows()
  27. return nil
  28. }
  29. func (i *tableCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  30. var cmds []tea.Cmd
  31. if i.table.Focused() {
  32. switch msg.(type) {
  33. case pubsub.Event[logging.LogMessage]:
  34. i.setRows()
  35. return i, nil
  36. }
  37. prevSelectedRow := i.table.SelectedRow()
  38. t, cmd := i.table.Update(msg)
  39. cmds = append(cmds, cmd)
  40. i.table = t
  41. selectedRow := i.table.SelectedRow()
  42. if selectedRow != nil {
  43. if prevSelectedRow == nil || selectedRow[0] == prevSelectedRow[0] {
  44. var log logging.LogMessage
  45. for _, row := range logging.Get().List() {
  46. if row.ID == selectedRow[0] {
  47. log = row
  48. break
  49. }
  50. }
  51. if log.ID != "" {
  52. cmds = append(cmds, util.CmdHandler(selectedLogMsg(log)))
  53. }
  54. }
  55. }
  56. }
  57. return i, tea.Batch(cmds...)
  58. }
  59. func (i *tableCmp) View() string {
  60. return i.table.View()
  61. }
  62. func (i *tableCmp) Blur() tea.Cmd {
  63. i.table.Blur()
  64. return nil
  65. }
  66. func (i *tableCmp) Focus() tea.Cmd {
  67. i.table.Focus()
  68. return nil
  69. }
  70. func (i *tableCmp) IsFocused() bool {
  71. return i.table.Focused()
  72. }
  73. func (i *tableCmp) GetSize() (int, int) {
  74. return i.table.Width(), i.table.Height()
  75. }
  76. func (i *tableCmp) SetSize(width int, height int) {
  77. i.table.SetWidth(width)
  78. i.table.SetHeight(height)
  79. cloumns := i.table.Columns()
  80. for i, col := range cloumns {
  81. col.Width = (width / len(cloumns)) - 2
  82. cloumns[i] = col
  83. }
  84. i.table.SetColumns(cloumns)
  85. }
  86. func (i *tableCmp) BindingKeys() []key.Binding {
  87. return layout.KeyMapToSlice(i.table.KeyMap)
  88. }
  89. func (i *tableCmp) setRows() {
  90. rows := []table.Row{}
  91. logs := logger.List()
  92. slices.SortFunc(logs, func(a, b logging.LogMessage) int {
  93. if a.Time.Before(b.Time) {
  94. return 1
  95. }
  96. if a.Time.After(b.Time) {
  97. return -1
  98. }
  99. return 0
  100. })
  101. for _, log := range logs {
  102. bm, _ := json.Marshal(log.Attributes)
  103. row := table.Row{
  104. log.ID,
  105. log.Time.Format("15:04:05"),
  106. log.Level,
  107. log.Message,
  108. string(bm),
  109. }
  110. rows = append(rows, row)
  111. }
  112. i.table.SetRows(rows)
  113. }
  114. func NewLogsTable() TableComponent {
  115. columns := []table.Column{
  116. {Title: "ID", Width: 4},
  117. {Title: "Time", Width: 4},
  118. {Title: "Level", Width: 10},
  119. {Title: "Message", Width: 10},
  120. {Title: "Attributes", Width: 10},
  121. }
  122. defaultStyles := table.DefaultStyles()
  123. defaultStyles.Selected = defaultStyles.Selected.Foreground(styles.Primary)
  124. tableModel := table.New(
  125. table.WithColumns(columns),
  126. table.WithStyles(defaultStyles),
  127. )
  128. return &tableCmp{
  129. table: tableModel,
  130. }
  131. }