From 4447138c3ee166735149e6af5268b168070595ec Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz> Date: Sat, 13 Aug 2022 12:00:42 +0200 Subject: [PATCH] Using ConfigSection.OptionalDefaultIsSectionName --- Module.cs | 8 ++++---- Parsers.cs | 2 +- Program.cs | 10 ++++------ Wrappers.cs | 3 +++ sample.conf | 4 +++- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Module.cs b/Module.cs index a30a0d2..1234eea 100644 --- a/Module.cs +++ b/Module.cs @@ -112,7 +112,7 @@ class ModuleConstant: Module string text; public void Init(ModuleParent _bar, ConfigSection section) { - text = section.Mandatory("text"); + text = section.OptionalDefaultIsSectionName("text").AsString(); } public IEnumerable<Block> Get() { @@ -146,7 +146,7 @@ text = NFound _color = red text = ERR ")); - parser = _bar.GetGlobal<ParserGetter>().ByNameFromConfig(section.Optional("parser")); + parser = _bar.GetGlobal<ParserGetter>().ByNameFromConfig(section.OptionalDefaultIsSectionName("parser")); parser.Init(_bar, this, section); } public IEnumerable<Block> Get() @@ -191,7 +191,7 @@ abstract class ModuleSourceThreadAndParser: Module scheudleIn_ms = section.Optional("delay")?.AsMs() ?? 10; showOld_ms = section.Optional("show_old")?.AsMs(); maxOld_ms = section.Optional("max_old")?.AsMs(); - parser = _bar.GetGlobal<ParserGetter>().ByNameFromConfig(section.Optional("parser")); + parser = _bar.GetGlobal<ParserGetter>().ByNameFromConfig(section.OptionalDefaultIsSectionName("parser")); parser.Init(_bar, this, section); inputThread = new Thread(this.inputThreadFunc); @@ -493,7 +493,7 @@ class ModuleI3Status: Module } public void Init(ModuleParent _bar, ConfigSection section) { - name = section["name"]; + name = section.OptionalDefaultIsSectionName("name").AsString(); configuration = section.Optional("config")?.AsString(); var g = _bar.GetGlobal<Global>(); g.modules.Add(this); diff --git a/Parsers.cs b/Parsers.cs index 649ded0..fb514e7 100644 --- a/Parsers.cs +++ b/Parsers.cs @@ -88,7 +88,7 @@ class ParserGetter: GlobalModuleResource if(val == null) return new ParserText(); var p = ByName(val.AsString()); if(p == null) - throw new ConfigMistake(val, $"Parser with name {val.AsString()} not exists."); + throw new ConfigMistake(val, $"Parser with name \"{val.AsString()}\" not exists."); return p; } diff --git a/Program.cs b/Program.cs index 72ce47d..edc16a9 100644 --- a/Program.cs +++ b/Program.cs @@ -243,12 +243,10 @@ refresh foreach(var s in p) { - #pragma warning disable 8600 - string type = s["_type"] ?? s.SectionName; - #pragma warning restore 8600 - #pragma warning disable 8604 - var constructor = moduleTypes[type].GetConstructor(constructorSignature); - #pragma warning restore 8604 + ConfigValue type = s.OptionalDefaultIsSectionName("_type"); + if(!moduleTypes.ContainsKey(type.AsString())) + throw new ConfigMistake(type, $"Module type with name \"{type.AsString()}\" not exists."); + var constructor = moduleTypes[type.AsString()].GetConstructor(constructorSignature); if(constructor == null) throw new Exception($"Missing constructor of {type} module"); Module module = (Module) constructor.Invoke(new object[]{}); diff --git a/Wrappers.cs b/Wrappers.cs index 743c094..443994a 100644 --- a/Wrappers.cs +++ b/Wrappers.cs @@ -40,6 +40,9 @@ abstract partial class StatusBar if(section.Contains("_color")) module = new WrapperColor(module, section.Mandatory("_color").AsColor()); + if(section.Contains("_exec")) + module = new WrapperExec(module, section.Mandatory("_exec").AsString(), MouseButton.Left, 0); + module.Init(parrent, section); return module; } diff --git a/sample.conf b/sample.conf index 44c8726..611f432 100644 --- a/sample.conf +++ b/sample.conf @@ -112,7 +112,7 @@ refresh = 1 # Note that only one i3status instance is running for the whole status bar. # Due to technical limitations of i3status the `name` option must be unique. name=ethernet _first_ -# mandatory <string> +# <string> default is section name config format_up = "E: %ip (%speed)" format_down = "E: down" @@ -176,6 +176,7 @@ short_format = {percent} [constant] # Just show some text text = Hi! +# <string> default is section name [file] @@ -311,6 +312,7 @@ _default_color = red # Some of modules use parsers. It is part of module that is responsible for # processing text oriented message to a part of the status bar. # You can specify which parser you would like to use by `parser` option. +# If `parser` option is not defined, section name will be used as parse name. # Parsers can specify extra options. # Parser: text -- GitLab