|
4 | 4 | "fmt"
|
5 | 5 | "strconv"
|
6 | 6 | "strings"
|
| 7 | + "time" |
7 | 8 |
|
8 | 9 | tuikitIO "github.com/jahvon/tuikit/io"
|
9 | 10 | "github.com/jahvon/tuikit/types"
|
@@ -44,7 +45,7 @@ func registerConfigResetCmd(ctx *context.Context, configCmd *cobra.Command) {
|
44 | 45 | func resetConfigFunc(ctx *context.Context, _ *cobra.Command, _ []string) {
|
45 | 46 | logger := ctx.Logger
|
46 | 47 | form, err := views.NewForm(
|
47 |
| - io.Theme(), |
| 48 | + io.Theme(ctx.Config.Theme.String()), |
48 | 49 | ctx.StdIn(),
|
49 | 50 | ctx.StdOut(),
|
50 | 51 | &views.FormField{
|
@@ -80,6 +81,9 @@ func registerSetConfigCmd(ctx *context.Context, configCmd *cobra.Command) {
|
80 | 81 | registerSetWorkspaceModeCmd(ctx, setCmd)
|
81 | 82 | registerSetLogModeCmd(ctx, setCmd)
|
82 | 83 | registerSetTUICmd(ctx, setCmd)
|
| 84 | + registerSetNotificationsCmd(ctx, setCmd) |
| 85 | + registerSetThemeCmd(ctx, setCmd) |
| 86 | + registerSetTimeoutCmd(ctx, setCmd) |
83 | 87 | configCmd.AddCommand(setCmd)
|
84 | 88 | }
|
85 | 89 |
|
@@ -190,6 +194,102 @@ func setInteractiveFunc(ctx *context.Context, _ *cobra.Command, args []string) {
|
190 | 194 | logger.PlainTextSuccess("Interactive UI " + strVal)
|
191 | 195 | }
|
192 | 196 |
|
| 197 | +func registerSetNotificationsCmd(ctx *context.Context, setCmd *cobra.Command) { |
| 198 | + notificationsCmd := &cobra.Command{ |
| 199 | + Use: "notifications [true|false]", |
| 200 | + Short: "Enable or disable notifications.", |
| 201 | + Args: cobra.ExactArgs(1), |
| 202 | + ValidArgs: []string{"true", "false"}, |
| 203 | + Run: func(cmd *cobra.Command, args []string) { setNotificationsFunc(ctx, cmd, args) }, |
| 204 | + } |
| 205 | + RegisterFlag(ctx, notificationsCmd, *flags.SetSoundNotificationFlag) |
| 206 | + setCmd.AddCommand(notificationsCmd) |
| 207 | +} |
| 208 | + |
| 209 | +func setNotificationsFunc(ctx *context.Context, cmd *cobra.Command, args []string) { |
| 210 | + logger := ctx.Logger |
| 211 | + enabled, err := strconv.ParseBool(args[0]) |
| 212 | + if err != nil { |
| 213 | + logger.FatalErr(errors.Wrap(err, "invalid boolean value")) |
| 214 | + } |
| 215 | + sound := flags.ValueFor[bool](ctx, cmd, *flags.SetSoundNotificationFlag, false) |
| 216 | + |
| 217 | + userConfig := ctx.Config |
| 218 | + if userConfig.Interactive == nil { |
| 219 | + userConfig.Interactive = &config.Interactive{} |
| 220 | + } |
| 221 | + userConfig.Interactive.NotifyOnCompletion = &enabled |
| 222 | + if sound { |
| 223 | + userConfig.Interactive.SoundOnCompletion = &enabled |
| 224 | + } |
| 225 | + if err := filesystem.WriteConfig(userConfig); err != nil { |
| 226 | + logger.FatalErr(err) |
| 227 | + } |
| 228 | + strVal := "disabled" |
| 229 | + if enabled { |
| 230 | + strVal = "enabled" |
| 231 | + } |
| 232 | + logger.PlainTextSuccess("Notifications " + strVal) |
| 233 | +} |
| 234 | + |
| 235 | +func registerSetThemeCmd(ctx *context.Context, setCmd *cobra.Command) { |
| 236 | + themeCmd := &cobra.Command{ |
| 237 | + Use: "theme [default|dark|light|dracula|tokyo-night]", |
| 238 | + Short: "Set the theme for the TUI views", |
| 239 | + Args: cobra.ExactArgs(1), |
| 240 | + ValidArgs: []string{ |
| 241 | + string(config.ConfigThemeDefault), |
| 242 | + string(config.ConfigThemeDark), |
| 243 | + string(config.ConfigThemeLight), |
| 244 | + string(config.ConfigThemeDracula), |
| 245 | + string(config.ConfigThemeTokyoNight), |
| 246 | + }, |
| 247 | + Run: func(cmd *cobra.Command, args []string) { setThemeFunc(ctx, cmd, args) }, |
| 248 | + } |
| 249 | + setCmd.AddCommand(themeCmd) |
| 250 | +} |
| 251 | + |
| 252 | +func setThemeFunc(ctx *context.Context, _ *cobra.Command, args []string) { |
| 253 | + logger := ctx.Logger |
| 254 | + themeName := args[0] |
| 255 | + |
| 256 | + userConfig := ctx.Config |
| 257 | + if userConfig.Interactive == nil { |
| 258 | + userConfig.Interactive = &config.Interactive{} |
| 259 | + } |
| 260 | + userConfig.Theme = config.ConfigTheme(themeName) |
| 261 | + if err := filesystem.WriteConfig(userConfig); err != nil { |
| 262 | + logger.FatalErr(err) |
| 263 | + } |
| 264 | + logger.PlainTextSuccess("Theme set to " + themeName) |
| 265 | +} |
| 266 | + |
| 267 | +func registerSetTimeoutCmd(ctx *context.Context, setCmd *cobra.Command) { |
| 268 | + timeoutCmd := &cobra.Command{ |
| 269 | + Use: "timeout DURATION", |
| 270 | + Short: "Set the default timeout for executables.", |
| 271 | + Args: cobra.ExactArgs(1), |
| 272 | + Run: func(cmd *cobra.Command, args []string) { setTimeoutFunc(ctx, cmd, args) }, |
| 273 | + } |
| 274 | + setCmd.AddCommand(timeoutCmd) |
| 275 | +} |
| 276 | + |
| 277 | +func setTimeoutFunc(ctx *context.Context, _ *cobra.Command, args []string) { |
| 278 | + logger := ctx.Logger |
| 279 | + timeoutStr := args[0] |
| 280 | + timeout, err := time.ParseDuration(timeoutStr) |
| 281 | + if err != nil { |
| 282 | + logger.FatalErr(errors.Wrap(err, "invalid duration")) |
| 283 | + } |
| 284 | + |
| 285 | + userConfig := ctx.Config |
| 286 | + userConfig.DefaultTimeout = timeout |
| 287 | + if err := filesystem.WriteConfig(userConfig); err != nil { |
| 288 | + logger.FatalErr(err) |
| 289 | + } |
| 290 | + logger.PlainTextSuccess("Default timeout set to " + timeoutStr) |
| 291 | +} |
| 292 | + |
193 | 293 | func registerViewConfigCmd(ctx *context.Context, configCmd *cobra.Command) {
|
194 | 294 | viewCmd := &cobra.Command{
|
195 | 295 | Use: "view",
|
|
0 commit comments