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 13 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
8 changes: 8 additions & 0 deletions pkg/views/util/text_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ func GetTerminalWidth() int {
}
return width
}
func GetTerminalHeight() int {
idagelic marked this conversation as resolved.
Show resolved Hide resolved
_, height, err := term.GetSize(int(os.Stdout.Fd()))
const maxHeight = 50
if err != nil {
return maxHeight
}
return height
}
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)

type ProjectConfigurationData struct {
BuildChoice string
Expand Down
87 changes: 71 additions & 16 deletions pkg/views/workspace/create/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"errors"
"fmt"
"log"
"math"
"slices"
"strings"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
Expand All @@ -35,11 +38,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 +92,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,6 +154,20 @@ 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, terminalHeight int) (width, height int) {
lines := strings.Split(content, "\n")
longestLine := slices.MaxFunc(lines, func(a, b string) int {
return len(a) - len(b)
})
width = len(longestLine)

maxHeight := terminalHeight

height = int(math.Min(float64(len(lines)), float64(maxHeight)))

return width, height
}

func NewSummaryModel(config SubmissionFormConfig) SummaryModel {
m := SummaryModel{width: maxWidth}
m.lg = lipgloss.DefaultRenderer()
Expand Down Expand Up @@ -192,6 +203,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, views_util.GetTerminalHeight())
m.viewport = viewport.New(m.width, m.height)
m.viewport.SetContent(content)

return m
}

Expand All @@ -212,7 +230,15 @@ 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
}
case tea.WindowSizeMsg:
m.viewport.Height = max(1, min(m.height, msg.Height-15))
m.viewport.Width = max(20, min(maxWidth, min(m.width, msg.Width-15)))

}

var cmds []tea.Cmd
Expand All @@ -238,15 +264,44 @@ func (m SummaryModel) View() string {
return ""
}

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

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 {
content = renderSummaryView(m)
} else {
content = m.form.WithHeight(5).View()
}

return view
return content + "\n" + helpLine
}

func renderSummaryView(m SummaryModel) string {
summary, err := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel)
if err != nil {
log.Fatal(err)
}
m.viewport.SetContent(summary)

return renderTitle(m) + lipgloss.JoinVertical(lipgloss.Top, renderBody(m), renderFooter(m)) + m.form.WithHeight(5).View()
}

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

func renderBody(m SummaryModel) string {
return m.viewport.Style.
Margin(1, 0, 0).
Padding(1, 2).
BorderForeground(views.LightGray).
Border(lipgloss.RoundedBorder()).
Render(m.viewport.View())
}

func renderFooter(m SummaryModel) string {
return helpStyle.Align(lipgloss.Right).Width(m.viewport.Width + 4).Render("↑ up • ↓ down")
}
Loading