Skip to content

Commit df775c0

Browse files
committed
Cleaned up generics and handling INodes with generics.
User should only ever need to use .Call() at the top of a node tree.
1 parent 9e490b3 commit df775c0

File tree

10 files changed

+106
-96
lines changed

10 files changed

+106
-96
lines changed

ConsoleHero.InjectionExample/Data.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
using ConsoleHero.Interfaces;
22
using System.Drawing;
33
namespace ConsoleHero.InjectionExample;
4-
public record struct Fruit(string Name) : IMenuOption
5-
{
6-
public ColorText Print() => Name.DefaultColor();
7-
}
8-
public record struct ColorFruit(string Name, Color Color) : IMenuOption
9-
{
10-
public ColorText Print() => Name.Color(Color);
11-
}
12-
public record struct Number(int Value) : IMenuOption
13-
{
14-
public ColorText Print() => Value.ToString().DefaultColor();
15-
}
164

175
[Singleton]
186
public class Data
197
{
208
public string Name { get; set; } = "Person";
219

22-
public readonly List<Fruit> Fruit =
10+
public readonly List<string> Fruit =
2311
[
2412
new("Apple"),
2513
new("Banana"),
2614
new("Cantaloupe"),
2715
new("Artichoke"),
2816
];
2917

30-
public readonly List<ColorFruit> ColoredFruit =
18+
public readonly List<ColorText> ColoredFruit =
3119
[
32-
new("Apple",Color.Red),
33-
new("Banana",Color.Yellow),
34-
new("Cantaloupe", GlobalSettings.DefaultTextColor),
35-
new("Another Cantaloupe", Color.White),
36-
new("Artichoke", Color.Green),
20+
"Apple".Color(Color.Red),
21+
"Banana".Color(Color.Yellow),
22+
"Cantaloupe".DefaultColor(),
23+
"Another Cantaloupe".Color(Color.White),
24+
"Artichoke".Color(Color.Green),
3725
];
3826

39-
public readonly List<Number> Numbers =
27+
public readonly List<int> Numbers =
4028
[
41-
new(7000),
42-
new(8888),
43-
new(9001),
44-
new(55),
29+
7000,
30+
8888,
31+
9001,
32+
55,
4533
];
4634

4735
public readonly List<Player> Players =

ConsoleHero.InjectionExample/Menus.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ public class Menus(Paragraphs paragraphs, Requests requests, Tunes tunes, Data d
3434

3535
public Menu FruitMenu =>
3636
Title("|---- Fruit ----|", Color.Green).
37-
OptionsFromList(_data.Fruit, (x) => _paragraphs.Eat(x).Call()).
37+
OptionsFromList(_data.Fruit, _paragraphs.Eat).
3838
Cancel();
3939

4040
public Menu FruitMenuColored =>
4141
Title("|---- Fruit ----|", Color.Green).
42-
OptionsFromList(_data.ColoredFruit, (x) => _paragraphs.Eat(x).Call()).
42+
OptionsFromList(_data.ColoredFruit, _paragraphs.Eat).
4343
Cancel();
4444

4545
public Menu FruitMenuWithA =>
4646
Title("|---- Fruit ----|", Color.Green).
47-
OptionsFromList(_data.Fruit, (x) => _paragraphs.Eat(x).Call(), x => x.StartsWith('A')).
47+
OptionsFromList(_data.Fruit, _paragraphs.Eat, x => x.StartsWith('A')).
4848
Cancel();
4949

5050
public Menu NumberMenu =>
5151
NoTitle().
52-
OptionsFromList(_data.Numbers, (x) => _paragraphs.ReadNumbers(x).Call()).
52+
OptionsFromList(_data.Numbers, _paragraphs.ReadNumbers).
5353
Cancel();
5454

5555
public Menu ReviewPlayers =>

ConsoleHero.InjectionExample/Paragraphs.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ public class Paragraphs(Data data)
77
{
88
private readonly Data _data = data;
99

10-
public Paragraph Eat(ColorFruit fruit) =>
11-
Line("You just ate a ").Text(fruit.Name, fruit.Color).Text($", {_data.Name}.").
10+
public Paragraph Eat(ColorText fruit) =>
11+
Line("You just ate a ").Text(fruit).Text($", {_data.Name}.").
1212
PressToContinue();
1313

14-
public Paragraph Eat(Fruit fruit) =>
15-
Line($"You just ate a {fruit.Name}, {_data.Name}.").
14+
public Paragraph Eat(string fruit) =>
15+
Line($"You just ate a {fruit}, {_data.Name}.").
1616
PressToContinue();
1717

1818
public Paragraph Crying =>
1919
Line("You cry and cry!", Color.DarkBlue).
2020
PressToContinue();
2121

22-
public Paragraph ReadNumbers(Number n) =>
23-
Line($"You read the number {n.Value}.").
24-
Line($"Twice that number is {n.Value * 2}").Text(".").
22+
public Paragraph ReadNumbers(int n) =>
23+
Line($"You read the number {n}").
24+
Line($"Twice that number is {n * 2}").Text(".").
2525
Line("This is ").Text("red", Color.Red).Text(" Text.").
2626
PressToContinue();
2727

ConsoleHero.StaticExample/Data.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,33 @@
22
using System.Drawing;
33

44
namespace ConsoleHero.StaticExample;
5-
public record struct Fruit(string Name) : IMenuOption
6-
{
7-
public ColorText Print() => Name.DefaultColor();
8-
}
9-
public record struct ColorFruit(string Name, Color Color) : IMenuOption
10-
{
11-
public ColorText Print() => Name.Color(Color);
12-
}
13-
public record struct Number(int Value) : IMenuOption
14-
{
15-
public ColorText Print() => Value.ToString().DefaultColor();
16-
}
175
public static class Data
186
{
197
public static string Name { get; set; } = "Person";
208

21-
public static readonly List<Fruit> Fruit =
9+
public static readonly List<string> Fruit =
2210
[
23-
new("Apple"),
24-
new("Banana"),
25-
new("Cantaloupe"),
26-
new("Artichoke"),
11+
"Apple",
12+
"Banana",
13+
"Cantaloupe",
14+
"Artichoke",
2715
];
2816

29-
public static readonly List<ColorFruit> ColoredFruit =
17+
public static readonly List<ColorText> ColoredFruit =
3018
[
31-
new("Apple",Color.Red),
32-
new("Banana",Color.Yellow),
33-
new("Cantaloupe", GlobalSettings.DefaultTextColor),
34-
new("Another Cantaloupe", Color.White),
35-
new("Artichoke", Color.Green),
19+
"Apple".Color(Color.Red),
20+
"Banana".Color(Color.Yellow),
21+
"Cantaloupe".DefaultColor(),
22+
"Another Cantaloupe".Color(Color.White),
23+
"Artichoke".Color(Color.Green),
3624
];
3725

38-
public static readonly List<Number> Numbers =
26+
public static readonly List<int> Numbers =
3927
[
40-
new(7000),
41-
new(8888),
42-
new(9001),
43-
new(55),
28+
7000,
29+
8888,
30+
9001,
31+
55,
4432
];
4533

4634
public static readonly List<Player> Players =

ConsoleHero.StaticExample/Menus.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ public static class Menus
2727

2828
public static Menu FruitMenu =>
2929
Title("|---- Fruit ----|", Color.Green).
30-
OptionsFromList(Data.Fruit, (x) => Paragraphs.Eat(x).Call()).
30+
OptionsFromList(Data.Fruit, Paragraphs.Eat).
3131
Cancel();
3232

3333
public static Menu FruitMenuColored =>
3434
Title("|---- Fruit ----|", Color.Green).
35-
OptionsFromList(Data.ColoredFruit, (x) => Paragraphs.Eat(x).Call()).
35+
OptionsFromList(Data.ColoredFruit, Paragraphs.Eat).
3636
Cancel();
3737

3838
public static Menu FruitMenuWithA =>
3939
Title("|---- Fruit ----|", Color.Green).
40-
OptionsFromList(Data.Fruit, (x) => Paragraphs.Eat(x).Call(), x => x.StartsWith('A')).
40+
OptionsFromList(Data.Fruit, Paragraphs.Eat, x => x.StartsWith('A')).
4141
Cancel();
4242

4343
public static Menu NumberMenu =>
4444
NoTitle().
45-
OptionsFromList(Data.Numbers, (x) => Paragraphs.ReadNumbers(x).Call()).
45+
OptionsFromList(Data.Numbers, Paragraphs.ReadNumbers).
4646
Cancel();
4747

4848
public static Menu ReviewPlayers =>

ConsoleHero.StaticExample/Paragraphs.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
namespace ConsoleHero.StaticExample;
55
public static class Paragraphs
66
{
7-
public static Paragraph Eat(ColorFruit fruit) =>
8-
Line("You just ate a ").Text(fruit.Name, fruit.Color).Text($", {Data.Name}.").
7+
public static Paragraph Eat(ColorText fruit) =>
8+
Line("You just ate a ").Text(fruit).Text($", {Data.Name}.").
99
PressToContinue();
1010

11-
public static Paragraph Eat(Fruit fruit) =>
12-
Line($"You just ate a {fruit.Name}, {Data.Name}.").
11+
public static Paragraph Eat(string fruit) =>
12+
Line($"You just ate a {fruit}, {Data.Name}.").
1313
PressToContinue();
1414

1515
public static Paragraph Crying =>
1616
Line("You cry and cry!", Color.DarkBlue).
1717
PressToContinue();
1818

19-
public static Paragraph ReadNumbers(Number n) =>
20-
Line($"You read the number {n.Value}.").
21-
Line($"Twice that number is {n.Value * 2}").Text(".").
19+
public static Paragraph ReadNumbers(int n) =>
20+
Line($"You read the number {n}.").
21+
Line($"Twice that number is {n * 2}").Text(".").
2222
Line("This is ").Text("red", Color.Red).Text(" Text.").
2323
PressToContinue();
2424

ConsoleHero/ConsoleHero.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1010
<PackageId>ConsoleHero</PackageId>
11-
<Version>0.4.8</Version>
11+
<Version>0.5.0</Version>
1212
<Authors>Derek Gooding</Authors>
1313
<Description>A library for making quick menus in a console application.</Description>
1414
<PackageTags>Console CLI Menu Navigation FluentAPI UserInterface CommandLine Framework CSharp Tools</PackageTags>

ConsoleHero/ListExtensions.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,40 @@ internal static MenuOption[] ToOptions(this IEnumerable<ColorText> list, INode n
6969
internal static MenuOption[] ToOptions(this IEnumerable<string> list, INode node, Func<string, bool>? condition = null)
7070
=> list.ToOptions(node.Call, condition);
7171

72-
internal static MenuOption[] ToOptions<T>(this IEnumerable<T> list, Action<T> effect, Func<string, bool>? condition = null) where T : IMenuOption
72+
internal static MenuOption[] ToOptions<T>(this IEnumerable<T> list, Action<T> effect, Func<string, bool>? condition = null)
7373
{
7474
List<MenuOption> options = new();
75-
foreach (IMenuOption x in list)
75+
foreach (T x in list)
7676
{
77-
ColorText colorText = x.Print();
77+
ColorText colorText = x is IMenuOption iMenuOption ? iMenuOption.Print() : (x?.ToString() ?? string.Empty).DefaultColor();
7878
Color color = colorText.Color;
7979
MenuOption menuOption = new()
8080
{
8181
Description = colorText.Text,
8282
Color = color,
83-
Effect = () => effect((T)x)
83+
Effect = () => effect(x)
84+
};
85+
if (condition != null)
86+
{
87+
menuOption.Check = () => condition(colorText.Text);
88+
}
89+
options.Add(menuOption);
90+
}
91+
return options.ToArray();
92+
}
93+
94+
internal static MenuOption[] ToOptions<T>(this IEnumerable<T> list, Func<T, INode> effect, Func<string, bool>? condition = null)
95+
{
96+
List<MenuOption> options = new();
97+
foreach (T x in list)
98+
{
99+
ColorText colorText = x is IMenuOption iMenuOption ? iMenuOption.Print() : (x?.ToString() ?? string.Empty).DefaultColor();
100+
Color color = colorText.Color;
101+
MenuOption menuOption = new()
102+
{
103+
Description = colorText.Text,
104+
Color = color,
105+
Effect = () => effect(x).Call()
84106
};
85107
if (condition != null)
86108
{

ConsoleHero/MenuBuilder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public interface IAddOptions
7878
/// Creates a set of <see cref="MenuOption"/> base off IMenuOptions. Only works with custom GoTo logic.
7979
/// </summary>
8080
public IAddOptions OptionsFromList<T>(IEnumerable<T> list, Action<T> effect, Func<string, bool>? condition = null) where T : IMenuOption;
81+
82+
/// <summary>
83+
/// Creates a set of <see cref="MenuOption"/> base off IMenuOptions. Only works with custom GoTo logic that requires T as an input.
84+
/// </summary>
85+
public IAddOptions OptionsFromList<T>(IEnumerable<T> list, Func<T, INode> effect, Func<string, bool>? condition = null);
86+
8187
/// <summary>
8288
/// Creates a set of <see cref="MenuOption"/> based of a list.
8389
/// </summary>
@@ -249,7 +255,7 @@ public IAddOptions GoTo(Action action)
249255

250256
public IAddOptions GoTo(INode node)
251257
{
252-
_menuOption.Effect = () => node.Call();
258+
_menuOption.Effect = node.Call;
253259
_item.Add(_menuOption);
254260
_menuOption = new();
255261
return this;
@@ -263,6 +269,14 @@ public IAddOptions OptionsFromList<T>(IEnumerable<T> list, Action<T> effect, Fun
263269
}
264270
return this;
265271
}
272+
public IAddOptions OptionsFromList<T>(IEnumerable<T> list, Func<T, INode> effect, Func<string, bool>? condition = null)
273+
{
274+
foreach (MenuOption option in list.ToOptions(effect, condition))
275+
{
276+
_item.Add(option);
277+
}
278+
return this;
279+
}
266280
public IAddOptions OptionsFromList(IEnumerable<ColorText> list, Action effect, Func<string, bool>? condition = null)
267281
{
268282
foreach (MenuOption option in list.ToOptions(effect, condition))

ConsoleHero/ParagraphBuilder.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ConsoleHero.Interfaces;
2+
using System.Drawing;
23

34
namespace ConsoleHero;
45
/// <summary>
@@ -47,6 +48,10 @@ public interface ISetLines
4748
/// </summary>
4849
public ISetLines Text(string text, Color color);
4950
/// <summary>
51+
/// Add additional text to the end of this line. Custom color.
52+
/// </summary>
53+
public ISetLines Text(ColorText colorText);
54+
/// <summary>
5055
/// After displaying this paragraph, will wait before continuing without input.
5156
/// </summary>
5257
public Paragraph Delay(TimeSpan delay);
@@ -110,28 +115,21 @@ public ISetLines ClearOnCall()
110115
return this;
111116
}
112117

113-
public ISetLines Line(string text)
114-
{
115-
ParagraphLine line = new();
116-
line.Components.Add(new ColorText(text));
117-
_item.Outputs.Add(line);
118-
return this;
119-
}
120-
public ISetLines Line(string text, Color color)
118+
public ISetLines Line(string text) => Line(new ColorText(text));
119+
public ISetLines Line(string text, Color color) => Line(new ColorText(text, color));
120+
public ISetLines Line(ColorText colorText)
121121
{
122122
ParagraphLine line = new();
123-
line.Components.Add(new ColorText(text, color));
123+
line.Components.Add(colorText);
124124
_item.Outputs.Add(line);
125125
return this;
126126
}
127-
public ISetLines Text(string text)
128-
{
129-
_item.Outputs[^1].Components.Add(new ColorText(text));
130-
return this;
131-
}
132-
public ISetLines Text(string text, Color color)
127+
128+
public ISetLines Text(string text) => Text(new ColorText(text));
129+
public ISetLines Text(string text, Color color) => Text(new ColorText(text, color));
130+
public ISetLines Text(ColorText colorText)
133131
{
134-
_item.Outputs[^1].Components.Add(new ColorText(text, color));
132+
_item.Outputs[^1].Components.Add(colorText);
135133
return this;
136134
}
137135
public Paragraph Delay(TimeSpan delay)

0 commit comments

Comments
 (0)