|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + |
| 6 | + "github.com/The-Mod-Elephant/infinity_dialog/pkg/readers" |
| 7 | + "github.com/charmbracelet/bubbles/key" |
| 8 | + "github.com/charmbracelet/bubbles/table" |
| 9 | + tea "github.com/charmbracelet/bubbletea" |
| 10 | +) |
| 11 | + |
| 12 | +type ModList struct { |
| 13 | + *table.Model |
| 14 | +} |
| 15 | + |
| 16 | +func NewModList() ModList { |
| 17 | + columns := []table.Column{ |
| 18 | + {Title: "Index", Width: int(0.1 * float64(width))}, |
| 19 | + {Title: "Tp2 file", Width: int(0.35 * float64(width))}, |
| 20 | + {Title: "Language", Width: int(0.1 * float64(width))}, |
| 21 | + {Title: "Component", Width: int(0.1 * float64(width))}, |
| 22 | + {Title: "Component name", Width: int(0.35 * float64(width))}, |
| 23 | + } |
| 24 | + t := table.New( |
| 25 | + table.WithColumns(columns), |
| 26 | + table.WithFocused(true), |
| 27 | + table.WithHeight(height-7), |
| 28 | + ) |
| 29 | + return ModList{&t} |
| 30 | +} |
| 31 | + |
| 32 | +func (m ModList) Init() tea.Cmd { |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +func (m ModList) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 37 | + if !m.Focused() { |
| 38 | + return m, nil |
| 39 | + } |
| 40 | + |
| 41 | + switch msg := msg.(type) { |
| 42 | + case PathMsg: |
| 43 | + path := filepath.Join("weidu.log", filepath.Clean(string(msg))) |
| 44 | + _, err := readers.ReadFileToSlice(path) |
| 45 | + if err != nil { |
| 46 | + return m, tea.Quit |
| 47 | + } |
| 48 | + |
| 49 | + case tea.KeyMsg: |
| 50 | + switch { |
| 51 | + case key.Matches(msg, m.KeyMap.LineUp): |
| 52 | + m.MoveUp(1) |
| 53 | + case key.Matches(msg, m.KeyMap.LineDown): |
| 54 | + m.MoveDown(1) |
| 55 | + case key.Matches(msg, m.KeyMap.PageUp): |
| 56 | + m.MoveUp(m.Height()) |
| 57 | + case key.Matches(msg, m.KeyMap.PageDown): |
| 58 | + m.MoveDown(m.Height()) |
| 59 | + case key.Matches(msg, m.KeyMap.HalfPageUp): |
| 60 | + m.MoveUp(m.Height() / 2) |
| 61 | + case key.Matches(msg, m.KeyMap.HalfPageDown): |
| 62 | + m.MoveDown(m.Height() / 2) |
| 63 | + case key.Matches(msg, m.KeyMap.GotoTop): |
| 64 | + m.GotoTop() |
| 65 | + case key.Matches(msg, m.KeyMap.GotoBottom): |
| 66 | + m.GotoBottom() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return m, nil |
| 71 | +} |
0 commit comments