public void TSF_Text() { var tsf = new TextSpanFormatter(); tsf.Write("hello"); Assert.AreEqual("hello" + nl, Flatten(tsf)); }
public void When_formatting_null_values_then_empty_span_is_returned() { var formatter = new TextSpanFormatter(); var span = formatter.Format(null); span.Should().Be(TextSpan.Empty()); }
public void TSF_Link() { var tsf = new TextSpanFormatter(); tsf.Write("go to "); tsf.WriteHyperlink("Hell", "Aitch-ee-double-hockeysticks"); Assert.AreEqual("go to _Hell_" + nl, Flatten(tsf)); }
internal static ContentView Create(object content, TextSpanFormatter formatter) { if (content == null) { return(new ContentView(TextSpan.Empty())); } return(CreateView((dynamic)content, formatter)); }
public void ToString_with_non_ansi_omits_ANSI_codes() { var span = new TextSpanFormatter().ParseToSpan( $"one{ForegroundColorSpan.Red()}two{ForegroundColorSpan.Reset()}three" ); span.ToString(OutputMode.NonAnsi).Should().Be("onetwothree"); }
public void ToString_with_ansi_includes_ANSI_codes() { var span = new TextSpanFormatter() .ParseToSpan($"one{ForegroundColorSpan.Red()}two{ForegroundColorSpan.Reset()}three"); span.ToString(OutputMode.Ansi) .Should() .Be($"one{Ansi.Color.Foreground.Red.EscapeSequence}two{Ansi.Color.Foreground.Default.EscapeSequence}three"); }
private TextViewModel CreateNode(params string[] lines) { var fmt = new TextSpanFormatter(); foreach (var line in lines) { fmt.WriteLine(line); } return(fmt.GetModel()); }
private void EnsureLines() { if (lines != null) { return; } var tsf = new TextSpanFormatter(); WriteCode(tsf); lines = tsf.GetLines(); }
public void A_simple_formattable_string_can_be_converted_to_a_ContentSpan() { var span = new TextSpanFormatter().ParseToSpan($"some text"); span.Should() .BeOfType <ContentSpan>() .Which .Content .Should() .Be("some text"); }
public void Ansi_control_codes_can_be_included_in_interpolated_strings() { var writer = new StringWriter(); var formatter = new TextSpanFormatter(); var span = formatter.ParseToSpan($"{Ansi.Color.Foreground.LightGray}hello{Ansi.Color.Off}"); writer.Write(span.ToString(OutputMode.Ansi)); writer.ToString() .Should() .Be($"{Ansi.Color.Foreground.LightGray.EscapeSequence}hello{Ansi.Color.Off.EscapeSequence}"); }
private string Flatten(TextSpanFormatter tsf) { var model = tsf.GetModel(); var sb = new StringBuilder(); var lines = model.GetLineSpans(model.LineCount); foreach (var line in lines) { foreach (var span in line.TextSpans) { EmitSpanWrapper(span, sb); sb.Append(span.GetText()); EmitSpanWrapper(span, sb); } sb.AppendLine(); } return(sb.ToString()); }
public DirectoryView(DirectoryInfo directory) { if (directory == null) { throw new ArgumentNullException(nameof(directory)); } var formatter = new TextSpanFormatter(); formatter.AddFormatter <DateTime>(d => $"{d:d} {ForegroundColorSpan.DarkGray()}{d:t}"); Add(new ContentView("")); Add(new ContentView("")); Add(new ContentView($"Directory: {directory.FullName}")); Add(new ContentView("")); Add(new ContentView("")); var directoryContents = directory.EnumerateFileSystemInfos() .OrderBy(f => f is DirectoryInfo ? 0 : 1).ToList(); var tableView = new TableView <FileSystemInfo>(); tableView.Items = directoryContents; tableView.AddColumn(f => f is DirectoryInfo ? Span($"{ForegroundColorSpan.LightGreen()}{f.Name} ") : Span($"{ForegroundColorSpan.White()}{f.Name} "), new ContentView(formatter.ParseToSpan($"{Ansi.Text.UnderlinedOn}Name{Ansi.Text.UnderlinedOff}"))); tableView.AddColumn(f => formatter.Format(f.CreationTime), new ContentView(formatter.ParseToSpan($"{Ansi.Text.UnderlinedOn}Created{Ansi.Text.UnderlinedOff}"))); tableView.AddColumn(f => formatter.Format(f.LastWriteTime), new ContentView(formatter.ParseToSpan($"{Ansi.Text.UnderlinedOn}Modified{Ansi.Text.UnderlinedOff}"))); Add(tableView); TextSpan Span(FormattableString formattableString) { return(formatter.ParseToSpan(formattableString)); } }
public ProcessesView(Process[] processes) { var formatter = new TextSpanFormatter(); formatter.AddFormatter <TimeSpan>(t => new ContentSpan(t.ToString(@"hh\:mm\:ss"))); Add(new ContentView("")); Add(new ContentView("Processes")); Add(new ContentView("")); var table = new TableView <Process> { Items = processes }; table.AddColumn(p => p.Id, new ContentView("PID".Underline())); table.AddColumn(p => Name(p), new ContentView("COMMAND".Underline())); table.AddColumn(p => p.PrivilegedProcessorTime, new ContentView("TIME".Underline())); table.AddColumn(p => p.Threads.Count, new ContentView("#TH".Underline())); table.AddColumn( p => p.PrivateMemorySize64.Abbreviate(), new ContentView("MEM".Underline()) ); table.AddColumn( p => { #pragma warning disable CS0618 // Type or member is obsolete var usage = p.TrackCpuUsage().First(); #pragma warning restore CS0618 // Type or member is obsolete return($"{usage.UsageTotal:P}"); }, new ContentView("CPU".Underline()) ); Add(table); FormattableString Name(Process p) { if (!p.Responding) { return($"{ForegroundColorSpan.Rgb(180, 0, 0)}{p.ProcessName}{ForegroundColorSpan.Reset()}"); } return($"{p.ProcessName}"); } }
private TextViewModel GenerateSimulatedHllCode() { var code = new List <AbsynStatement>(); var ase = new Structure.AbsynStatementEmitter(code); var a_id = new Identifier("a", PrimitiveType.Int32, null); var sum_id = new Identifier("sum", PrimitiveType.Int32, null); ase.EmitAssign(a_id, Constant.Int32(10)); ase.EmitAssign(sum_id, Constant.Int32(0)); var tsf = new TextSpanFormatter(); var fmt = new AbsynCodeFormatter(tsf); fmt.InnerFormatter.UseTabs = false; foreach (var stm in code) { stm.Accept(fmt); } return(tsf.GetModel()); }
public EgretConsole(IConsole providedConsole, ILogger <EgretConsole> providedLogger) { logger = providedLogger; console = providedConsole; terminal = console.GetTerminal(preferVirtualTerminal: true, OutputMode.Auto); OutputMode = (terminal ?? console).DetectOutputMode(); formatter = new TextSpanFormatter(); renderer = new ConsoleRenderer(terminal ?? console, resetAfterRender: false); formatter.AddFormatter <FileInfo>((f) => f.FullName.StyleValue()); formatter.AddFormatter <double>((x) => x.ToString("N2").StyleNumber()); formatter.AddFormatter <int>((x) => x.ToString().StyleNumber()); var queue = new BlockingCollection <string>(); // created our own little async event loop so we can run things sequentially context = new AsyncContextThread(); }
public void A_formattable_string_containing_ansi_codes_can_be_converted_to_a_ContainerSpan() { var span = new TextSpanFormatter().ParseToSpan( $"some {StyleSpan.BlinkOn()}blinking{StyleSpan.BlinkOff()} text" ); var containerSpan = span.Should().BeOfType <ContainerSpan>().Subject; containerSpan .Should() .BeEquivalentTo( new ContainerSpan( new ContentSpan("some "), StyleSpan.BlinkOn(), new ContentSpan("blinking"), StyleSpan.BlinkOff(), new ContentSpan(" text") ), options => options.WithStrictOrdering().Excluding(s => s.Parent).Excluding(s => s.Root) ); }
public void Empty_strings_are_returned_as_empty_spans() { var formatter = new TextSpanFormatter(); var span = formatter.ParseToSpan( $"{Ansi.Color.Foreground.Red}normal{Ansi.Color.Foreground.Default:a}" ); var containerSpan = span.Should().BeOfType <ContainerSpan>().Subject; containerSpan .Should() .BeEquivalentTo( new ContainerSpan( TextSpan.Empty(), new ContentSpan("normal"), TextSpan.Empty() ), options => options.WithStrictOrdering().Excluding(s => s.Parent).Excluding(s => s.Root) ); }
public void FormattableString_parsing_handles_escapes( FormattableString fs, int expectedCount ) { var formatter = new TextSpanFormatter(); var span = formatter.ParseToSpan(fs); if (expectedCount > 1) { var containerSpan = span.Should().BeOfType <ContainerSpan>().Subject; output.WriteLine(containerSpan.ToString()); containerSpan.Count.Should().Be(expectedCount); } else { span.Should().BeOfType <ContentSpan>(); } }
private static ContentView CreateView(TextSpan span, TextSpanFormatter _) => new ContentView(span);
private static ContentView CreateView <T>(IObservable <T> observable, TextSpanFormatter _) => FromObservable(observable);
public void TSF_Empty() { var tsf = new TextSpanFormatter(); Assert.AreEqual("", Flatten(tsf)); }
static ExecuteRequestHandler() { _textSpanFormatter = new TextSpanFormatter(); }
private static ContentView CreateView(object value, TextSpanFormatter formatter) => new ContentView(formatter.Format(value));
private static ContentView CreateView(string stringContent, TextSpanFormatter _) => new(stringContent);
private TextViewModel GenerateSimulatedHllCode() { var code = new List<AbsynStatement>(); var ase = new Structure.AbsynStatementEmitter(code); var m = new ExpressionEmitter(); var a_id = new Identifier("a", PrimitiveType.Int32, null); var sum_id = new Identifier("sum", PrimitiveType.Int32, null); ase.EmitAssign(a_id, Constant.Int32(10)); ase.EmitAssign(sum_id, Constant.Int32(0)); var tsf = new TextSpanFormatter(); var fmt = new AbsynCodeFormatter(tsf); fmt.InnerFormatter.UseTabs = false; foreach (var stm in code) { stm.Accept(fmt); } return tsf.GetModel(); }