Skip to content

Commit aeb3208

Browse files
authored
Merge pull request #55 from meokullu/add_feature_0005
v1.10.0 Key-Action pair
2 parents 57ddbe3 + 7a0933c commit aeb3208

File tree

4 files changed

+171
-2
lines changed

4 files changed

+171
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#### Added
1717
* `Write(string format, object arg0)`, `Write(string format, object arg0, object arg1)`, `Write(string format, object arg0, object arg2)`, `Write(string format, object arg0, object arg1, object arg2, object arg3)` and `Write(string format, param object[] arg)` are added into WriteCore.cs.
1818
* `WriteLine(string format, object arg0)`, `WriteLine(string format, object arg0, object arg1)`, `Write(string format, object arg0, object arg2)`, `WriteLine(string format, object arg0, object arg1, object arg2, object arg3)` and `WriteLine(string format, param object[] arg)` are added into WriteCore.cs.
19+
* Key-Action pair structure is added.
20+
* Simplified construstor of `ConsoleKeyInfo` is added. `ConsoleKeyInfo(char keychar, ConsoleKey key)` calls `ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl)` with false value modifiers.
21+
* `List<Tuple<ConsoleKeyInfo, Action>>` KeyActionPairList is added. It is `public`.
22+
* `AddKeyActionPair(ConsoleKeyInfo cki, Action action)`, `ListKeyActionPairList()` and `ClearKeyActionPairList()` methods added to work with `KeyActionParList`.
23+
* `WaitKeyAction()` is added which calls `KeyAction(ConsoleKeyInfo cki)` after asking user to type a key.
24+
* `KeyAction(ConsoleKeyInfo cki)` is added which calls predefined `Action` by match of `cki` on the list.
1925

2026
#### Changed
2127
* Summary improvements on WriteCore.cs and WriteLineCore.cs

