Skip to content
Snippets Groups Projects
Commit 590d0dcb authored by Jiří Kalvoda's avatar Jiří Kalvoda
Browse files

Module: File and OsddLast

parent 6fb520bf
No related branches found
No related tags found
No related merge requests found
......@@ -97,6 +97,91 @@ namespace i3csstatus {
return new Element[]{new Element(text, color: color)};
}
}
[ModuleName("file")]
class ModuleFile: Module
{
string path;
protected Color color;
InnerStatusBar ifNotFound;
InnerStatusBar ifReadError;
public void Init(StatusBar _bar, ConfigParser config, ConfigSection section)
{
path = section.Mandatory("path").AsPath();
color = section.Optional("color")?.AsColor() ?? System.Drawing.ColorTranslator.FromHtml("white");
ifNotFound = new InnerStatusBar(_bar, section.Optional("not_found")?.AsConfig() ??
section.Optional("read_error")?.AsConfig() ??
new ConfigParser(
@"
[constant]
color = red
text = NFound
"));
ifReadError = new InnerStatusBar(_bar, section.Optional("read_error")?.AsConfig() ??
new ConfigParser(
@"
[constant]
color = red
text = ERR
"));
}
protected virtual IEnumerable<Element> Parse(string text)
{
if(text.Length > 0 && text[^1] == '\n') text = text[..^1];
return text.Split("\n").Select(x => new Element(x, color: color)).ToArray();
}
public IEnumerable<Element> Get()
{
string text;
try
{
text = File.ReadAllText(path);
}
catch (Exception ex) when (ex is FileNotFoundException || ex is DirectoryNotFoundException)
{
return ifNotFound.Get();
}
catch (Exception)
{
return ifReadError.Get();
}
return Parse(text);
}
}
[ModuleName("osdd_last")]
class ModuleOsddLast: ModuleFile
{
public new void Init(StatusBar _bar, ConfigParser config, ConfigSection section)
{
base.Init(_bar, config, section);
}
static string timeShow(long t_s)
{
if(t_s<180) return $"{t_s}s";
if(t_s<180*60) return $"{t_s/60}min";
return $"{t_s/60/60}h";
}
protected override IEnumerable<Element> Parse(string text)
{
try
{
var lines = text.Split("\n");
List<Element> output = new();
long timeEleapsed_s = DateTimeOffset.Now.ToUnixTimeSeconds()-long.Parse(lines[0]);
bool first = true;
foreach(var l in lines[1..])
{
if(l.Length > 7)
output.Add(new Element((first?$"[{timeShow(timeEleapsed_s)}] ":"") + l[7..], color: System.Drawing.ColorTranslator.FromHtml("#"+l[..6])));
first = false;
}
return output;
}
catch(Exception)
{
return new Element[]{new Element("OSDD parse ERROR", color: Color.Red)};
}
}
}
[ModuleName("i3status")]
class ModuleI3Status: Module
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment