Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for themes #21

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package cmd

import (
"errors"
"fmt"
"os"

"github.com/charmbracelet/bubbletea"
"github.com/noahgorstein/jqp/tui/bubbles/jqplayground"
"github.com/noahgorstein/jqp/tui/theme"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

var rootCmd = &cobra.Command{
Expand All @@ -17,6 +21,14 @@ var rootCmd = &cobra.Command{
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {

cmd.Flags().VisitAll(func(f *pflag.Flag) {
// Apply the viper config value to the flag when the flag is not set and viper has a value
if !f.Changed && viper.IsSet(f.Name) {
val := viper.Get(f.Name)
cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
}
})

if isInputFromPipe() {
stdin := streamToBytes(os.Stdin)

Expand All @@ -25,7 +37,7 @@ var rootCmd = &cobra.Command{
return errors.New("JSON is not valid")
}

bubble := jqplayground.New(stdin, "STDIN")
bubble := jqplayground.New(stdin, "STDIN", theme.GetTheme(flags.theme))
p := tea.NewProgram(bubble, tea.WithAltScreen())
if err := p.Start(); err != nil {
return err
Expand Down Expand Up @@ -57,7 +69,7 @@ var rootCmd = &cobra.Command{
return err
}

bubble := jqplayground.New(data, fi.Name())
bubble := jqplayground.New(data, fi.Name(), theme.GetTheme(flags.theme))
p := tea.NewProgram(bubble, tea.WithAltScreen())

if err := p.Start(); err != nil {
Expand All @@ -69,23 +81,63 @@ var rootCmd = &cobra.Command{
},
}

func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".jqp" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".jqp")
}

if err := viper.ReadInConfig(); err == nil {
fmt.Println("Config file:", viper.ConfigFileUsed(), "was used.")
}
}

var flags struct {
filepath string
theme string
}

var flagsName = struct {
file, fileShort string
file string
fileShort string
theme string
themeShort string
}{
"file", "f",
file: "file",
fileShort: "f",
theme: "theme",
themeShort: "t",
}

var cfgFile string

func Execute() {

cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.jqp.yaml)")

rootCmd.Flags().StringVarP(
&flags.filepath,
flagsName.file,
flagsName.fileShort,
"", "path to the input JSON file")

rootCmd.Flags().StringVarP(
&flags.theme,
flagsName.theme,
flagsName.themeShort,
"", "jqp theme [\"default\", \"dracula\",\"nord\", \"monokai\", \"monokailight\", \"autumn\"]")

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
20 changes: 17 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,42 @@ require (
github.com/alecthomas/chroma v0.10.0
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/bubbles v0.14.1-0.20220915145142-72d87e451304
github.com/charmbracelet/bubbletea v0.22.1
github.com/charmbracelet/bubbletea v0.22.2-0.20220926195852-5c4218e5f6f1
github.com/charmbracelet/lipgloss v0.5.0
github.com/itchyny/gojq v0.12.8
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.13.0
)

require (
github.com/containerd/console v1.0.3 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/itchyny/timefmt-go v0.1.3 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading