Created
March 26, 2023 11:19
-
-
Save mikejk8s/539849f4f19ef0d1a85595ecd03354f1 to your computer and use it in GitHub Desktop.
bubbletea1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type View struct { | |
Title string | |
Width int | |
Height int | |
Content string | |
} | |
type model struct { | |
currentView int | |
views []View | |
} | |
type upMsg struct{} | |
type downMsg struct{} | |
type leftMsg struct{} | |
type rightMsg struct{} | |
func main() { | |
program := bubbletea.Program{ | |
Init: initModel, | |
Update: update, | |
View: view, | |
Inputs: make(chan bubbletea.Msg), | |
Tick: time.Tick(time.Second / 60), | |
Shutdown: func() {}, | |
} | |
if err := program.Start(); err != nil { | |
panic(err) | |
} | |
} | |
func initModel() bubbletea.Model { | |
views := []View{ | |
{ | |
Title: "View 1", | |
Width: 30, | |
Height: 10, | |
Content: "This is the first view.", | |
}, | |
{ | |
Title: "View 2", | |
Width: 20, | |
Height: 5, | |
Content: "This is the second view.", | |
}, | |
{ | |
Title: "View 3", | |
Width: 40, | |
Height: 15, | |
Content: "This is the third view.", | |
}, | |
} | |
return model{ | |
currentView: 0, | |
views: views, | |
} | |
} | |
func (m model) Init() tea.Cmd { | |
return nil | |
} | |
func update(m bubbletea.Model, msg bubbletea.Msg) (bubbletea.Model, bubbletea.Cmd) { | |
switch msg.(type) { | |
case bubbletea.KeyMsg: | |
key := msg.(bubbletea.KeyMsg).Type | |
switch key { | |
case bubbletea.KeyArrowLeft: | |
return model(m).(model), leftMsg{} | |
case bubbletea.KeyArrowRight: | |
return model(m).(model), rightMsg{} | |
case bubbletea.KeyArrowUp: | |
return model(m).(model), upMsg{} | |
case bubbletea.KeyArrowDown: | |
return model(m).(model), downMsg{} | |
} | |
case leftMsg: | |
m.(model).currentView-- | |
if m.(model).currentView < 0 { | |
m.(model).currentView = len(m.(model).views) - 1 | |
} | |
case rightMsg: | |
m.(model).currentView++ | |
if m.(model).currentView >= len(m.(model).views) { | |
m.(model).currentView = 0 | |
} | |
case upMsg: | |
m.(model).currentView -= 2 | |
if m.(model).currentView < 0 { | |
m.(model).currentView += len(m.(model).views) | |
} | |
case downMsg: | |
m.(model).currentView += 2 | |
if m.(model).currentView >= len(m.(model).views) { | |
m.(model).currentView -= len(m.(model).views) | |
} | |
} | |
return m, nil | |
} | |
func view(m bubbletea.Model) string { | |
switch m.(type) { | |
case model: | |
model := m.(model) | |
view := model.views[model.currentView] | |
lines := strings.Split(view.Content, "\n") | |
clear := exec.Command("clear") | |
clear.Stdout = os.Stdout | |
clear.Run() | |
title := fmt.Sprintf("%s %s", view.Title, strings.Repeat("-", view.Width-len(view.Title))) | |
border := fmt.Sprintf("+%s+", strings.Repeat("-", view.Width)) | |
output := []string{title, border} | |
for i := 0; i < view.Height; i++ { | |
if i < len(lines) { | |
output = append(output, fmt.Sprintf("|%-"+strconv.Itoa(view.Width)+"s|", lines[i])) | |
} else { | |
output = append(output, fmt.Sprintf("|%-"+strconv.Itoa(view.Width)+"s|", "")) | |
} | |
} | |
output = append(output, border) | |
return strings.Join(output, "\n") | |
default: | |
return "" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment