Skip to content

Commit

Permalink
Merge pull request #42 from Glow-Project/enhance-output
Browse files Browse the repository at this point in the history
Enhance output
  • Loading branch information
Tch1b0 authored Aug 6, 2023
2 parents 4983568 + 8e7f541 commit 883b16e
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 35 deletions.
File renamed without changes.
7 changes: 3 additions & 4 deletions pkg/commands/init.go → internal/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"fmt"
"path"

"github.com/Glow-Project/ppm/pkg/utility"
"github.com/fatih/color"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/urfave/cli/v2"
)

Expand All @@ -19,10 +18,10 @@ func initialize(ctx *cli.Context) error {
return fmt.Errorf("error creating ppm.json config-file: %w", err)
}

fmt.Println(color.GreenString("new ppm.json config-file generated"))
utility.ColorPrintln("{GRN}new ppm.json config-file generated")

if ok, _ := utility.DoesPathExist(path.Join(paths.Root, ".git")); ok {
fmt.Println(color.YellowString("when using ppm it is recommended to add the addons directory to your .gitignore file"))
utility.ColorPrintln("{YLW}when using ppm it is recommended to add the addons direcotry to your .gitignore file")
}

return nil
Expand Down
25 changes: 11 additions & 14 deletions pkg/commands/install.go → internal/commands/install.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package commands

import (
"fmt"

"github.com/Glow-Project/ppm/pkg/fetch"
"github.com/Glow-Project/ppm/pkg/utility"
"github.com/fatih/color"
"github.com/Glow-Project/ppm/internal/fetch"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/go-git/go-git/v5"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -43,29 +40,29 @@ func installAllDependencies(config *utility.PpmConfig, paths utility.Paths) erro

func installDependency(config *utility.PpmConfig, paths utility.Paths, dependency utility.Dependency, isSubDependency bool) error {
if !isSubDependency {
fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginIdentifier(dependency.Identifier)))
utility.ColorPrintln("\rinstalling {YLW}%s", dependency.Identifier)
} else {
fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginIdentifier(dependency.Identifier)))
utility.ColorPrintln("\t -> installing {YLW}%s", dependency.Identifier)
}
loadAnim := utility.StartLoading()

err := fetch.InstallDependency(dependency, paths)
loadAnim.Stop()

