Skip to content

Commit

Permalink
Merge pull request #18 from noahstreller/feature/config-file
Browse files Browse the repository at this point in the history
  • Loading branch information
noahstreller authored Sep 4, 2024
2 parents 5158c50 + 2805e05 commit 8e2c9fb
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ require (
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 5 additions & 0 deletions igittconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This is the default configuration for Igitt.
# Please adjust the values as needed.

# Choices: "emoji", "unicode", "nerdfont" - default: unicode
iconType: "nerdfont"
31 changes: 22 additions & 9 deletions internal/operations/interactive/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/fatih/color"
"github.com/noahstreller/igitt/internal/operations/git"
"github.com/noahstreller/igitt/internal/utilities/config"
"github.com/noahstreller/igitt/internal/utilities/logger"
"github.com/rivo/uniseg"

Expand All @@ -27,7 +28,7 @@ const (
type IconType int

func (d IconType) String() string {
return [...]string{"Unicode", "Emoji", "NerdFont"}[d]
return [...]string{"unicode", "emoji", "nerdfont"}[d]
}

type Command struct {
Expand Down Expand Up @@ -55,18 +56,30 @@ type CommandFlowResult struct {

const iconWidth = 3
const shortcutsEnabled = false
const iconVariant = NerdFont

func bold(s string) string {
return color.New(color.Bold).Sprint(s)
var iconVariant = getIconVariantFromConfig()

func getIconVariantFromConfig() IconType {
config := config.GetConfig()
userIconType := strings.ToLower(config.IconType)

if userIconType == "emoji" {
return Emoji
}

if userIconType == "nerdfont" {
return NerdFont
}

return Unicode
}

func getTitle(command Command) string {
if iconVariant == Emoji {
return command.IconEmoji + strings.Repeat(" ", iconWidth-uniseg.StringWidth(command.IconEmoji)) + bold(command.Name)
return command.IconEmoji + strings.Repeat(" ", iconWidth-uniseg.StringWidth(command.IconEmoji)) + command.Name
}
if iconVariant == NerdFont {
return command.IconNerdFont + strings.Repeat(" ", iconWidth-uniseg.StringWidth(command.IconNerdFont)) + bold(command.Name)
return command.IconNerdFont + strings.Repeat(" ", iconWidth-uniseg.StringWidth(command.IconNerdFont)) + command.Name
}
return command.Icon + strings.Repeat(" ", iconWidth-uniseg.StringWidth(command.Icon)) + command.Name
}
Expand Down Expand Up @@ -181,7 +194,7 @@ func StartInteractive() {
huh.NewGroup(
huh.NewSelect[string]().
Title("Branch selection").
Description(bold("\n Select a branch\n")).
Description("\n Select a branch\n").
OptionsFunc(getBranchOptions, &commandFlowResult.SelectedCommand).
Value(&commandFlowResult.SelectedBranch))).WithTheme(theme)

Expand Down Expand Up @@ -295,7 +308,7 @@ func StartInteractive() {
getTitle(commandFlowResult.SelectedCommand),
commandFlowResult.SelectedCommand.Description,
getNextStepIcon(iconVariant),
bold("Next step: "+commandFlowResult.SelectedCommand.NextStepTitle),
"Next step: "+commandFlowResult.SelectedCommand.NextStepTitle,
)
}

Expand All @@ -304,7 +317,7 @@ func StartInteractive() {
getTitle(commandFlowResult.SelectedCommand),
commandFlowResult.SelectedCommand.Description,
getNoNextStepIcon(iconVariant),
bold("No"),
"No",
)
}, &commandFlowResult.SelectedCommand).
Options(commandOptions...).
Expand Down
2 changes: 1 addition & 1 deletion internal/operations/interactive/operations.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Status",
"shortcut": "s",
"description": "Show changed files in the working directory",
"icon": "⚆⚆",
"icon": "?",
"icon_emoji": "👀",
"icon_nerdfont": "",
"nextStep": "none",
Expand Down
122 changes: 122 additions & 0 deletions internal/utilities/config/confighelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package config

import (
"fmt"
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/noahstreller/igitt/internal/utilities/logger"
"gopkg.in/yaml.v3"
)

const configFileName = "igittconfig.yaml"

type IgittConfig struct {
IconType string `yaml:"iconType"`
}

func InitialConfig() error {

configExists, configPath := HasConfigFile()
if configExists {
return nil
}

file, err := os.OpenFile(configPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)

if err != nil {
logger.ErrorLogger.Fatal(err)
return err
}

_, err = file.Write([]byte(GetDefaultConfig()))

if err != nil {
logger.ErrorLogger.Fatal(err)
return err
}

defer file.Close()

return nil
}

func HasConfigFile() (bool, string) {
executable, err := os.Executable()
executableDir := filepath.Dir(executable)
configPath := filepath.Join(executableDir, configFileName)

if err != nil {
logger.ErrorLogger.Fatal(err)
}

_, err = os.Stat(configPath)

return !os.IsNotExist(err), configPath
}

func GetConfig() IgittConfig {
configExists, configPath := HasConfigFile()
if !configExists {
logger.ErrorLogger.Fatal("Config file does not exist, creating one now")
err := InitialConfig()
if err != nil {
logger.ErrorLogger.Fatal(err)
return IgittConfig{}
}
}

config, err := ReadConfigFromPath(configPath)
if err != nil {
logger.ErrorLogger.Fatal(err)
return IgittConfig{}
}

return config
}

func ReadConfigFromPath(configPath string) (IgittConfig, error) {
var config IgittConfig
file, err := os.Open(configPath)
if err != nil {
return config, err
}
defer file.Close()

decoder := yaml.NewDecoder(file)
err = decoder.Decode(&config)
return config, err
}

func GetDefaultConfig() string {
configContent := `# This is the configuration for Igitt.
# Please adjust the values as needed.
# Choices: "emoji", "unicode", "nerdfont"
iconType: "unicode"
`

return configContent
}

func GetConfigPath(print bool) string {
configExists, configPath := HasConfigFile()

if !configExists {
logger.ErrorLogger.Fatal("Config file does not exist, creating one now")
err := InitialConfig()
if err != nil {
logger.ErrorLogger.Fatal(err)
return ""
}
}

if print {
fmt.Printf("\n\nTo edit the configuration, %s in your text editor:\n\n", color.YellowString("open the following file"))
color.Blue(configPath)
fmt.Println()
}

return configPath
}
18 changes: 17 additions & 1 deletion internal/utilities/initialize/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/noahstreller/igitt/internal/operations"
"github.com/noahstreller/igitt/internal/operations/git"
"github.com/noahstreller/igitt/internal/operations/interactive"
"github.com/noahstreller/igitt/internal/utilities/config"
"github.com/noahstreller/igitt/internal/utilities/logger"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -124,9 +125,23 @@ func InitializeIgitt() {
},
}

var igittConfigCmd = &cobra.Command{
Use: "config",
Short: "Print the path of the igitt configuration file",
Run: func(cmd *cobra.Command, args []string) {
config.GetConfigPath(true)
},
}

cobra.OnInitialize(func() {
err := config.InitialConfig()

if err != nil {
logger.ErrorLogger.Fatal(err)
return
}

if len(os.Args) == 1 {
// fmt.Println(color.RedString("No arguments provided, entering interactive mode."))
interactive.StartInteractive()
}
})
Expand All @@ -143,6 +158,7 @@ func InitializeIgitt() {
commitCmd,
createAliasScripts,
branchCmd,
igittConfigCmd,
)
err := rootCmd.Execute()
if err != nil {
Expand Down

0 comments on commit 8e2c9fb

Please sign in to comment.