|
1 | 1 | package app |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
| 5 | + |
4 | 6 | tea "github.com/charmbracelet/bubbletea" |
5 | 7 | "github.com/charmbracelet/lipgloss" |
6 | 8 | "github.com/maniac-en/req/internal/tui/styles" |
| 9 | + "github.com/maniac-en/req/internal/tui/views" |
| 10 | +) |
| 11 | + |
| 12 | +type ViewName string |
| 13 | + |
| 14 | +const ( |
| 15 | + Collections ViewName = "collections" |
7 | 16 | ) |
8 | 17 |
|
9 | 18 | type AppModel struct { |
10 | | - width int |
11 | | - height int |
| 19 | + ctx *Context |
| 20 | + width int |
| 21 | + height int |
| 22 | + Views map[ViewName]views.ViewInterface |
| 23 | + focusedView ViewName |
12 | 24 | } |
13 | 25 |
|
14 | 26 | func (a AppModel) Init() tea.Cmd { |
15 | 27 | return nil |
16 | 28 | } |
17 | 29 |
|
18 | 30 | func (a AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 31 | + var cmd tea.Cmd |
| 32 | + var cmds []tea.Cmd |
19 | 33 | switch msg := msg.(type) { |
20 | 34 | case tea.WindowSizeMsg: |
21 | 35 | a.height = msg.Height |
22 | 36 | a.width = msg.Width |
| 37 | + a.Views[a.focusedView], cmd = a.Views[a.focusedView].Update(tea.WindowSizeMsg{Height: a.AvailableHeight(), Width: msg.Width}) |
| 38 | + cmds = append(cmds, cmd) |
23 | 39 | case tea.KeyMsg: |
24 | 40 | switch msg.String() { |
25 | 41 | case "ctrl+c": |
26 | 42 | return a, tea.Quit |
27 | 43 | } |
28 | 44 | } |
29 | | - return a, nil |
| 45 | + return a, tea.Batch(cmds...) |
30 | 46 | } |
31 | 47 |
|
32 | 48 | func (a AppModel) View() string { |
33 | 49 | footer := a.Footer() |
34 | 50 | header := a.Header() |
35 | | - availableHeight := a.height - lipgloss.Height(header) - lipgloss.Height(footer) |
36 | | - view := lipgloss.NewStyle().Height(availableHeight).Width(a.width).Align(lipgloss.Center, lipgloss.Center).Render("Hello world!") |
| 51 | + view := a.Views[a.focusedView].View() |
37 | 52 | return lipgloss.JoinVertical(lipgloss.Top, header, view, footer) |
38 | 53 | } |
39 | 54 |
|
| 55 | +func (a *AppModel) AvailableHeight() int { |
| 56 | + footer := a.Footer() |
| 57 | + header := a.Header() |
| 58 | + return a.height - lipgloss.Height(header) - lipgloss.Height(footer) |
| 59 | +} |
| 60 | + |
40 | 61 | func (a AppModel) Header() string { |
41 | | - return "Hello World" |
| 62 | + var b strings.Builder |
| 63 | + |
| 64 | + for key, value := range a.Views { |
| 65 | + if key == a.focusedView { |
| 66 | + b.WriteString(styles.TabHeadingActive.Render(value.Name())) |
| 67 | + } else { |
| 68 | + b.WriteString(styles.TabHeadingInactive.Render(value.Name())) |
| 69 | + } |
| 70 | + } |
| 71 | + b.WriteString(styles.TabHeadingInactive.Render("")) |
| 72 | + return b.String() |
42 | 73 | } |
43 | 74 |
|
44 | 75 | func (a AppModel) Footer() string { |
45 | 76 | name := styles.GradientText("REQ", styles.FooterNameFGFrom, styles.FooterNameFGTo, styles.FooterNameStyle, styles.FooterNameBGStyle) |
46 | | - version := styles.FooterVersionStyle.Width(a.width - lipgloss.Width(name)).Render("v0.1.0-alpha2") |
47 | | - return lipgloss.JoinHorizontal(lipgloss.Left, name, version) |
| 77 | + footerText := styles.FooterSegmentStyle.Render(a.Views[a.focusedView].GetFooterSegment()) |
| 78 | + version := styles.FooterVersionStyle.Width(a.width - lipgloss.Width(name) - lipgloss.Width(footerText)).Render("v0.1.0-alpha2") |
| 79 | + return lipgloss.JoinHorizontal(lipgloss.Left, name, footerText, version) |
48 | 80 | } |
49 | 81 |
|
50 | | -func NewAppModel() AppModel { |
51 | | - return AppModel{} |
| 82 | +func NewAppModel(ctx *Context) AppModel { |
| 83 | + model := AppModel{ |
| 84 | + focusedView: Collections, |
| 85 | + ctx: ctx, |
| 86 | + } |
| 87 | + model.Views = map[ViewName]views.ViewInterface{ |
| 88 | + Collections: views.NewCollectionsView(model.ctx.Collections), |
| 89 | + } |
| 90 | + return model |
52 | 91 | } |
0 commit comments