Skip to content

Commit a869a15

Browse files
committed
R: line endings and cleanup
1 parent 8dfead5 commit a869a15

File tree

8 files changed

+64
-36
lines changed

8 files changed

+64
-36
lines changed

samples/RenderingPlayground/Program.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,36 @@ public static void Main(
4343
height ?? Console.WindowHeight,
4444
overwrite);
4545

46+
var console = invocationContext.Console;
47+
4648
if (overwrite &&
47-
invocationContext.Console is ITerminal terminal)
49+
console is ITerminal terminal)
4850
{
4951
terminal.Clear();
5052
}
5153

5254
var consoleRenderer = new ConsoleRenderer(
53-
invocationContext.Console,
55+
console,
5456
mode: invocationContext.BindingContext.OutputMode(),
5557
resetAfterRender: true);
5658

5759
switch (sample)
5860
{
5961
case SampleName.Colors:
6062
{
61-
var screen = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
63+
var screen = new ScreenView(renderer: consoleRenderer, console);
6264
screen.Child = new ColorsView(text ?? "*");
6365

6466
screen.Render(region);
6567
}
6668
break;
6769

6870
case SampleName.Dir:
69-
var directoryTableView = new DirectoryTableView(new DirectoryInfo(Directory.GetCurrentDirectory()));
70-
directoryTableView.Render(consoleRenderer, region);
71+
72+
var directoryTableView = new DirectoryTableView(
73+
new DirectoryInfo(Directory.GetCurrentDirectory()));
74+
75+
console.Append(directoryTableView);
7176

7277
break;
7378

@@ -94,14 +99,14 @@ public static void Main(
9499
table.AddColumn(process => $"{process.ProcessName} ", "Name");
95100
table.AddColumn(process => ContentView.FromObservable(process.TrackCpuUsage(), x => $"{x.UsageTotal:P}"), "CPU", ColumnDefinition.Star(1));
96101

97-
var screen = new ScreenView(renderer: consoleRenderer, invocationContext.Console) { Child = table };
102+
var screen = new ScreenView(renderer: consoleRenderer, console) { Child = table };
98103
screen.Render();
99104
}
100105
break;
101106

102107
case SampleName.Clock:
103108
{
104-
var screen = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
109+
var screen = new ScreenView(renderer: consoleRenderer, console);
105110
var lastTime = DateTime.Now;
106111
var clockObservable = new BehaviorSubject<DateTime>(lastTime);
107112
var clockView = ContentView.FromObservable(clockObservable, x => $"{x:T}");
@@ -121,7 +126,7 @@ public static void Main(
121126

122127
case SampleName.GridLayout:
123128
{
124-
var screen = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
129+
var screen = new ScreenView(renderer: consoleRenderer, console);
125130
var content = new ContentView(
126131
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum for Kevin.");
127132
var smallContent = new ContentView("Kevin Bost");
@@ -159,7 +164,7 @@ public static void Main(
159164
}
160165
else
161166
{
162-
var screen = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
167+
var screen = new ScreenView(renderer: consoleRenderer, console);
163168
var stackLayout = new StackLayoutView();
164169
var content1 = new ContentView("Hello World!");
165170
var content2 = new ContentView(

src/System.CommandLine.Rendering/AnsiRenderingSpanVisitor.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5+
using System.CommandLine.IO;
56
using static System.CommandLine.Rendering.Ansi;
67

78
namespace System.CommandLine.Rendering
@@ -16,11 +17,20 @@ public AnsiRenderingSpanVisitor(
1617

1718
protected override void SetCursorPosition(int? left = null, int? top = null)
1819
{
19-
Writer.Write(
20-
Cursor.Move
21-
.ToLocation(left: left + 1, top: top + 1)
22-
.EscapeSequence);
23-
20+
if (Region == Region.Scrolling)
21+
{
22+
Writer.WriteLine(
23+
Cursor.Move
24+
.ToLocation(left: left + 1)
25+
.EscapeSequence);
26+
}
27+
else
28+
{
29+
Writer.Write(
30+
Cursor.Move
31+
.ToLocation(left: left + 1, top: top + 1)
32+
.EscapeSequence);
33+
}
2434
}
2535

2636
public override void VisitForegroundColorSpan(ForegroundColorSpan span)
@@ -62,6 +72,7 @@ public override void VisitStyleSpan(StyleSpan span)
6272
Writer.Write(controlCode.EscapeSequence);
6373
}
6474
}
75+
6576
public override void VisitCursorControlSpan(CursorControlSpan cursorControlSpan)
6677
{
6778
if (_styleControlCodeMappings.TryGetValue(cursorControlSpan.Name, out var controlCode))
@@ -130,4 +141,4 @@ public override void VisitCursorControlSpan(CursorControlSpan cursorControlSpan)
130141
[nameof(StyleSpan.UnderlinedOn)] = Ansi.Text.UnderlinedOn,
131142
};
132143
}
133-
}
144+
}

src/System.CommandLine.Rendering/EntireTerminalRegion.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,4 @@ public EntireTerminalRegion() : base(0, 0, Console.WindowWidth, Console.WindowHe
1717

1818
public override int Left => Console.CursorLeft;
1919
}
20-
21-
internal class ScrollingTerminalRegion : Region
22-
{
23-
public ScrollingTerminalRegion() : base(0, 0, Console.WindowWidth, Console.WindowHeight, false)
24-
{
25-
}
26-
27-
public override int Height => int.MaxValue;
28-
29-
public override int Width => Console.WindowWidth;
30-
31-
public override int Top => Console.CursorTop;
32-
33-
public override int Left => Console.CursorLeft;
34-
}
3520
}

src/System.CommandLine.Rendering/FileRenderingSpanVisitor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace System.CommandLine.Rendering
77
{
88
internal class FileRenderingSpanVisitor : ContentRenderingSpanVisitor
99
{
10-
// TODO: (FileRenderingSpanVisitor) rename: PlainTextRenderingSpanVisitor?
1110
public FileRenderingSpanVisitor(
1211
IStandardStreamWriter writer,
1312
Region region) : base(writer, region)

src/System.CommandLine.Rendering/Region.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class Region
1212
public Region(
1313
int left,
1414
int top,
15-
int width,
16-
int height,
15+
int? width = null,
16+
int? height = null,
1717
bool isOverwrittenOnRender = true)
1818
{
1919
if (height < 0)
@@ -36,8 +36,14 @@ public Region(
3636
throw new ArgumentOutOfRangeException(nameof(left));
3737
}
3838

39-
Height = height;
40-
Width = width;
39+
Height = height ??
40+
(Console.IsOutputRedirected
41+
? 100
42+
: Console.WindowHeight);
43+
Width = width ??
44+
(Console.IsOutputRedirected
45+
? 100
46+
: Console.WindowWidth);
4147
Top = top;
4248
Left = left;
4349

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace System.CommandLine.Rendering
5+
{
6+
internal class ScrollingTerminalRegion : Region
7+
{
8+
public ScrollingTerminalRegion() : base(0, 0, isOverwrittenOnRender: false)
9+
{
10+
}
11+
12+
public override int Height => int.MaxValue;
13+
14+
public override int Width => Console.WindowWidth;
15+
16+
public override int Top => Console.CursorTop;
17+
18+
public override int Left => Console.CursorLeft;
19+
}
20+
}

src/System.CommandLine.Tests/CommandTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.CommandLine.Binding;
5+
using System.CommandLine.Invocation;
46
using System.CommandLine.Parsing;
57
using FluentAssertions;
68
using System.Linq;

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ public CollectionWithCustomTypeConverter(IEnumerable<string> values)
19981998

19991999
}
20002000
}
2001-
2001+
20022002
public class CustomCollectionTypeConverter : TypeConverter
20032003
{
20042004
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

0 commit comments

Comments
 (0)