Skip to content

Commit

Permalink
list command, print as table and improve styling
Browse files Browse the repository at this point in the history
  • Loading branch information
Gjergj committed Apr 29, 2024
1 parent 19f5391 commit 7f483cd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 42 deletions.
86 changes: 44 additions & 42 deletions cmd/testmyapp/listproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ import (
"flag"
"fmt"
"github.com/Gjergj/testmyapp/pkg/models"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/peterbourgon/ff/v3/ffcli"
"io"
"log"
"os"
)

const (
existsOnlyLocally exists = "Local"
existsOnlyOnRemote exists = "Remote"
existsOnBoth exists = "Both"
existsHere exists = "Current Directory"
)

type exists string

func listProjectCommand() *ffcli.Command {
c, err := getConfig()
if err != nil {
Expand Down Expand Up @@ -91,73 +102,64 @@ func getAllProjectsByUserID(username string, printDirs bool, c *Config) {
return
}
type p struct {
Icon string
ProjectURL string
ProjectDir string
ProjectName string
ExistsOnlyLocally bool
ExistsOnlyOnRemote bool
ProjectURL string
ProjectDir string
ProjectName string
Exists exists
}
projects := make(map[string]p)

for _, project := range apiResp.Projects {
projects[project.ProjectName] = p{
Icon: "❌",
ProjectURL: project.URL,
ProjectName: project.ProjectName,
ProjectDir: "",
ExistsOnlyLocally: false,
ExistsOnlyOnRemote: true,
ProjectURL: project.URL,
ProjectName: project.ProjectName,
ProjectDir: "",
Exists: existsOnlyOnRemote,
}
for _, rp := range c.Accounts[userName].Projects {
if rp.ProjectName == project.ProjectName && rp.ProjectDir == pwd {
projects[project.ProjectName] = p{
Icon: "→",
ProjectURL: project.URL,
ProjectName: project.ProjectName,
ProjectDir: rp.ProjectDir,
ExistsOnlyLocally: false,
ExistsOnlyOnRemote: false,
ProjectURL: project.URL,
ProjectName: project.ProjectName,
ProjectDir: rp.ProjectDir,
Exists: existsHere,
}
} else if rp.ProjectName == project.ProjectName {
projects[project.ProjectName] = p{
Icon: "✔",
ProjectURL: project.URL,
ProjectName: project.ProjectName,
ProjectDir: rp.ProjectDir,
ExistsOnlyLocally: false,
ExistsOnlyOnRemote: false,
ProjectURL: project.URL,
ProjectName: project.ProjectName,
ProjectDir: rp.ProjectDir,
Exists: existsOnBoth,
}
}
}
}
for _, rp := range c.Accounts[userName].Projects {
if _, ok := projects[rp.ProjectName]; !ok {
projects[rp.ProjectName] = p{
Icon: "❌",
ProjectURL: "",
ProjectName: rp.ProjectName,
ProjectDir: rp.ProjectDir,
ExistsOnlyLocally: true,
ExistsOnlyOnRemote: false,
ProjectURL: "",
ProjectName: rp.ProjectName,
ProjectDir: rp.ProjectDir,
Exists: existsOnlyLocally,
}
}
}
projectTable := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("#008000"))).Width(120)
if printDirs {
projectTable.Headers("Exists", "Project Name", "URL", "Dir")
} else {
projectTable.Headers("Exists", "Project Name", "URL")
}

// Print the projects
for _, project := range projects {
fmt.Printf("%s", project.Icon)
if project.ExistsOnlyLocally {
fmt.Printf("\t%s", "Local only")
} else if project.ExistsOnlyOnRemote {
fmt.Printf("\t%s", "Remote Only")
} else {
fmt.Printf("\t\t")
}
fmt.Printf("\t%s", project.ProjectName)
fmt.Printf("\t%s", project.ProjectURL)
if printDirs {
fmt.Printf("\t%s", project.ProjectDir)
projectTable.Row(string(project.Exists), project.ProjectName, project.ProjectURL, project.ProjectDir)
} else {
projectTable.Row(string(project.Exists), project.ProjectName, project.ProjectURL)
}
fmt.Println()
}
fmt.Println(projectTable)
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ module github.com/Gjergj/testmyapp
go 1.21.0

require (
github.com/charmbracelet/lipgloss v0.10.0
github.com/peterbourgon/ff/v3 v3.4.0
github.com/rjeczalik/notify v0.9.3
golang.org/x/crypto v0.20.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
)
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/peterbourgon/ff/v3 v3.4.0 h1:QBvM/rizZM1cB0p0lGMdmR7HxZeI/ZrBWB4DqLkMUBc=
github.com/peterbourgon/ff/v3 v3.4.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
Expand Down

0 comments on commit 7f483cd

Please sign in to comment.