From fbb570c5f308b589b0cde4e652fda7503b247ff0 Mon Sep 17 00:00:00 2001 From: Kevin Lyda <kevin@lyda.ie> Date: Fri, 2 Dec 2022 16:10:13 +0000 Subject: [PATCH] Show errors in the i3 status bar. --- bar/config.go | 8 +++---- bar/run.go | 3 +-- cmd/root.go | 4 +++- config/config.go | 30 +++++++++++++++---------- modules/error.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++ modules/modules.go | 2 +- 6 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 modules/error.go diff --git a/bar/config.go b/bar/config.go index 606a8d5..722412f 100644 --- a/bar/config.go +++ b/bar/config.go @@ -3,15 +3,13 @@ package bar import ( - "github.com/spf13/cobra" "gitlab.ie.suberic.net/kevin/i3going-on/config" ) -func readConfig(configFile string) (*config.Config, error) { - cfg, err := config.ReadConfig(configFile) - cobra.CheckErr(err) +func readConfig(configFile string) *config.Config { + cfg := config.ReadConfig(configFile) if cfg.Refresh < 1 { cfg.Refresh = 5 } - return cfg, err + return cfg } diff --git a/bar/run.go b/bar/run.go index 6d653fa..6c457c2 100644 --- a/bar/run.go +++ b/bar/run.go @@ -16,8 +16,7 @@ import ( // Run is essentially the main program. func Run(cmd *cobra.Command, args []string) { viper.BindPFlags(cmd.Flags()) - cfg, err := readConfig(viper.GetString("config")) - cobra.CheckErr(err) + cfg := readConfig(viper.GetString("config")) clicks := make(chan modules.Click, 1) diff --git a/cmd/root.go b/cmd/root.go index cd19a3a..fd562ab 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,9 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) home, err := os.UserHomeDir() - cobra.CheckErr(err) + if err != nil { + home = "/" + } defaultConfig := path.Join(home, ".config", "i3going-on", "config.yaml") rootCmd.Flags().StringP("config", "c", defaultConfig, "config file") } diff --git a/config/config.go b/config/config.go index 1ba94b7..bb06e38 100644 --- a/config/config.go +++ b/config/config.go @@ -2,7 +2,6 @@ package config import ( "os" - "strings" "gitlab.ie.suberic.net/kevin/i3going-on/modules" "gopkg.in/yaml.v3" @@ -14,23 +13,30 @@ type Config struct { Modules []modules.Module `yaml:"modules"` } -// ReadConfigStr reads in the config. -func ReadConfigStr(configStr string) (*Config, error) { - configParser := yaml.NewDecoder(strings.NewReader(configStr)) - cfg := &Config{} - err := configParser.Decode(&cfg) - return cfg, err -} - // ReadConfig reads in the config. -func ReadConfig(configFile string) (*Config, error) { +func ReadConfig(configFile string) *Config { config, err := os.Open(configFile) if err != nil { - return nil, err + return &Config{ + Refresh: 5, + Modules: []modules.Module{ + modules.NewErrorModule("open", err), + }, + } } + defer config.Close() configParser := yaml.NewDecoder(config) cfg := &Config{} err = configParser.Decode(&cfg) - return cfg, err + if err != nil { + return &Config{ + Refresh: 5, + Modules: []modules.Module{ + modules.NewErrorModule("decode", err), + }, + } + } + + return cfg } diff --git a/modules/error.go b/modules/error.go new file mode 100644 index 0000000..2213863 --- /dev/null +++ b/modules/error.go @@ -0,0 +1,55 @@ +// Package module implements all the modules. +// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie> +package modules + +import ( + "fmt" +) + +// ErrorMod module parameters for the text module. +type ErrorMod struct { + name string + onclick string + Error string +} + +// NewError creates a ErrorMod instance. +func NewError(m *Module) *ErrorMod { + e := &ErrorMod{ + name: m.Name, + onclick: m.OnClick, + } + if e.name == "" { + e.name = "error" + } + return e +} + +// Name returns the name setting for the module. +func (e *ErrorMod) Name() string { + return e.name +} + +// OnClick returns the on-click setting for the module. +func (e *ErrorMod) OnClick() string { + return e.onclick +} + +// Render renders the module. +func (e *ErrorMod) Render() string { + // TODO: Make color and name optional. + return fmt.Sprintf(`{"name": "%s", "full_text": "%s", "color": "#ff1111"}`, e.name, e.Error) +} + +// NewErrorModule creates a Module instance. +func NewErrorModule(name string, errtxt error) Module { + return Module{ + Module: "error", + Name: "decode", + OnClick: "", + Params: &ErrorMod{ + name: "open", + Error: errtxt.Error(), + }, + } +} diff --git a/modules/modules.go b/modules/modules.go index aa9cdd6..e66bdcb 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -41,7 +41,7 @@ func (m *Module) UnmarshalYAML(node *yaml.Node) error { } if strings.ContainsAny(m.Name, "{}") { - return fmt.Errorf("module name field can't contain '{' or '}'.") + return fmt.Errorf("module name field can't contain '{' or '}' characters") } switch params.Module { case "date": -- GitLab