// The package defines an extensible TUI via the bubbletea framework. // // TODO enable collection recursing (i.e, embeded collections) // // TODO enable scroll/viewport logic on issues!!! // // While the package remains in v0.0.X releases, this TUI may be undocumented. package issues import ( "fmt" "strings" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" ) // [lipgloss] style definitions, stores the currently displayed "widget" var ( titleStyle = lipgloss.NewStyle(). Bold(true). Underline(true) statusStyle = lipgloss.NewStyle(). Faint(true). Italic(true) variadicTitleStyle = lipgloss.NewStyle(). Align(lipgloss.Left). Italic(true) variadicDataStyle = lipgloss.NewStyle(). Width(40). BorderStyle(lipgloss.ASCIIBorder()) borderStyle = lipgloss.NewStyle(). Padding(1, 2). Margin(1). BorderStyle(lipgloss.NormalBorder()) indexStyle = lipgloss.NewStyle(). Italic(true) pointerStyle = lipgloss.NewStyle(). Faint(true) collectionStyleLeft = lipgloss.NewStyle(). Align(lipgloss.Left) ) // interface for renderable structs type widget interface { view() tea.Msg } // The main bubbletea Model type Model struct { widget widget content string Path string // viewport viewport.Model } // The bubbletea init function func (m Model) Init() tea.Cmd { return m.load } // Handles quit logic and viewport scroll and size updates func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "q": return m, tea.Quit case "j": if collection, ok := m.widget.(IssueCollection); ok { if collection.selection+1 < len(collection.Collection) { collection.selection = collection.selection + 1 } else { collection.selection = 0 } m.widget = collection return m, collection.view } return m, nil case "k": // do something only if widget is collection if collection, ok := m.widget.(IssueCollection); ok { if collection.selection != 0 { collection.selection = collection.selection - 1 } else { collection.selection = len(collection.Collection) - 1 } m.widget = collection return m, collection.view } return m, nil case "enter": if _, ok := m.widget.(IssueCollection); ok { m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path return m, m.load } return m, nil } case widget: switch T := msg.(type) { case Issue: m.widget = T return m, T.view case IssueCollection: m.widget = T return m, T.view } case string: m.content = msg return m, nil } return m, nil } // Handles top level view functionality func (m Model) View() string { var output string if len(m.content) == 0 { return "loading..." } else { output = output + m.content } output = output + "\nj/k: down/up\tenter: select\tq: quit" return output } type issueCreate struct { focusedField int inputs []textinput.Model issue Issue } func (c issueCreate) init(i Issue) issueCreate { c.issue = i title := textinput.Model{Placeholder: "title", CharLimit: 64, Width: 45} status := textinput.Model{Placeholder: "status", CharLimit: 64, Width: 45} tags := textinput.Model{Placeholder: "tags", CharLimit: 64, Width: 45} blockers := textinput.Model{Placeholder: "blockers", CharLimit: 64, Width: 45} c.inputs = append(c.inputs, title, status, tags, blockers) return c } func (c issueCreate) update(msg tea.Msg) (issueCreate, tea.Cmd) { switch T := msg.(type) { case issueCreate: c.issue = T.issue c.focusedField = T.focusedField c.inputs = T.inputs return c, nil case tea.KeyMsg: switch T.String() { case "tab": if c.focusedField < len(c.inputs)-1 { c.focusedField++ } else { c.focusedField = 0 } return c, nil default: inputModel, inputCmd := c.inputs[c.focusedField].Update(msg) c.inputs[c.focusedField] = inputModel return c, inputCmd } } return c, nil } func (c issueCreate) view() tea.Msg { return "testing" } // Handles load logic func (m Model) load() tea.Msg { if IsIssue(m.Path) { issue, err := Issue{}.NewFromPath(m.Path) if err != nil { return nil } return issue } if IsIssueCollection(m.Path) { collection, err := IssueCollection{}.NewFromPath(m.Path) if err != nil { return nil } return collection } return issueCreate{issue: Issue{Path: m.Path}} } // Handles all view logic for Issue structs func (i Issue) view() tea.Msg { var output string // title output = output + titleStyle.Render(i.Title) // status output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data)) // variadics var tags string for _, field := range i.Tags.Fields { tags = tags + field.Path + ", " } tags = strings.TrimRight(tags, ", ") var blockedby string for _, field := range i.Blockedby.Fields { blockedby = blockedby + field.Path + ", " } blockedby = strings.TrimRight(blockedby, ", ") if len(i.Tags.Fields) > 0 { output = output + variadicTitleStyle.Render("\n\nTags:") output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags)) } if len(i.Blockedby.Fields) > 0 { output = output + variadicTitleStyle.Render("\n\nBlockedby:") output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby)) } // description output = output + titleStyle.Render("\n\nDescription:\n") output = output + fmt.Sprintf("\n%s", i.Description.Data) return borderStyle.Render(output) } // Handles all view logic for IssueCollection structs. func (ic IssueCollection) view() tea.Msg { var output string var left string output = output + "Issues in " + ic.Path + "...\n\n" for i, issue := range ic.Collection { // pointer render if i == ic.selection { left = left + pointerStyle.Render("-> ") } else { left = left + pointerStyle.Render(" ") } // index render left = left + "[" + indexStyle.Render(fmt.Sprintf("%d", i+1)) + "]: " // title render left = left + fmt.Sprintf("%s\n", titleStyle.Render(issue.Title)) } output = output + collectionStyleLeft.Render(left) return output }