-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/gabe565/cli-of-life/internal/pattern" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
ShellBash = "bash" | ||
ShellZsh = "zsh" | ||
ShellFish = "fish" | ||
ShellPowerShell = "powershell" | ||
) | ||
|
||
func shells() []string { | ||
return []string{ShellBash, ShellZsh, ShellFish, ShellPowerShell} | ||
} | ||
|
||
func RegisterCompletion(cmd *cobra.Command) error { | ||
return errors.Join( | ||
cmd.RegisterFlagCompletionFunc(FileFlag, | ||
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return []string{pattern.ExtRLE, pattern.ExtPlaintext}, cobra.ShellCompDirectiveFilterFileExt | ||
}, | ||
), | ||
cmd.RegisterFlagCompletionFunc(FileFormatFlag, | ||
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return pattern.FormatStrings(), cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
), | ||
cmd.RegisterFlagCompletionFunc(RuleStringFlag, | ||
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return []string{pattern.GameOfLife().String(), pattern.HighLife().String()}, cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package config | ||
|
||
import "github.com/gabe565/cli-of-life/internal/pattern" | ||
|
||
type Config struct { | ||
File string | ||
FileFormat string | ||
RuleString string | ||
Play bool | ||
|
||
Completion string | ||
} | ||
|
||
func New() *Config { | ||
return &Config{ | ||
FileFormat: "auto", | ||
RuleString: pattern.GameOfLife().String(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package config | ||
|
||
import "context" | ||
|
||
type ctxKey uint8 | ||
|
||
const configKey ctxKey = iota | ||
|
||
func NewContext(ctx context.Context, conf *Config) context.Context { | ||
return context.WithValue(ctx, configKey, conf) | ||
} | ||
|
||
func FromContext(ctx context.Context) (*Config, bool) { | ||
conf, ok := ctx.Value(configKey).(*Config) | ||
return conf, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gabe565/cli-of-life/internal/pattern" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
const ( | ||
FileFlag = "file" | ||
FileFormatFlag = "file-format" | ||
RuleStringFlag = "rule-string" | ||
PlayFlag = "play" | ||
CompletionFlag = "completion" | ||
) | ||
|
||
func (c *Config) RegisterFlags(fs *pflag.FlagSet) { | ||
fs.StringVarP(&c.File, FileFlag, "f", c.File, "Loads a pattern file on startup") | ||
fs.StringVar(&c.FileFormat, FileFormatFlag, c.FileFormat, "File format (one of: "+strings.Join(pattern.FormatStrings(), ", ")+")") | ||
fs.StringVar(&c.RuleString, RuleStringFlag, c.RuleString, "Rule string to use. This will be ignored if a pattern file is loaded.") | ||
fs.BoolVar(&c.Play, PlayFlag, c.Play, "Play on startup") | ||
fs.StringVar(&c.Completion, CompletionFlag, c.Completion, "Output command-line completion code for the specified shell (one of: "+strings.Join(shells(), ", ")+")") | ||
} |