Files

209 lines
5.3 KiB
Go

package screens
import (
"fmt"
"io"
"net/http"
"strings"
"github.com/Khan/genqlient/graphql"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"git.ohea.xyz/cursorius/tui/settings"
"git.ohea.xyz/cursorius/tui/widget"
)
func ServerLogin(s settings.CursoriusServer) tea.Msg {
dashboard, err := createDashboard(s)
if err != nil {
return err
}
return ScreenSwitchMsg{
NewScreen: dashboard,
}
}
func tabBorderWithBottom(left, middle, right string) lipgloss.Border {
border := lipgloss.RoundedBorder()
border.BottomLeft = left
border.Bottom = middle
border.BottomRight = right
return border
}
var (
inactiveTabBorder = tabBorderWithBottom("┴", "─", "┴")
activeTabBorder = tabBorderWithBottom("┘", " ", "└")
docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)
highlightColor = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}
inactiveTabStyle = lipgloss.NewStyle().Border(inactiveTabBorder, true).BorderForeground(highlightColor).Padding(0, 1)
activeTabStyle = inactiveTabStyle.Copy().Border(activeTabBorder, true)
windowStyle = lipgloss.NewStyle().BorderForeground(highlightColor).Padding(2, 0).Align(lipgloss.Center).Border(lipgloss.NormalBorder()).UnsetBorderTop()
)
type Dashboard struct {
Tabs []string
TabContent []tea.Model
activeTab int
width int
height int
client graphql.Client
}
func (m Dashboard) Init() tea.Cmd {
return nil
}
func (m Dashboard) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
for i := 0; i < len(m.TabContent); i++ {
m.TabContent[i].Update(msg)
// m.TabContent[i].SetWidth(m.width - 10)
// m.TabContent[i].SetHeight(m.height - 10)
}
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "tab":
m.activeTab = min(m.activeTab+1, len(m.Tabs)-1)
return m, nil
case "shift+tab":
m.activeTab = max(m.activeTab-1, 0)
return m, nil
}
}
var cmd tea.Cmd
m.TabContent[m.activeTab], cmd = m.TabContent[m.activeTab].Update(msg)
return m, cmd
}
func (m Dashboard) View() string {
doc := strings.Builder{}
var renderedTabs []string
for i, t := range m.Tabs {
var style lipgloss.Style
isFirst, isLast, isActive := i == 0, i == len(m.Tabs)-1, i == m.activeTab
if isActive {
style = activeTabStyle.Copy()
} else {
style = inactiveTabStyle.Copy()
}
border, _, _, _, _ := style.GetBorder()
if isFirst && isActive {
border.BottomLeft = "│"
} else if isFirst && !isActive {
border.BottomLeft = "├"
} else if isLast && isActive {
border.BottomRight = "└"
} else if isLast && !isActive {
border.BottomRight = "┴"
}
style = style.Border(border)
renderedTabs = append(renderedTabs, style.Render(t))
}
row := lipgloss.JoinHorizontal(lipgloss.Top, renderedTabs...)
doc.WriteString(row)
rows := strings.Split(row, "\n")
// not sure why this is -4 ... :shrug:
x := m.width - lipgloss.Width(rows[2]) - 1 - 4
extraStyle := lipgloss.NewStyle().Foreground(highlightColor)
for i := 0; i < x; i++ {
doc.WriteString(extraStyle.Render("─"))
}
doc.WriteString(extraStyle.Render("┐"))
doc.WriteString("\n")
doc.WriteString(windowStyle.Width((m.width - 4 - windowStyle.GetHorizontalFrameSize())).Height(m.height - 6).Render(m.TabContent[m.activeTab].View()))
return docStyle.Render(doc.String())
}
type dashboardItem string
func (i dashboardItem) FilterValue() string { return "" }
type dashboardItemDelegate struct{}
func (d dashboardItemDelegate) Height() int { return 1 }
func (d dashboardItemDelegate) Spacing() int { return 0 }
func (d dashboardItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
func (d dashboardItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
var str string
switch i := listItem.(type) {
case dashboardItem:
str = string(i)
}
fn := itemStyle.Render
if index == m.Index() {
fn = func(s string) string {
return selectedItemStyle.Render("> " + s)
}
}
fmt.Fprint(w, fn(str))
}
func createDashboard(s settings.CursoriusServer) (Dashboard, error) {
client := graphql.NewClient(s.Url, http.DefaultClient)
tabs := []string{"Pipelines", "Secrets", "Clone Credentials", "Runners"}
pipelineWidget, err := widget.CreatePipelineWidget(client)
if err != nil {
return Dashboard{}, fmt.Errorf("Could not create Pipelines tab: %w", err)
}
//secretWidget, err := widget.CreateSecretWidget(client)
//if err != nil {
// return Dashboard{}, fmt.Errorf("Could not create Secrets tab: %w", err)
//}
//cloneCredentialWidget, err := widget.CreateCloneCredentialWidget(client)
//if err != nil {
// return Dashboard{}, fmt.Errorf("Could not create Clone Credentials tab: %w", err)
//}
//runnerWidget, err := widget.CreateRunnerWidget(client)
//if err != nil {
// return Dashboard{}, fmt.Errorf("Could not create Runner tab: %w", err)
//}
tabContent := []tea.Model{
pipelineWidget,
//secretWidget,
//cloneCredentialWidget,
//runnerWidget,
}
return Dashboard{
Tabs: tabs,
TabContent: tabContent,
width: 50,
height: 50,
client: client,
}, nil
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}