HelpConsole/HelpConsole.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReleaseNotes>
1717
v1.10.0
1818
* Missing Write() and WriteLine() methods are added into WriteCore.cs and WriteLineCore respectively.
19+
* Key-Action pair structure is added. `WaitKeyAction()` and `KeyAction(ConsoleKeyInfo cki)` methods call predefined action on predefined `KeyPairActionList`.
1920
See changelog (https://github.com/meokullu/HelpConsole/blob/master/CHANGELOG.md)
2021
</PackageReleaseNotes>
2122
<RepositoryUrl>https://github.com/meokullu/HelpConsole</RepositoryUrl>

HelpConsole/src/KeyActionPair.cs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Runtime.CompilerServices;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace HelpConsole
11+
{
12+
public partial class HelpConsole
13+
{
14+
#region Transition of charKey to ConsoleKey
15+
16+
//private static ConsoleKey ToConsoleKeyInfo(char charKey)
17+
//{
18+
// return (ConsoleKey)System.Enum.Parse(typeof(ConsoleKey), charKey.ToString());
19+
//}
20+
21+
#endregion Transition of charKey to ConsoleKey
22+
23+
#region Additional constructors
24+
25+
//public static ConsoleKeyInfo ConsoleKeyInfo(char keyChar) => new ConsoleKeyInfo(keyChar: keyChar, ToConsoleKeyInfo(keyChar), false, false, false);
26+
27+
/// <summary>
28+
/// ConsoleKeyInfo consructor works with keyChar and key, all modes are false.
29+
/// </summary>
30+
/// <param name="keyChar">A character on keybord represented by a char.</param>
31+
/// <param name="key">A key that is defined on ConsoleKey enum. Integere value of key must be between 0 and 255.</param>
32+
/// <returns>ConsoleKeyInfo consists keyChar, key and mods.</returns>
33+
public static ConsoleKeyInfo ConsoleKeyInfo(char keyChar, ConsoleKey key) => new ConsoleKeyInfo(keyChar: keyChar, key: key, shift: false, alt: false, control: false);
34+
35+
#endregion Additional constructors
36+
37+
#region List of Key-Action pair.
38+
39+
/// <summary>
40+
/// Key-Action pair list that holds values which will be used by KeyAction(ConsoleKeyInfo cki). Readonly?
41+
/// </summary>
42+
public static List<Tuple<ConsoleKeyInfo, Action>> KeyActionPairList = new List<Tuple<ConsoleKeyInfo, Action>>();
43+
44+
#endregion List of Key-Action pair.
45+
46+
#region Add, List and Clear of Key-Action pair list.
47+
48+
/// <summary>
49+
/// Adds Key-Action pair into KeyActionPairList which will be used by KeyAction(ConsoleKeyInfo cki).
50+
/// </summary>
51+
/// <param name="cki">ConsoleKeyInfo constructed by either ConsoleKeyInfo(char keyChar, ConsoleKey consoleKey) or predefined ConsoleKeyInfo(char keyChar, ConsoleKey consoleKey, bool alt, bool shift, bool ctrl)</param>
52+
/// <param name="action">An action to be called with match of cki.</param>
53+
public static void AddKeyActionPair(ConsoleKeyInfo cki, Action action)
54+
{
55+
// Adding given value into the list.
56+
KeyActionPairList.Add(new Tuple<ConsoleKeyInfo, Action>(cki, action));
57+
}
58+
59+
/// <summary>
60+
/// List Key-Action par list.
61+
/// </summary>
62+
public static void ListKeyActionPairList()
63+
{
64+
// Variable of cki value.
65+
ConsoleKeyInfo cki = new ConsoleKeyInfo();
66+
67+
// Loop to print all items.
68+
for (int i = 0; i < KeyActionPairList.Count; i++)
69+
{
70+
// set variable from the list with first element of tuple.
71+
cki = KeyActionPairList[i].Item1;
72+
73+
// Checking if KeyChar is empty character.
74+
if (cki.KeyChar != ' ')
75+
{
76+
// Writing KeyChar value.
77+
Write($"KeyChar: {cki.KeyChar} ");
78+
}
79+
80+
// Writing rest of the values of ConsoleKeyInfo.
81+
WriteLine($"Key: {cki.Key} Mods: {Enum.GetName(typeof(ConsoleModifiers), cki.Modifiers)} Action: {KeyActionPairList[i].Item2.Method.Name}");
82+
}
83+
}
84+
85+
/// <summary>
86+
/// Clear all items of Key-Action pair list.
87+
/// </summary>
88+
public static void ClearKeyActionPairList()
89+
{
90+
// Calling clear() function of List.
91+
KeyActionPairList.Clear();
92+
}
93+
94+
#endregion Add, List and Clear of Key-Action pair list.
95+
96+
#region Wait
97+
98+
// Show any message or list options could be optional parameter.
99+
100+
/// <summary>
101+
///
102+
/// </summary>
103+
public static void WaitKeyAction()
104+
{
105+
ConsoleKeyInfo? cki = null;
106+
107+
while (cki == null)
108+
{
109+
// Wait for a key.
110+
cki = Console.ReadKey();
111+
}
112+
113+
// Checking if cki is in Key-Action pair list.
114+
if (KeyActionPairList.Select(p => p.Item1).Any(q => q == cki))
115+
{
116+
// cki is not null now.
117+
KeyAction(cki.Value);
118+
}
119+
else
120+
{
121+
Debug.WriteLine("HelpConsole:Key is not defined on Key-Action pair list. Use AddKeyActionPair() or ListActionPairList()");
122+
}
123+
}
124+
125+
#endregion Wait
126+
127+
#region Do
128+
129+
/// <summary>
130+
/// Call action by predefined on Key-Action pair list.
131+
/// </summary>
132+
/// <param name="cki">ConsoleKeyInfo to call predefined Action</param>
133+
public static void KeyAction(ConsoleKeyInfo cki)
134+
{
135+
// Get action for the list.
136+
Action action = KeyActionPairList.Where(p => p.Item1 == cki).Select(q => q.Item2).SingleOrDefault();
137+
138+
// Checking if it is null.
139+
if (action == null)
140+
{
141+
// Inform user that action is not defined.
142+
Debug.WriteLine("HelpConsole: Action is not defined on list.");
143+
}
144+
else
145+
{
146+
// calling action.
147+
action();
148+
}
149+
}
150+
151+
#endregion Do
152+
}
153+
}

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ HelpConsole is a project to offer useful methods which makes easier to use conso
88
[Download on NuGet gallery](https://www.nuget.org/packages/HelpConsole/)
99

1010
### Description
11-
1211
HelpConsole has methods to increase liability of output, state and methods.
1312

1413
### Example Usage
@@ -60,6 +59,16 @@ Percentage2F(int item1, int item2);
6059
```
6160
Percentage8F(int item1, int item2);
6261
```
62+
#### Key-Action
63+
```
64+
AddKeyActionPair(new Tuple<ConsoleKeyInfo, Action>(cki: cki, action: action))`;
65+
```
66+
```
67+
WaitKeyAction();
68+
```
69+
```
70+
KeyAction(ConsoleKeyInfo cki);
71+
```
6372

6473
To check listed methods, example of output visit wiki page. [HelpConsole Wiki](https://github.com/meokullu/HelpConsole/wiki)
6574

@@ -79,4 +88,4 @@ If you'd like to contribute, then contribute. [contributing guide](https://githu
7988
[![Contributors](https://contrib.rocks/image?repo=meokullu/HelpConsole)](https://github.com/meokullu/HelpConsole/graphs/contributors)
8089

8190
### Help
82-
Twitter: Enes Okullu [@enesokullu](https://twitter.com/EnesOkullu)
91+
Twitter: Enes Okullu [@enesokullu](https://twitter.com/EnesOkullu)

0 commit comments

Comments
 (0)