|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/The-Mod-Elephant/infinity_file_formats/bg" |
| 10 | + "github.com/charmbracelet/bubbles/key" |
| 11 | + "github.com/charmbracelet/bubbles/table" |
| 12 | + tea "github.com/charmbracelet/bubbletea" |
| 13 | +) |
| 14 | + |
| 15 | +type ModList struct { |
| 16 | + *table.Model |
| 17 | +} |
| 18 | + |
| 19 | +func NewModList() ModList { |
| 20 | + columns := []table.Column{ |
| 21 | + {Title: "Index", Width: int(0.1 * float64(width))}, |
| 22 | + {Title: "Tp2 file", Width: int(0.35 * float64(width))}, |
| 23 | + {Title: "Language", Width: int(0.1 * float64(width))}, |
| 24 | + {Title: "Component", Width: int(0.1 * float64(width))}, |
| 25 | + {Title: "Component name", Width: int(0.35 * float64(width))}, |
| 26 | + } |
| 27 | + t := table.New( |
| 28 | + table.WithColumns(columns), |
| 29 | + table.WithFocused(true), |
| 30 | + table.WithHeight(height-7), |
| 31 | + ) |
| 32 | + return ModList{&t} |
| 33 | +} |
| 34 | + |
| 35 | +func (m ModList) Init() tea.Cmd { |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +func (m ModList) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 40 | + if !m.Focused() { |
| 41 | + return m, nil |
| 42 | + } |
| 43 | + |
| 44 | + switch msg := msg.(type) { |
| 45 | + case SelectedFilePath: |
| 46 | + path := filepath.Join(filepath.Clean(string(msg)), "weidu.log") |
| 47 | + file, err := os.Open(filepath.Clean(path)) |
| 48 | + if err != nil { |
| 49 | + return m, tea.Quit |
| 50 | + } |
| 51 | + defer file.Close() |
| 52 | + log, err := bg.OpenLog(file) |
| 53 | + if err != nil { |
| 54 | + return m, tea.Quit |
| 55 | + } |
| 56 | + rows := []table.Row{} |
| 57 | + for i, c := range log.Components { |
| 58 | + items := []string{strconv.Itoa(i), fmt.Sprintf("%s%s%s", c.Name, string(os.PathSeparator), c.TpFile), c.Lang, c.Component, c.ComponentName} |
| 59 | + rows = append(rows, table.Row(items)) |
| 60 | + } |
| 61 | + m.SetRows(rows) |
| 62 | + case tea.KeyMsg: |
| 63 | + switch { |
| 64 | + case key.Matches(msg, m.KeyMap.LineUp): |
| 65 | + m.MoveUp(1) |
| 66 | + case key.Matches(msg, m.KeyMap.LineDown): |
| 67 | + m.MoveDown(1) |
| 68 | + case key.Matches(msg, m.KeyMap.PageUp): |
| 69 | + m.MoveUp(m.Height()) |
| 70 | + case key.Matches(msg, m.KeyMap.PageDown): |
| 71 | + m.MoveDown(m.Height()) |
| 72 | + case key.Matches(msg, m.KeyMap.HalfPageUp): |
| 73 | + m.MoveUp(m.Height() / 2) |
| 74 | + case key.Matches(msg, m.KeyMap.HalfPageDown): |
| 75 | + m.MoveDown(m.Height() / 2) |
| 76 | + case key.Matches(msg, m.KeyMap.GotoTop): |
| 77 | + m.GotoTop() |
| 78 | + case key.Matches(msg, m.KeyMap.GotoBottom): |
| 79 | + m.GotoBottom() |
| 80 | + } |
| 81 | + switch msg.String() { |
| 82 | + case "q", "esc": |
| 83 | + return state.PreviousCommand(), nil |
| 84 | + case "ctrl+c", "ctrl+d": |
| 85 | + return m, tea.Quit |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return m, nil |
| 90 | +} |
0 commit comments