Support width as a config setting

This commit is contained in:
Christian Muehlhaeuser 2020-10-23 08:46:59 +02:00
parent c45b2fc4b0
commit 2091f03235
2 changed files with 14 additions and 8 deletions

View file

@ -180,6 +180,8 @@ Here's an example config:
style: "light" style: "light"
# show local files only; no network (TUI-mode only) # show local files only; no network (TUI-mode only)
local: true local: true
# word-wrap at width
width: 80
``` ```
## 🔒 Encryption: How It Works ## 🔒 Encryption: How It Works

20
main.go
View file

@ -134,6 +134,11 @@ func sourceFromArg(arg string) (*source, error) {
} }
func validateOptions(cmd *cobra.Command) { func validateOptions(cmd *cobra.Command) {
// grab config values from Viper
style = viper.GetString("style")
width = viper.GetUint("width")
localOnly = viper.GetBool("local")
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd())) isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
// We want to use a special no-TTY style, when stdout is not a terminal // We want to use a special no-TTY style, when stdout is not a terminal
// and there was no specific style passed by arg // and there was no specific style passed by arg
@ -142,22 +147,19 @@ func validateOptions(cmd *cobra.Command) {
} }
// Detect terminal width // Detect terminal width
if isTerminal && !cmd.Flags().Changed("width") { if isTerminal && width == 0 && !cmd.Flags().Changed("width") {
w, _, err := terminal.GetSize(int(os.Stdout.Fd())) w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err == nil { if err == nil {
width = uint(w) width = uint(w)
} }
if width > 120 {
width = 120
}
} }
if width == 0 { if width == 0 {
width = 80 width = 80
} }
if width > 120 {
width = 120
}
// grab config values from Viper
style = viper.GetString("style")
localOnly = viper.GetBool("local")
} }
func execute(cmd *cobra.Command, args []string) error { func execute(cmd *cobra.Command, args []string) error {
@ -319,8 +321,10 @@ func init() {
// Config bindings // Config bindings
_ = viper.BindPFlag("style", rootCmd.Flags().Lookup("style")) _ = viper.BindPFlag("style", rootCmd.Flags().Lookup("style"))
_ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width"))
_ = viper.BindPFlag("local", rootCmd.Flags().Lookup("local")) _ = viper.BindPFlag("local", rootCmd.Flags().Lookup("local"))
viper.SetDefault("style", "auto") viper.SetDefault("style", "auto")
viper.SetDefault("width", 0)
viper.SetDefault("local", "false") viper.SetDefault("local", "false")
// Stash // Stash