-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve git provider config list view (#1252)
Signed-off-by: Ivan Dagelic <[email protected]>
- Loading branch information
Showing
3 changed files
with
164 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024 Daytona Platforms Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package info | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
"github.com/daytonaio/daytona/pkg/views" | ||
"github.com/daytonaio/daytona/pkg/views/gitprovider" | ||
"golang.org/x/term" | ||
) | ||
|
||
const propertyNameWidth = 20 | ||
|
||
var propertyNameStyle = lipgloss.NewStyle(). | ||
Foreground(views.LightGray) | ||
|
||
var propertyValueStyle = lipgloss.NewStyle(). | ||
Foreground(views.Light). | ||
Bold(true) | ||
|
||
func Render(gp *gitprovider.GitProviderView, forceUnstyled bool) { | ||
var output string | ||
output += "\n\n" | ||
|
||
output += views.GetStyledMainTitle("Git Provider Info") + "\n\n" | ||
|
||
output += getInfoLine("ID", gp.Id) + "\n" | ||
|
||
output += getInfoLine("Name", gp.Name) + "\n" | ||
|
||
output += getInfoLine("Alias", gp.Alias) + "\n" | ||
|
||
output += getInfoLine("Username", gp.Username) + "\n" | ||
|
||
if gp.BaseApiUrl != "" { | ||
output += getInfoLine("Base API URL", gp.BaseApiUrl) + "\n" | ||
} | ||
|
||
terminalWidth, _, err := term.GetSize(int(os.Stdout.Fd())) | ||
if err != nil { | ||
fmt.Println(output) | ||
return | ||
} | ||
if terminalWidth < views.TUITableMinimumWidth || forceUnstyled { | ||
renderUnstyledInfo(output) | ||
return | ||
} | ||
|
||
renderTUIView(output, views.GetContainerBreakpointWidth(terminalWidth)) | ||
} | ||
|
||
func renderUnstyledInfo(output string) { | ||
fmt.Println(output) | ||
} | ||
|
||
func renderTUIView(output string, width int) { | ||
output = lipgloss.NewStyle().PaddingLeft(3).Render(output) | ||
|
||
content := lipgloss. | ||
NewStyle().Width(width). | ||
Render(output) | ||
|
||
fmt.Println(content) | ||
} | ||
|
||
func getInfoLine(key, value string) string { | ||
return propertyNameStyle.Render(fmt.Sprintf("%-*s", propertyNameWidth, key)) + propertyValueStyle.Render(value) + "\n" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 Daytona Platforms Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package list | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/daytonaio/daytona/pkg/views" | ||
"github.com/daytonaio/daytona/pkg/views/gitprovider" | ||
"github.com/daytonaio/daytona/pkg/views/gitprovider/info" | ||
views_util "github.com/daytonaio/daytona/pkg/views/util" | ||
) | ||
|
||
type rowData struct { | ||
Name string | ||
Alias string | ||
Username string | ||
BaseApiUrl string | ||
} | ||
|
||
func ListGitProviders(gitProviderViewList []gitprovider.GitProviderView) { | ||
var showBaseApiUrlColumn bool | ||
headers := []string{"Name", "Alias", "Username", "Base API URL"} | ||
|
||
for _, gp := range gitProviderViewList { | ||
if gp.BaseApiUrl != "" { | ||
showBaseApiUrlColumn = true | ||
break | ||
} | ||
} | ||
|
||
data := [][]string{} | ||
|
||
for _, b := range gitProviderViewList { | ||
data = append(data, getRowFromRowData(b)) | ||
} | ||
|
||
if !showBaseApiUrlColumn { | ||
headers = headers[:len(headers)-1] | ||
for value := range data { | ||
data[value] = data[value][:len(data[value])-1] | ||
} | ||
} | ||
|
||
table := views_util.GetTableView(data, headers, nil, func() { | ||
renderUnstyledList(gitProviderViewList) | ||
}) | ||
|
||
fmt.Println(table) | ||
} | ||
|
||
func renderUnstyledList(gitProviderViewList []gitprovider.GitProviderView) { | ||
for _, b := range gitProviderViewList { | ||
info.Render(&b, true) | ||
|
||
if b.Id != gitProviderViewList[len(gitProviderViewList)-1].Id { | ||
fmt.Printf("\n%s\n\n", views.SeparatorString) | ||
} | ||
} | ||
} | ||
|
||
func getRowFromRowData(build gitprovider.GitProviderView) []string { | ||
var data rowData | ||
|
||
data.Name = build.Name + views_util.AdditionalPropertyPadding | ||
data.Alias = build.Alias | ||
data.Username = build.Username | ||
data.BaseApiUrl = build.BaseApiUrl | ||
|
||
return []string{ | ||
views.NameStyle.Render(data.Name), | ||
views.DefaultRowDataStyle.Render(data.Alias), | ||
views.DefaultRowDataStyle.Render(data.Username), | ||
views.DefaultRowDataStyle.Render(data.BaseApiUrl), | ||
} | ||
} |