switch err.(type) {
switch err := err.(type) {
case nil:
break
case *fetch.InvalidVersionError:
dependency.Version = nil
versionError(dependency.Identifier, err.(*fetch.InvalidVersionError).Version)
versionError(dependency.Identifier, err.Version)
case *fetch.CloneError:
gitErr := err.(*fetch.CloneError).GitError
gitErr := err.GitError
if gitErr == git.ErrRepositoryAlreadyExists {
alreadyInstalled(dependency.Identifier)
return nil
} else {
installError(dependency.Identifier)
return err.(*fetch.CloneError).GitError
return err.GitError
}
default:
return err
Expand Down Expand Up @@ -103,13 +100,13 @@ func installDependency(config *utility.PpmConfig, paths utility.Paths, dependenc
}

func alreadyInstalled(dependency string) {
fmt.Println(color.GreenString("\rthe plugin"), color.YellowString(dependency), color.GreenString("is already installed"))
utility.ColorPrintln("\r{GRN}the plugin {YLW}%s {GRN}is already installed", dependency)
}

func installError(dependency string) {
fmt.Printf(color.RedString("\rsome issues occured while trying to install %s, %s"), color.YellowString(dependency), color.RedString("are you sure you spelled it right?\n"))
utility.ColorPrintln("\r{RED}some issues occured while trying to install {YLW}%s {RED}are you sure you spelled it right?", dependency)
}

func versionError(dependency string, version string) {
fmt.Printf(color.RedString("\rthe version \"%s\" %s %s %s"), color.YellowString(version), color.RedString("for the dependency"), color.YellowString(dependency), color.RedString("was not found. The default version was installed\n"))
utility.ColorPrintln("\r{RED}the version {YLW}%s {RED} for the dependency {YLW}%s {RED}was not found. The default version was installed", version, dependency)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package commands

import (
"github.com/Glow-Project/ppm/pkg/utility"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/urfave/cli/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/show.go → internal/commands/show.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package commands

import (
"github.com/Glow-Project/ppm/pkg/utility"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/urfave/cli/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/tidy.go → internal/commands/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"os"

"github.com/Glow-Project/ppm/pkg/utility"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/urfave/cli/v2"
)

Expand Down
12 changes: 5 additions & 7 deletions pkg/commands/uninstall.go → internal/commands/uninstall.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package commands

import (
"fmt"
"os"
"path"

"github.com/Glow-Project/ppm/pkg/utility"
"github.com/fatih/color"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/urfave/cli/v2"
)

Expand All @@ -27,9 +25,9 @@ func uninstall(ctx *cli.Context) error {
if config.HasDependency(dep) && !config.HasSubDependency(dep) {
uninstallDependency(&config, paths, dep, false)
} else if config.HasSubDependency(dep) {
fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is a sub dependency and can only be uninstalled by uninstalling its parent"))
utility.ColorPrintln("{RED}the plugin {YLW}%s {RED}is a sub-dependency and can only be uninstalled by uninstalling its parent", dep.Identifier)
} else {
fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is not installed"))
utility.ColorPrintln("{RED}the plugin %s is not installed", dep.Identifier)
}
}

Expand All @@ -54,9 +52,9 @@ func uninstallAllDependencies(config *utility.PpmConfig, paths utility.Paths, ha

func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, dependency utility.Dependency, isSubDependency bool) error {
if !isSubDependency {
fmt.Println("\runinstalling", color.YellowString(dependency.Identifier))
utility.ColorPrintln("\runinstalling {YLW}%s", dependency.Identifier)
} else {
fmt.Println("\t -> uninstalling", color.YellowString(dependency.Identifier))
utility.ColorPrintln("\t -> uninstalling {YLW}%s", dependency.Identifier)
}
loadAnim := utility.StartLoading()

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/update.go → internal/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package commands
import (
"path/filepath"

"github.com/Glow-Project/ppm/pkg/utility"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/urfave/cli/v2"
)

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/fetch/fetch.go → internal/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"regexp"
"strings"

"github.com/Glow-Project/ppm/pkg/utility"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/go-git/go-git/v5"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions internal/utility/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package utility

import (
"fmt"
"strings"
)

var UseColor = true

func ColorPrint(str string, args ...any) {
fmt.Printf(colorFmt(str), args...)
}

func ColorPrintln(str string, args ...any) {
fmt.Printf(colorFmt(str)+"\n", args...)
}

func colorFmt(str string) string {
colors := map[string]string{
"{RED}": "\033[31m",
"{YLW}": "\033[33m",
"{BLU}": "\033[34m",
"{PUR}": "\033[35m",
"{GRN}": "\033[32m",
"{RES}": "\033[0m",
}

if !UseColor {
for k := range colors {
colors[k] = ""
}
}

return replaceAll(str+"{RES}", colors)
}

func replaceAll(str string, substrings map[string]string) string {
for k, v := range substrings {
str = strings.ReplaceAll(str, k, v)
}

return str
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 1 addition & 4 deletions pkg/utility/utility.go → internal/utility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package utility

import (
"errors"
"fmt"
"os"
"strings"

"github.com/fatih/color"
)

// check wether an absolute path exists
Expand Down Expand Up @@ -47,7 +44,7 @@ func GetVersionOrNot(dependency string) (string, string) {
}

func PrintDone() {
fmt.Print(color.GreenString("\rdone\n"))
ColorPrintln("{GRN}done")
}

func GetPluginIdentifier(name string) string {
Expand Down
File renamed without changes.
16 changes: 15 additions & 1 deletion ppm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"log"
"os"

"github.com/Glow-Project/ppm/pkg/commands"
"github.com/Glow-Project/ppm/internal/commands"
"github.com/Glow-Project/ppm/internal/utility"
"github.com/urfave/cli/v2"
)

Expand All @@ -18,6 +19,19 @@ func main() {
EnableBashCompletion: true,
Commands: commands.Commands(),
HideVersion: false,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "no-color",
Required: false,
Action: func(ctx *cli.Context, b bool) error {
if b {
utility.UseColor = false
}

return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 883b16e

Please sign in to comment.