Skip to content

Commit

Permalink
feat: --raw flag to list environments without special decorations
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Feb 24, 2024
1 parent e862e8c commit 7ec50b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
22 changes: 14 additions & 8 deletions internal/cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

"github.com/gookit/color"
"github.com/luisnquin/senv/internal/core"
"github.com/samber/lo"
)

var rxSenvDotEnvComment = regexp.MustCompile("[\\#\\_]+")

func Ls(currentDir string) error {
func Ls(currentDir string, raw bool) error {
settings, err := core.LoadUserPreferences()
if err != nil {
return err
Expand All @@ -33,19 +34,24 @@ func Ls(currentDir string) error {

sort.Strings(envNames)

namePrinter := color.New(color.HiGreen, color.OpItalic) // color.HEX("#a4d43f")
activePrinter := color.New(color.LightCyan, color.Bold) // color.HEX("#84dde0")
activeLabel := "(active)"
if !raw {
activePrinter := color.New(color.LightCyan, color.Bold) // color.HEX("#84dde0")
activeLabel = activePrinter.Sprint(activeLabel)

fmt.Fprintf(os.Stdout, "%s:\n", settings.SourceFilePath)
}

fmt.Fprintf(os.Stdout, "%s:\n", settings.SourceFilePath)
namePrinter := color.New(color.HiGreen, color.OpItalic) // color.HEX("#a4d43f")

for _, name := range envNames {
activeLabel := ""
isCurrentEnv := name == activeEnv

if name == activeEnv {
activeLabel = activePrinter.Sprint("(active)")
if !raw {
name = namePrinter.Sprint(name)
}

fmt.Fprintf(os.Stdout, "- %s %s\n", namePrinter.Sprint(name), activeLabel)
fmt.Fprintf(os.Stdout, "- %s %s\n", name, lo.If(isCurrentEnv, activeLabel).Else(""))
}

return nil
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type flags struct {
Get bool
Set string
CompletionShell string
Ls struct {
Raw bool
}
}

func main() {
Expand All @@ -31,6 +34,7 @@ func main() {

ls := flaggy.NewSubcommand("ls")
ls.Description = "List all the environments in the working directory"
ls.Bool(&flags.Ls.Raw, "", "raw", "list environment without especial decorations")
flaggy.AttachSubcommand(ls, 1)

init := flaggy.NewSubcommand("init")
Expand Down Expand Up @@ -68,7 +72,7 @@ func main() {
log.Pretty.Error(err.Error())
}
case ls.Used:
if err := cmd.Ls(currentDir); err != nil {
if err := cmd.Ls(currentDir, flags.Ls.Raw); err != nil {
log.Pretty.Error(err.Error())
}
case flags.Get:
Expand Down

0 comments on commit 7ec50b6

Please sign in to comment.