From b56e949bb966ca11936deff76e636d05122247be Mon Sep 17 00:00:00 2001 From: bryans-go Date: Wed, 23 Oct 2024 00:11:42 +0530 Subject: [PATCH 01/23] fix: add viewport for showing summary Signed-off-by: bryans-go --- pkg/views/workspace/create/configuration.go | 2 +- pkg/views/workspace/create/summary.go | 35 ++++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/pkg/views/workspace/create/configuration.go b/pkg/views/workspace/create/configuration.go index 2a09af0304..5d0f6a9882 100644 --- a/pkg/views/workspace/create/configuration.go +++ b/pkg/views/workspace/create/configuration.go @@ -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 diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index d265318117..98d050d95d 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -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" @@ -40,6 +41,7 @@ type SummaryModel struct { projectList []apiclient.CreateProjectDTO defaults *views_util.ProjectConfigDefaults nameLabel string + viewport viewport.Model } type SubmissionFormConfig struct { @@ -87,14 +89,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)) @@ -192,6 +186,11 @@ func NewSummaryModel(config SubmissionFormConfig) SummaryModel { ), ).WithShowHelp(false).WithTheme(views.GetCustomTheme()) + content, _ := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel) + + m.viewport = viewport.New(80, 13) + m.viewport.SetContent(content) + return m } @@ -212,6 +211,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 } } @@ -238,15 +241,23 @@ 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 { + if len(m.projectList) > 1 || (len(m.projectList) == 1 && ProjectsConfigurationChanged) { + helpLine += "\n" + "↑ up • ↓ down" summary, err := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel) if err != nil { log.Fatal(err) } - view = views.GetBorderedMessage(summary) + "\n" + view + var title string + if m.name == "" { + title = views.GetStyledMainTitle("SUMMARY") + } else { + title = views.GetStyledMainTitle(fmt.Sprintf("SUMMARY - %s %s", m.nameLabel, m.name)) + } + m.viewport.SetContent(summary) + return title + views.GetBorderedMessage(m.viewport.View()) + "\n" + m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) } - return view + return m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) } From af683e58cec9801e9569c6721e74e37c2894c27d Mon Sep 17 00:00:00 2001 From: bryans-go Date: Wed, 23 Oct 2024 10:52:12 +0530 Subject: [PATCH 02/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 51 +++++++++++++++++++-------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 98d050d95d..6ae73c3e11 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -241,23 +241,46 @@ func (m SummaryModel) View() string { return "" } + helpLine := buildHelpLine(m) + + if shouldDisplaySummary(m) { + return renderSummaryView(m, helpLine) + } + + return renderSimpleView(m, helpLine) +} + +func buildHelpLine(m SummaryModel) string { helpLine := "enter: next • f10: advanced configuration" + if len(m.projectList) > 1 || ProjectsConfigurationChanged { + helpLine += "\n↑ up • ↓ down" + } + return helpLine +} - if len(m.projectList) > 1 || (len(m.projectList) == 1 && ProjectsConfigurationChanged) { - helpLine += "\n" + "↑ up • ↓ down" - summary, err := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel) - if err != nil { - log.Fatal(err) - } - var title string - if m.name == "" { - title = views.GetStyledMainTitle("SUMMARY") - } else { - title = views.GetStyledMainTitle(fmt.Sprintf("SUMMARY - %s %s", m.nameLabel, m.name)) - } - m.viewport.SetContent(summary) - return title + views.GetBorderedMessage(m.viewport.View()) + "\n" + m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) +func shouldDisplaySummary(m SummaryModel) bool { + return len(m.projectList) > 1 || ProjectsConfigurationChanged +} + +func renderSummaryView(m SummaryModel, helpLine string) 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) + + return title + + views.GetBorderedMessage(m.viewport.View()) + "\n" + + m.form.WithHeight(5).View() + "\n" + + HelpStyle.Render(helpLine) +} + +func renderSimpleView(m SummaryModel, helpLine string) string { return m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) } From 51c2a51e5eaca964a832a55d548cfdd62117384e Mon Sep 17 00:00:00 2001 From: bryans-go Date: Wed, 23 Oct 2024 11:20:44 +0530 Subject: [PATCH 03/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 6ae73c3e11..0fe1bdf112 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -241,25 +241,14 @@ func (m SummaryModel) View() string { return "" } - helpLine := buildHelpLine(m) - - if shouldDisplaySummary(m) { - return renderSummaryView(m, helpLine) - } - - return renderSimpleView(m, helpLine) -} - -func buildHelpLine(m SummaryModel) string { helpLine := "enter: next • f10: advanced configuration" + if len(m.projectList) > 1 || ProjectsConfigurationChanged { helpLine += "\n↑ up • ↓ down" + return renderSummaryView(m, helpLine) } - return helpLine -} -func shouldDisplaySummary(m SummaryModel) bool { - return len(m.projectList) > 1 || ProjectsConfigurationChanged + return m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) } func renderSummaryView(m SummaryModel, helpLine string) string { @@ -280,7 +269,3 @@ func renderSummaryView(m SummaryModel, helpLine string) string { m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) } - -func renderSimpleView(m SummaryModel, helpLine string) string { - return m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) -} From 02224db57e70c43141fb331faedd819afc383ef8 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Wed, 23 Oct 2024 11:34:19 +0530 Subject: [PATCH 04/23] calculate viewport size Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 0fe1bdf112..1b10642ab9 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -36,6 +36,7 @@ type SummaryModel struct { styles *Styles form *huh.Form width int + height int quitting bool name string projectList []apiclient.CreateProjectDTO @@ -151,8 +152,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) (width, height int) { + lines := strings.Split(content, "\n") + maxWidth := 0 + + for _, line := range lines { + if len(line) > maxWidth { + maxWidth = len(line) + } + } + return maxWidth, len(lines) +} + func NewSummaryModel(config SubmissionFormConfig) SummaryModel { - m := SummaryModel{width: maxWidth} + m := SummaryModel{} m.lg = lipgloss.DefaultRenderer() m.styles = NewStyles(m.lg) m.name = *config.ChosenName @@ -188,7 +201,9 @@ func NewSummaryModel(config SubmissionFormConfig) SummaryModel { content, _ := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel) - m.viewport = viewport.New(80, 13) + // Dynamically calculate viewport size + m.width, m.height = calculateViewportSize(content) + m.viewport = viewport.New(m.width, m.height) m.viewport.SetContent(content) return m From bab04528440b0684a7bfcd82f17d825d673ad453 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Wed, 23 Oct 2024 11:53:14 +0530 Subject: [PATCH 05/23] add viewport help Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 1b10642ab9..75f120c884 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -259,14 +259,13 @@ func (m SummaryModel) View() string { helpLine := "enter: next • f10: advanced configuration" if len(m.projectList) > 1 || ProjectsConfigurationChanged { - helpLine += "\n↑ up • ↓ down" - return renderSummaryView(m, helpLine) + return renderSummaryView(m) + "\n" + HelpStyle.Render(helpLine) } return m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) } -func renderSummaryView(m SummaryModel, helpLine string) string { +func renderSummaryView(m SummaryModel) string { title := views.GetStyledMainTitle("SUMMARY") if m.name != "" { title = views.GetStyledMainTitle(fmt.Sprintf("SUMMARY - %s %s", m.nameLabel, m.name)) @@ -280,7 +279,6 @@ func renderSummaryView(m SummaryModel, helpLine string) string { m.viewport.SetContent(summary) return title + - views.GetBorderedMessage(m.viewport.View()) + "\n" + - m.form.WithHeight(5).View() + "\n" + - HelpStyle.Render(helpLine) + views.GetBorderedMessage(m.viewport.View()) + HelpStyle.Render(" ↑ up • ↓ down") + "\n" + + m.form.WithHeight(5).View() } From 4634ef026921a673cfb35001dbeadf28f4bf35d8 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Wed, 23 Oct 2024 12:09:51 +0530 Subject: [PATCH 06/23] add viewport help Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 75f120c884..3c9bbbbdf1 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -161,7 +161,13 @@ func calculateViewportSize(content string) (width, height int) { maxWidth = len(line) } } - return maxWidth, len(lines) + + height = len(lines) + if height > 25 { + height = 25 + } + + return maxWidth, height } func NewSummaryModel(config SubmissionFormConfig) SummaryModel { @@ -275,10 +281,13 @@ func renderSummaryView(m SummaryModel) string { 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()) + HelpStyle.Render(" ↑ up • ↓ down") + "\n" + + views.GetBorderedMessage(m.viewport.View()) + "\n" + m.form.WithHeight(5).View() } From 09c053aaa744ae2b58dfa2411a5d86cbe8ea0cf4 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Wed, 23 Oct 2024 21:48:08 +0530 Subject: [PATCH 07/23] dynamic height-widthof viewport Signed-off-by: bryans-go --- pkg/views/util/text_wrapper.go | 8 +++++++ pkg/views/workspace/create/summary.go | 34 ++++++++++++--------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pkg/views/util/text_wrapper.go b/pkg/views/util/text_wrapper.go index 6ecefa6443..1f6e7a8060 100644 --- a/pkg/views/util/text_wrapper.go +++ b/pkg/views/util/text_wrapper.go @@ -48,3 +48,11 @@ func GetTerminalWidth() int { } return width } +func GetTerminalHeight() int { + _, height, err := term.GetSize(int(os.Stdout.Fd())) + const maxHeight = 50 + if err != nil { + return maxHeight + } + return height +} diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 3c9bbbbdf1..15782f367f 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -7,6 +7,8 @@ import ( "errors" "fmt" "log" + "math" + "slices" "strings" "github.com/charmbracelet/bubbles/viewport" @@ -152,22 +154,17 @@ 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) { +func calculateViewportSize(content string, terminalHeight int) (width, height int) { lines := strings.Split(content, "\n") - maxWidth := 0 + maxWidth := slices.MaxFunc(lines, func(a, b string) int { + return len(a) - len(b) + }) - for _, line := range lines { - if len(line) > maxWidth { - maxWidth = len(line) - } - } + maxHeight := terminalHeight - height = len(lines) - if height > 25 { - height = 25 - } + height = int(math.Min(float64(len(lines)), float64(maxHeight))) - return maxWidth, height + return len(maxWidth), height } func NewSummaryModel(config SubmissionFormConfig) SummaryModel { @@ -208,7 +205,7 @@ func NewSummaryModel(config SubmissionFormConfig) SummaryModel { content, _ := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel) // Dynamically calculate viewport size - m.width, m.height = calculateViewportSize(content) + m.width, m.height = calculateViewportSize(content, views_util.GetTerminalHeight()) m.viewport = viewport.New(m.width, m.height) m.viewport.SetContent(content) @@ -237,6 +234,10 @@ func (m SummaryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { 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(m.width, msg.Width-50)) + } var cmds []tea.Cmd @@ -282,12 +283,7 @@ func renderSummaryView(m SummaryModel) string { 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" + + views.GetBorderedMessage(m.viewport.View()) + HelpStyle.Render("↑ up • ↓ down") + m.form.WithHeight(5).View() } From fa9548d7f3f996e1f9517475623efa4aa9618bc0 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 10:53:39 +0530 Subject: [PATCH 08/23] fix Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 15782f367f..c63c54daa2 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -236,7 +236,7 @@ func (m SummaryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case tea.WindowSizeMsg: m.viewport.Height = max(1, min(m.height, msg.Height-15)) - m.viewport.Width = max(20, min(m.width, msg.Width-50)) + m.viewport.Width = max(20, min(maxWidth, min(m.width, msg.Width-15))) } From e71b9e04245881db9a832342ff3927db0d759cfb Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 12:49:31 +0530 Subject: [PATCH 09/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 39 +++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index c63c54daa2..cd3ac0dd8f 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -263,27 +263,44 @@ func (m SummaryModel) View() string { return "" } - helpLine := "enter: next • f10: advanced configuration" + helpLine := HelpStyle.Render("enter: next • f10: advanced configuration") + var content string if len(m.projectList) > 1 || ProjectsConfigurationChanged { - return renderSummaryView(m) + "\n" + HelpStyle.Render(helpLine) + content = renderSummaryView(m) + } else { + content = m.form.WithHeight(5).View() } - return m.form.WithHeight(5).View() + "\n" + HelpStyle.Render(helpLine) + return content + "\n" + 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) - return title + - views.GetBorderedMessage(m.viewport.View()) + HelpStyle.Render("↑ up • ↓ down") + - m.form.WithHeight(5).View() + + 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") } From 7e907a3f85e6a51fff7d1b56059fe1eb2bbaa719 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 12:52:17 +0530 Subject: [PATCH 10/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index cd3ac0dd8f..76818ec98e 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -168,7 +168,7 @@ func calculateViewportSize(content string, terminalHeight int) (width, height in } func NewSummaryModel(config SubmissionFormConfig) SummaryModel { - m := SummaryModel{} + m := SummaryModel{width: maxWidth} m.lg = lipgloss.DefaultRenderer() m.styles = NewStyles(m.lg) m.name = *config.ChosenName From f03f7c7b0f3a27ceb6f2844e1d9240b197ce33dd Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 12:55:37 +0530 Subject: [PATCH 11/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 76818ec98e..9f6cf455ae 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -156,15 +156,16 @@ func projectDetailOutput(projectDetailKey ProjectDetail, projectDetailValue stri func calculateViewportSize(content string, terminalHeight int) (width, height int) { lines := strings.Split(content, "\n") - maxWidth := slices.MaxFunc(lines, func(a, b string) int { + 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 len(maxWidth), height + return width, height } func NewSummaryModel(config SubmissionFormConfig) SummaryModel { From e570217d4c846cc1b6ecc99f45066a8305cae487 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 13:19:20 +0530 Subject: [PATCH 12/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/configuration.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/views/workspace/create/configuration.go b/pkg/views/workspace/create/configuration.go index 5d0f6a9882..6fb73170db 100644 --- a/pkg/views/workspace/create/configuration.go +++ b/pkg/views/workspace/create/configuration.go @@ -23,7 +23,7 @@ const ( DEVCONTAINER_FILEPATH = ".devcontainer/devcontainer.json" ) -var HelpStyle = lipgloss.NewStyle().Foreground(views.Gray) +var helpStyle = lipgloss.NewStyle().Foreground(views.Gray) type ProjectConfigurationData struct { BuildChoice string From 0372184449f81294a9d8a6871592b4f20776d259 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 13:20:08 +0530 Subject: [PATCH 13/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 9f6cf455ae..d10d6d6b31 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -264,7 +264,7 @@ func (m SummaryModel) View() string { return "" } - helpLine := HelpStyle.Render("enter: next • f10: advanced configuration") + helpLine := helpStyle.Render("enter: next • f10: advanced configuration") var content string if len(m.projectList) > 1 || ProjectsConfigurationChanged { @@ -303,5 +303,5 @@ func renderBody(m SummaryModel) string { } func renderFooter(m SummaryModel) string { - return HelpStyle.Align(lipgloss.Right).Width(m.viewport.Width + 4).Render("↑ up • ↓ down") + return helpStyle.Align(lipgloss.Right).Width(m.viewport.Width + 4).Render("↑ up • ↓ down") } From 2fd32116d30988f4d144e3ff518f4ad2a7b48f9a Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 17:07:40 +0530 Subject: [PATCH 14/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index d10d6d6b31..124c1fc5e7 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -236,7 +236,7 @@ func (m SummaryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.viewport.LineDown(1) // Scroll down } case tea.WindowSizeMsg: - m.viewport.Height = max(1, min(m.height, msg.Height-15)) + m.viewport.Height = max(1, min(25, min(m.height, msg.Height-15))) m.viewport.Width = max(20, min(maxWidth, min(m.width, msg.Width-15))) } From f91bdb78ba6cba2d9ebf6143cfc7e2ce69da96a8 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 17:34:08 +0530 Subject: [PATCH 15/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 124c1fc5e7..6009943ce2 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -236,7 +236,7 @@ func (m SummaryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.viewport.LineDown(1) // Scroll down } case tea.WindowSizeMsg: - m.viewport.Height = max(1, min(25, min(m.height, msg.Height-15))) + m.viewport.Height = max(1, min(msg.Height-19, m.height)) m.viewport.Width = max(20, min(maxWidth, min(m.width, msg.Width-15))) } From 74a4b76f4c34f68403487f696918d3c71bb72238 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Thu, 24 Oct 2024 18:20:42 +0530 Subject: [PATCH 16/23] refactor code Signed-off-by: bryans-go --- pkg/views/workspace/create/summary.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 6009943ce2..d10d6d6b31 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -236,7 +236,7 @@ func (m SummaryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.viewport.LineDown(1) // Scroll down } case tea.WindowSizeMsg: - m.viewport.Height = max(1, min(msg.Height-19, m.height)) + m.viewport.Height = max(1, min(m.height, msg.Height-15)) m.viewport.Width = max(20, min(maxWidth, min(m.width, msg.Width-15))) } From cb6f5a9b3b26e4de2aa85481c8093ecf7c605b4c Mon Sep 17 00:00:00 2001 From: Divanshu Grover Date: Fri, 25 Oct 2024 18:05:57 +0530 Subject: [PATCH 17/23] move title into summary content Signed-off-by: Divanshu Grover --- pkg/views/workspace/create/summary.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index d10d6d6b31..8d085e4725 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -92,6 +92,14 @@ 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)) @@ -283,14 +291,7 @@ func renderSummaryView(m SummaryModel) string { } 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") + return lipgloss.JoinVertical(lipgloss.Top, renderBody(m), renderFooter(m)) + m.form.WithHeight(5).View() } func renderBody(m SummaryModel) string { From 95138a0094c1df55ae90a80c00640a0a5a13ada1 Mon Sep 17 00:00:00 2001 From: Divanshu Grover Date: Fri, 25 Oct 2024 20:05:03 +0530 Subject: [PATCH 18/23] remove height func & perform direct call Signed-off-by: Divanshu Grover --- pkg/views/util/text_wrapper.go | 8 -------- pkg/views/workspace/create/summary.go | 11 ++++++++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/views/util/text_wrapper.go b/pkg/views/util/text_wrapper.go index 1f6e7a8060..6ecefa6443 100644 --- a/pkg/views/util/text_wrapper.go +++ b/pkg/views/util/text_wrapper.go @@ -48,11 +48,3 @@ func GetTerminalWidth() int { } return width } -func GetTerminalHeight() int { - _, height, err := term.GetSize(int(os.Stdout.Fd())) - const maxHeight = 50 - if err != nil { - return maxHeight - } - return height -} diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 8d085e4725..347ba4c307 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "math" + "os" "slices" "strings" @@ -19,6 +20,7 @@ import ( "github.com/daytonaio/daytona/pkg/apiclient" "github.com/daytonaio/daytona/pkg/views" views_util "github.com/daytonaio/daytona/pkg/views/util" + "golang.org/x/term" ) type ProjectDetail string @@ -162,14 +164,17 @@ 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) { +func calculateViewportSize(content string) (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 + _, maxHeight, err := term.GetSize(int(os.Stdout.Fd())) + if err != nil { + maxHeight = 25 + } height = int(math.Min(float64(len(lines)), float64(maxHeight))) @@ -214,7 +219,7 @@ func NewSummaryModel(config SubmissionFormConfig) SummaryModel { content, _ := RenderSummary(m.name, m.projectList, m.defaults, m.nameLabel) // Dynamically calculate viewport size - m.width, m.height = calculateViewportSize(content, views_util.GetTerminalHeight()) + m.width, m.height = calculateViewportSize(content) m.viewport = viewport.New(m.width, m.height) m.viewport.SetContent(content) From 15a4f098009d5f7278320c5dee614e2ed5d04b06 Mon Sep 17 00:00:00 2001 From: Divanshu Grover Date: Fri, 25 Oct 2024 20:42:48 +0530 Subject: [PATCH 19/23] fix: weirdness in sorting of env vars Signed-off-by: Divanshu Grover --- pkg/views/workspace/create/summary.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 347ba4c307..efa69bc696 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -10,6 +10,7 @@ import ( "math" "os" "slices" + "sort" "strings" "github.com/charmbracelet/bubbles/viewport" @@ -150,11 +151,23 @@ func renderProjectDetails(project apiclient.CreateProjectDTO, buildChoice views_ output += "\n" } - var envVars string - for key, val := range project.EnvVars { - envVars += fmt.Sprintf("%s=%s; ", key, val) + keys := make([]string, 0, len(project.EnvVars)) + for key := range project.EnvVars { + keys = append(keys, key) } - output += projectDetailOutput(EnvVars, strings.TrimSuffix(envVars, "; ")) + sort.Strings(keys) + + var envVarsBuilder strings.Builder + for _, key := range keys { + envVarsBuilder.WriteString(key + "=" + project.EnvVars[key] + "; ") + } + + envVars := envVarsBuilder.String() + if len(envVars) > 2 { + envVars = envVars[:len(envVars)-2] + } + + output += projectDetailOutput("EnvVars", envVars) } return output From d3beb5a9a30aa92912c88a910a49fa1641f8e1ea Mon Sep 17 00:00:00 2001 From: Divanshu Grover Date: Wed, 30 Oct 2024 11:20:30 +0530 Subject: [PATCH 20/23] fix: arrangment of env vars Signed-off-by: Divanshu Grover --- pkg/views/workspace/create/summary.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index efa69bc696..19912f5bd8 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -174,7 +174,26 @@ func renderProjectDetails(project apiclient.CreateProjectDTO, buildChoice views_ } func projectDetailOutput(projectDetailKey ProjectDetail, projectDetailValue string) string { - return fmt.Sprintf("\t%s%-*s%s", lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)), DEFAULT_PADDING-len(string(projectDetailKey)), EMPTY_STRING, projectDetailValue) + // Special handling for environment variables + if projectDetailKey == EnvVars { + envVars := strings.Split(projectDetailValue, "; ") + if len(envVars) == 0 { + return "" + } + + output := fmt.Sprintf("\t%s\n", lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey))) + + for _, env := range envVars { + output += fmt.Sprintf("\t\t%s\n", env) + } + + return strings.TrimSuffix(output, "\n") + } + 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) { From dd21919e6d817ac847f374957a1ca3b320ed88f4 Mon Sep 17 00:00:00 2001 From: Divanshu Grover Date: Wed, 30 Oct 2024 15:23:25 +0530 Subject: [PATCH 21/23] fix: arrangment of env vars Signed-off-by: Divanshu Grover --- pkg/views/workspace/create/summary.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 19912f5bd8..88229eb643 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -174,20 +174,15 @@ func renderProjectDetails(project apiclient.CreateProjectDTO, buildChoice views_ } func projectDetailOutput(projectDetailKey ProjectDetail, projectDetailValue string) string { - // Special handling for environment variables if projectDetailKey == EnvVars { envVars := strings.Split(projectDetailValue, "; ") if len(envVars) == 0 { return "" } - - output := fmt.Sprintf("\t%s\n", lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey))) - - for _, env := range envVars { - output += fmt.Sprintf("\t\t%s\n", env) - } - - return strings.TrimSuffix(output, "\n") + output := fmt.Sprintf("\t%s\n\t\t%s", + lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)), + strings.Join(envVars, "\n\t\t")) + return output } return fmt.Sprintf("\t%s%-*s%s", lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)), From 26526630af78a9c6000b246b7a8c290b57d1de75 Mon Sep 17 00:00:00 2001 From: Divanshu Grover Date: Wed, 30 Oct 2024 15:29:12 +0530 Subject: [PATCH 22/23] fix: arrangment of env vars Signed-off-by: Divanshu Grover --- pkg/views/workspace/create/summary.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index 88229eb643..d206f83558 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -176,9 +176,6 @@ func renderProjectDetails(project apiclient.CreateProjectDTO, buildChoice views_ func projectDetailOutput(projectDetailKey ProjectDetail, projectDetailValue string) string { if projectDetailKey == EnvVars { envVars := strings.Split(projectDetailValue, "; ") - if len(envVars) == 0 { - return "" - } output := fmt.Sprintf("\t%s\n\t\t%s", lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)), strings.Join(envVars, "\n\t\t")) From a4f878e636a8078f9e82b40bd6e69759d6435d53 Mon Sep 17 00:00:00 2001 From: Divanshu Grover Date: Wed, 30 Oct 2024 15:38:56 +0530 Subject: [PATCH 23/23] fix: arrangment of env vars Signed-off-by: Divanshu Grover --- pkg/views/workspace/create/summary.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pkg/views/workspace/create/summary.go b/pkg/views/workspace/create/summary.go index d206f83558..ca879a8381 100644 --- a/pkg/views/workspace/create/summary.go +++ b/pkg/views/workspace/create/summary.go @@ -174,15 +174,14 @@ func renderProjectDetails(project apiclient.CreateProjectDTO, buildChoice views_ } func projectDetailOutput(projectDetailKey ProjectDetail, projectDetailValue string) string { + keyStyle := lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)) + if projectDetailKey == EnvVars { - envVars := strings.Split(projectDetailValue, "; ") - output := fmt.Sprintf("\t%s\n\t\t%s", - lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)), - strings.Join(envVars, "\n\t\t")) - return output + projectDetailValue = strings.ReplaceAll(projectDetailValue, "; ", "\n\t\t") + return fmt.Sprintf("\t%s\n\t\t%s", keyStyle, projectDetailValue) } return fmt.Sprintf("\t%s%-*s%s", - lipgloss.NewStyle().Foreground(views.Green).Render(string(projectDetailKey)), + keyStyle, DEFAULT_PADDING-len(string(projectDetailKey)), EMPTY_STRING, projectDetailValue)