Skip to content

Commit

Permalink
feat(lib): Use Lip Gloss to make outputs prettier
Browse files Browse the repository at this point in the history
Add colors, symbols and indents to make outputs clean.
  • Loading branch information
5ouma committed Apr 5, 2024
1 parent 401f62c commit 667a3ab
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (cmd *cmd) execCheckCmd(command *cobra.Command, args []string) error {
return err
}

fmt.Println(lib.H2.Render("🔍 Check Login Items"))
if err := cmd.loginItems.Load(path); err != nil {
return err
}
Expand All @@ -39,9 +40,10 @@ func (cmd *cmd) execCheckCmd(command *cobra.Command, args []string) error {
return err
}
if !reflect.DeepEqual(cmd.loginItems, loginItems) {
fmt.Println()
return fmt.Errorf("login items are out-of-date")
}
fmt.Println("✅ Login Items are up-to-date!")
fmt.Println(lib.H1.Render("✅ Login Items are up-to-date!"))

return nil
}
2 changes: 1 addition & 1 deletion cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func New() *cmd {
}
cmd.command.CompletionOptions.HiddenDefaultCmd = true
cmd.command.SetVersionTemplate("📑 {{.Use}} {{.Version}}\n")
cmd.command.SetErrPrefix("🚨")
cmd.command.SetErrPrefix(" 🚨")
cmd.command.AddCommand(
cmd.newCheckCmd(),
cmd.newLoadCmd(),
Expand Down
5 changes: 4 additions & 1 deletion cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path/filepath"

"github.com/5ouma/mli/lib"
"github.com/spf13/cobra"
)

Expand All @@ -29,13 +30,15 @@ func (cmd *cmd) execLoadCmd(command *cobra.Command, args []string) error {
return err
}

fmt.Println(lib.H2.Render("📂 Load Login Items"))
if err := cmd.loginItems.Load(path); err != nil {
return err
}
if err := cmd.loginItems.Add(); err != nil {
fmt.Println()
return err
}
fmt.Printf("✅ Successfully loaded from \"%s\"!\n", path)
fmt.Println(lib.H1.Render("✅ Successfully loaded"))

return nil
}
6 changes: 5 additions & 1 deletion cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path/filepath"

"github.com/5ouma/mli/lib"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,13 +35,16 @@ func (cmd *cmd) execSaveCmd(command *cobra.Command, args []string) error {
return err
}

fmt.Println(lib.H2.Render("💾 Save Login Items"))
if err := cmd.loginItems.Get(); err != nil {
fmt.Println()
return err
}
if err := cmd.loginItems.Save(path, force); err != nil {
fmt.Println()
return err
}
fmt.Printf("✅ Successfully saved to \"%s\"!\n", path)
fmt.Println(lib.H1.Render("✅", filepath.Base(path)))

return nil
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ go 1.22.2

require (
github.com/andybrewer/mack v0.0.0-20220307193339-22e922cc18af
github.com/charmbracelet/lipgloss v0.10.0
github.com/spf13/cobra v1.8.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // 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
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.12.0 // indirect
)
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
github.com/andybrewer/mack v0.0.0-20220307193339-22e922cc18af h1:PNE0xdyuLeOTujftqZs8DlhDoi+T54ONZhiGOxH5t2A=
github.com/andybrewer/mack v0.0.0-20220307193339-22e922cc18af/go.mod h1:oUO968BJnuljnB5tntrY3w3zDfI5/PqnQ+RuiZ8aFhk=
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/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
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/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/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions lib/oascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (loginItems *LoginItems) Get() error {
return err
}
*loginItems = append(*loginItems, &loginItem{names[i], path[i], hidden})
fmt.Printf("🔍 %s\n", names[i])
fmt.Println(CheckedItem.Render(), names[i])
}
sort.Slice(*loginItems, func(before, after int) bool {
return (*loginItems)[before].Name < (*loginItems)[after].Name
Expand All @@ -46,13 +46,13 @@ func (loginItems *LoginItems) Get() error {
func (loginItems *LoginItems) Add() error {
for _, loginItem := range *loginItems {
if isExist, err := isExist(loginItem.Path); !isExist && err == nil {
fmt.Printf("⚠️ not found: \"%s\"\n", loginItem.Path)
fmt.Println(WarnedItem.Render(), loginItem.Path)
continue
}
if _, err := mack.Tell("System Events", fmt.Sprintf(`make login item at end with properties { name: "%s", path: "%s", hidden: %v }`, loginItem.Name, loginItem.Path, loginItem.Hidden)); err != nil {
return err
}
fmt.Printf("🔍 %s\n", loginItem.Name)
fmt.Println(CheckedItem.Render(), loginItem.Name)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion lib/oascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func ExampleLoginItems() {
loginItems := LoginItems{{"Not found", "/Applications/Not found.app", false}}
loginItems.Add()
// Output:
// ⚠️ not found: "/Applications/Not found.app"
// ⚠/Applications/Not found.app
}
20 changes: 20 additions & 0 deletions lib/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package lib
import (
"errors"
"os"

"github.com/charmbracelet/lipgloss"
)

type loginItem struct {
Expand All @@ -12,6 +14,24 @@ type loginItem struct {
}
type LoginItems []*loginItem

var (
heading = lipgloss.NewStyle().
Foreground(lipgloss.CompleteColor{TrueColor: "#007aff", ANSI256: "27"}).
Bold(true).
Padding(1)
H1 = heading.Copy()
H2 = heading.Copy().SetString("▌")

item = lipgloss.NewStyle().
PaddingLeft(2)
CheckedItem = item.Copy().
Foreground(lipgloss.CompleteColor{TrueColor: "#63b946", ANSI256: "41"}).
SetString("✔︎")
WarnedItem = item.Copy().
Foreground(lipgloss.CompleteColor{TrueColor: "#ffc627", ANSI256: "226"}).
SetString("⚠︎")
)

func isExist(path string) (bool, error) {
if _, err := os.Stat(path); err == nil {
return true, nil
Expand Down

0 comments on commit 667a3ab

Please sign in to comment.