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

better terminal and plain-rext outputs

parent 614f5697
Branches
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ using System.Text.Json;
using System.Text.Json.Nodes;
using System.Linq;
using System.Threading;
using StringInfo = System.Globalization.StringInfo;
using System.CommandLine;
using System.Runtime.InteropServices;
......@@ -242,23 +243,59 @@ abstract class RootStatusBar: StatusBar
class StatusBarPlainText: RootStatusBar
{
public static string RemoveMarkup(string s, Markup m)
{
if(m == Markup.Pango)
{
bool inTag = new();
StringBuilder res = new();
foreach(char c in s)
{
if(c == '<') inTag = true;
if(!inTag) res.Append(c);
if(c == '>') inTag = false;
}
return res.ToString();
}
return s;
}
protected virtual string elementAsString(Element e)
{
string s = RemoveMarkup(e.Text, e.Markup);
if(e.MinWidth_string != null || e.MinWidth_int != null)
{
int minWidth = 0;
if(e.MinWidth_string != null)
minWidth = new StringInfo(e.MinWidth_string).LengthInTextElements;
if(e.MinWidth_int != null)
minWidth = (int)e.MinWidth_int/6;
int current = new StringInfo(s).LengthInTextElements;
if(current < minWidth)
{
if(e.Align == Align.Left) s = s + new String(' ', minWidth-current);
if(e.Align == Align.Right) s = new String(' ', minWidth-current) + s;
if(e.Align == Align.Center) s = new String(' ', (minWidth-current+1)/2) + s + new String(' ', (minWidth-current)/2);
}
}
return s;
}
public StatusBarPlainText(FileInfo configFile):base(configFile){}
override protected void format(TextWriter w, List<Element> elements)
{
bool first = true;
Element? last = null;
foreach(var e in elements)
{
if(!first)
w.Write("|");
first = false;
w.Write(e.Text);
if(last != null)
w.Write(last.Separator?"|":" ");
w.Write(elementAsString(e));
last = e;
}
w.WriteLine("");
}
}
class StatusBarTerminal: RootStatusBar
class StatusBarTerminal: StatusBarPlainText
{
Thread? inputThread;
public StatusBarTerminal(FileInfo configFile, bool doInput):base(configFile)
......@@ -283,15 +320,23 @@ class StatusBarTerminal: RootStatusBar
Console.WriteLine("");
}
}
override protected string elementAsString(Element e)
{
string msg = base.elementAsString(e);
if(e.Urgent) msg=$"\x1B[1m{msg}\x1B[0m";
if(e.Color != null) msg = msg.Pastel(e.Color.Value);
if(e.BackgroundColor != null) msg = msg.PastelBg(e.BackgroundColor.Value);
return msg;
}
override protected void format(TextWriter w, List<Element> elements)
{
bool first = true;
Element? last = null;
foreach(var e in elements)
{
if(!first)
w.Write("|".Pastel(Color.FromArgb(165, 229, 250)));
first = false;
w.Write(e.Color!=null?e.Text.Pastel(e.Color.Value):e.Text);
if(last != null)
w.Write((last.Separator?"|":" ").Pastel(Color.FromArgb(165, 229, 250)));
w.Write(elementAsString(e));
last = e;
}
w.WriteLine("");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment