Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add viewport for showing summary #1284

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/views/workspace/create/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
DEVCONTAINER_FILEPATH = ".devcontainer/devcontainer.json"
)

var configurationHelpLine = lipgloss.NewStyle().Foreground(views.Gray).Render("enter: next f10: advanced configuration")
var HelpStyle = lipgloss.NewStyle().Foreground(views.Gray)
Tpuljak marked this conversation as resolved.
Show resolved Hide resolved

type ProjectConfigurationData struct {
BuildChoice string
Expand Down
75 changes: 58 additions & 17 deletions pkg/views/workspace/create/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"strings"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
Expand All @@ -35,11 +36,13 @@ type SummaryModel struct {
styles *Styles
form *huh.Form
width int
height int
quitting bool
name string
projectList []apiclient.CreateProjectDTO
defaults *views_util.ProjectConfigDefaults
nameLabel string
viewport viewport.Model
}

type SubmissionFormConfig struct {
Expand Down Expand Up @@ -87,14 +90,6 @@ func RunSubmissionForm(config SubmissionFormConfig) error {

func RenderSummary(name string, projectList []apiclient.CreateProjectDTO, defaults *views_util.ProjectConfigDefaults, nameLabel string) (string, error) {
var output string
if name == "" {
output = views.GetStyledMainTitle("SUMMARY")
} else {
output = views.GetStyledMainTitle(fmt.Sprintf("SUMMARY - %s %s", nameLabel, name))
}

output += "\n\n"

for i := range projectList {
if len(projectList) == 1 {
output += fmt.Sprintf("%s - %s\n", lipgloss.NewStyle().Foreground(views.Green).Render("Project"), (projectList[i].Source.Repository.Url))
Expand Down Expand Up @@ -157,8 +152,26 @@ func projectDetailOutput(projectDetailKey ProjectDetail, projectDetailValue stri
return fmt.Sprintf("\t%s%-*s%s", lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)), DEFAULT_PADDING-len(string(projectDetailKey)), EMPTY_STRING, projectDetailValue)
}

func calculateViewportSize(content string) (width, height int) {
Tpuljak marked this conversation as resolved.
Show resolved Hide resolved
lines := strings.Split(content, "\n")
maxWidth := 0

for _, line := range lines {
Tpuljak marked this conversation as resolved.
Show resolved Hide resolved
if len(line) > maxWidth {
maxWidth = len(line)
}
}

height = len(lines)
Tpuljak marked this conversation as resolved.
Show resolved Hide resolved
if height > 25 {
height = 25
}

return maxWidth, height
}

func NewSummaryModel(config SubmissionFormConfig) SummaryModel {
m := SummaryModel{width: maxWidth}
m := SummaryModel{}
m.lg = lipgloss.DefaultRenderer()
m.styles = NewStyles(m.lg)
m.name = *config.ChosenName
Expand Down Expand Up @@ -192,6 +205,13 @@ func NewSummaryModel(config SubmissionFormConfig) SummaryModel {
),
).WithShowHelp(false).WithTheme(views.GetCustomTheme())

content, _ := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel)

// Dynamically calculate viewport size
m.width, m.height = calculateViewportSize(content)
m.viewport = viewport.New(m.width, m.height)
m.viewport.SetContent(content)

return m
}

Expand All @@ -212,6 +232,10 @@ func (m SummaryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.form.State = huh.StateCompleted
configureCheck = true
return m, tea.Quit
case "up":
m.viewport.LineUp(1) // Scroll up
case "down":
m.viewport.LineDown(1) // Scroll down
}
}

Expand All @@ -238,15 +262,32 @@ func (m SummaryModel) View() string {
return ""
}

view := m.form.WithHeight(5).View() + "\n" + configurationHelpLine
helpLine := "enter: next • f10: advanced configuration"

if len(m.projectList) > 1 || len(m.projectList) == 1 && ProjectsConfigurationChanged {
summary, err := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel)
if err != nil {
log.Fatal(err)
}
view = views.GetBorderedMessage(summary) + "\n" + view
if len(m.projectList) > 1 || ProjectsConfigurationChanged {
return renderSummaryView(m) + "\n" + HelpStyle.Render(helpLine)
}

return view
return m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine)
}

func renderSummaryView(m SummaryModel) string {
title := views.GetStyledMainTitle("SUMMARY")
if m.name != "" {
title = views.GetStyledMainTitle(fmt.Sprintf("SUMMARY - %s %s", m.nameLabel, m.name))
}

summary, err := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel)
if err != nil {
log.Fatal(err)
}
m.viewport.SetContent(summary)
if m.viewport.Height == 25 {
return title +
views.GetBorderedMessage(m.viewport.View()) + HelpStyle.Render(" ↑ up • ↓ down") + "\n" +
m.form.WithHeight(5).View()
}
return title +
views.GetBorderedMessage(m.viewport.View()) + "\n" +
m.form.WithHeight(5).View()
}
Loading