Initial commit

This commit is contained in:
2023-02-18 02:50:14 -07:00
commit 3f87018dfa
7 changed files with 651 additions and 0 deletions
+181
View File
@@ -0,0 +1,181 @@
package screens
import (
"fmt"
"io"
"strings"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type CursoriusServer struct {
Name string
Url string
Token string
}
func (s CursoriusServer) Login() ScreenSwitchMsg {
dashboard := createDashboard(s)
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 []list.Model
activeTab int
width int
height int
}
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].SetWidth(m.width - 10)
m.TabContent[i].SetHeight(m.height - 10)
}
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl+c", "q":
return m, tea.Quit
case "right", "l", "n", "tab":
m.activeTab = min(m.activeTab+1, len(m.Tabs)-1)
return m, nil
case "left", "h", "p", "shift+tab":
m.activeTab = max(m.activeTab-1, 0)
return m, nil
}
}
return m, nil
}
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 = "│"
border.BottomRight = "└"
} else if isLast && !isActive {
//border.BottomRight = "┤"
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 CursoriusServer) Dashboard {
tabs := []string{"Pipelines", "Secrets", "Clone Credentials", "Runners"}
content := []list.Item{dashboardItem("Pipelines"), dashboardItem("Secrets"), dashboardItem("Clone Credentials"), dashboardItem("Runners")}
tabContent := []list.Model{
list.New(content, dashboardItemDelegate{}, 50, 50),
list.New(content, dashboardItemDelegate{}, 50, 50),
list.New(content, dashboardItemDelegate{}, 50, 50),
}
return Dashboard{
Tabs: tabs,
TabContent: tabContent,
width: 50,
height: 50,
}
}
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
}