Skip to content

Commit 66bbd55

Browse files
committed
Refactor options to their own class
Introduce a class for options for a Get-PSReadlineOptions cmdlet.
1 parent d5308da commit 66bbd55

File tree

3 files changed

+295
-194
lines changed

3 files changed

+295
-194
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,193 @@
44

55
namespace PSConsoleUtilities
66
{
7+
public class PSConsoleReadlineOptions
8+
{
9+
public const ConsoleColor DefaultCommentForegroundColor = ConsoleColor.DarkGreen;
10+
public const ConsoleColor DefaultKeywordForegroundColor = ConsoleColor.Green;
11+
public const ConsoleColor DefaultStringForegroundColor = ConsoleColor.DarkCyan;
12+
public const ConsoleColor DefaultOperatorForegroundColor = ConsoleColor.DarkGray;
13+
public const ConsoleColor DefaultVariableForegroundColor = ConsoleColor.Green;
14+
public const ConsoleColor DefaultCommandForegroundColor = ConsoleColor.Yellow;
15+
public const ConsoleColor DefaultParameterForegroundColor = ConsoleColor.DarkGray;
16+
public const ConsoleColor DefaultTypeForegroundColor = ConsoleColor.Gray;
17+
public const ConsoleColor DefaultNumberForegroundColor = ConsoleColor.White;
18+
public const ConsoleColor DefaultMemberForegroundColor = ConsoleColor.Gray;
19+
20+
public const string DefaultContinuationPrompt = ">>> ";
21+
22+
/// <summary>
23+
/// The maximum number of commands to store in the history.
24+
/// </summary>
25+
public const int DefaultMaximumHistoryCount = 1024;
26+
27+
/// <summary>
28+
/// The maximum number of items to store in the kill ring.
29+
/// </summary>
30+
public const int DefaultMaximumKillRingCount = 10;
31+
32+
/// <summary>
33+
/// In Emacs, when searching history, the cursor doesn't move.
34+
/// In 4NT, the cursor moves to the end. This option allows
35+
/// for either behavior.
36+
/// </summary>
37+
public const bool DefaultHistorySearchCursorMovesToEnd = false;
38+
39+
/// <summary>
40+
/// When displaying possible completions, either display
41+
/// tooltips or dipslay just the completions.
42+
/// </summary>
43+
public const bool DefaultShowToolTips = false;
44+
45+
/// <summary>
46+
/// When ringing the bell, what frequency do we use?
47+
/// </summary>
48+
public const int DefaultDingTone = 1221;
49+
50+
public const int DefaultDingDuration = 50;
51+
52+
/// <summary>
53+
/// When ringing the bell, what should be done?
54+
/// </summary>
55+
public const BellStyle DefaultBellStyle = BellStyle.Audible;
56+
57+
public PSConsoleReadlineOptions()
58+
{
59+
ResetColors();
60+
ContinuationPrompt = DefaultContinuationPrompt;
61+
ContinuationPromptBackgroundColor = Console.BackgroundColor;
62+
ContinuationPromptForegroundColor = Console.ForegroundColor;
63+
ExtraPromptLineCount = DefaultExtraPromptLineCount;
64+
AddToHistoryHandler = null;
65+
HistoryNoDuplicates = DefaultHistoryNoDuplicates;
66+
MaximumHistoryCount = DefaultMaximumHistoryCount;
67+
MaximumKillRingCount = DefaultMaximumKillRingCount;
68+
HistorySearchCursorMovesToEnd = DefaultHistorySearchCursorMovesToEnd;
69+
ShowToolTips = DefaultShowToolTips;
70+
DingDuration = DefaultDingDuration;
71+
DingTone = DefaultDingTone;
72+
BellStyle = DefaultBellStyle;
73+
}
74+
75+
public string ContinuationPrompt { get; set; }
76+
public ConsoleColor ContinuationPromptForegroundColor { get; set; }
77+
public ConsoleColor ContinuationPromptBackgroundColor { get; set; }
78+
79+
/// <summary>
80+
/// Prompts are typically 1 line, but sometimes they may span lines. This
81+
/// count is used to make sure we can display the full prompt after showing
82+
/// ambiguous completions
83+
/// </summary>
84+
public int ExtraPromptLineCount { get; set; }
85+
public const int DefaultExtraPromptLineCount = 0;
86+
87+
/// <summary>
88+
///
89+
/// </summary>
90+
public Func<string, bool> AddToHistoryHandler { get; set; }
91+
92+
/// <summary>
93+
/// When true, duplicates will not be added to the history.
94+
/// </summary>
95+
public bool HistoryNoDuplicates { get; set; }
96+
public const bool DefaultHistoryNoDuplicates = false;
97+
98+
public int MaximumHistoryCount { get; set; }
99+
public int MaximumKillRingCount { get; set; }
100+
public bool HistorySearchCursorMovesToEnd { get; set; }
101+
public bool ShowToolTips { get; set; }
102+
public int DingTone { get; set; }
103+
104+
/// <summary>
105+
/// When ringing the bell, how long (in ms)?
106+
/// </summary>
107+
public int DingDuration { get; set; }
108+
public BellStyle BellStyle { get; set; }
109+
110+
public ConsoleColor DefaultTokenForegroundColor { get; set; }
111+
public ConsoleColor CommentForegroundColor { get; set; }
112+
public ConsoleColor KeywordForegroundColor { get; set; }
113+
public ConsoleColor StringForegroundColor { get; set; }
114+
public ConsoleColor OperatorForegroundColor { get; set; }
115+
public ConsoleColor VariableForegroundColor { get; set; }
116+
public ConsoleColor CommandForegroundColor { get; set; }
117+
public ConsoleColor ParameterForegroundColor { get; set; }
118+
public ConsoleColor TypeForegroundColor { get; set; }
119+
public ConsoleColor NumberForegroundColor { get; set; }
120+
public ConsoleColor MemberForegroundColor { get; set; }
121+
public ConsoleColor DefaultTokenBackgroundColor { get; set; }
122+
public ConsoleColor CommentBackgroundColor { get; set; }
123+
public ConsoleColor KeywordBackgroundColor { get; set; }
124+
public ConsoleColor StringBackgroundColor { get; set; }
125+
public ConsoleColor OperatorBackgroundColor { get; set; }
126+
public ConsoleColor VariableBackgroundColor { get; set; }
127+
public ConsoleColor CommandBackgroundColor { get; set; }
128+
public ConsoleColor ParameterBackgroundColor { get; set; }
129+
public ConsoleColor TypeBackgroundColor { get; set; }
130+
public ConsoleColor NumberBackgroundColor { get; set; }
131+
public ConsoleColor MemberBackgroundColor { get; set; }
132+
133+
internal void ResetColors()
134+
{
135+
CommentForegroundColor = DefaultCommandForegroundColor;
136+
KeywordForegroundColor = DefaultKeywordForegroundColor;
137+
StringForegroundColor = DefaultStringForegroundColor;
138+
OperatorForegroundColor = DefaultOperatorForegroundColor;
139+
VariableForegroundColor = DefaultVariableForegroundColor;
140+
CommandForegroundColor = DefaultCommandForegroundColor;
141+
ParameterForegroundColor = DefaultParameterForegroundColor;
142+
TypeForegroundColor = DefaultTypeForegroundColor;
143+
NumberForegroundColor = DefaultNumberForegroundColor;
144+
MemberForegroundColor = DefaultNumberForegroundColor;
145+
CommentBackgroundColor = Console.BackgroundColor;
146+
KeywordBackgroundColor = Console.BackgroundColor;
147+
StringBackgroundColor = Console.BackgroundColor;
148+
OperatorBackgroundColor = Console.BackgroundColor;
149+
VariableBackgroundColor = Console.BackgroundColor;
150+
CommandBackgroundColor = Console.BackgroundColor;
151+
ParameterBackgroundColor = Console.BackgroundColor;
152+
TypeBackgroundColor = Console.BackgroundColor;
153+
NumberBackgroundColor = Console.BackgroundColor;
154+
MemberBackgroundColor = Console.BackgroundColor;
155+
}
156+
157+
internal void SetForegroundColor(TokenClassification tokenKind, ConsoleColor color)
158+
{
159+
switch (tokenKind)
160+
{
161+
case TokenClassification.None: DefaultTokenForegroundColor = color; break;
162+
case TokenClassification.Comment: CommentForegroundColor = color; break;
163+
case TokenClassification.Keyword: KeywordForegroundColor = color; break;
164+
case TokenClassification.String: StringForegroundColor = color; break;
165+
case TokenClassification.Operator: OperatorForegroundColor = color; break;
166+
case TokenClassification.Variable: VariableForegroundColor = color; break;
167+
case TokenClassification.Command: CommandForegroundColor = color; break;
168+
case TokenClassification.Parameter: ParameterForegroundColor = color; break;
169+
case TokenClassification.Type: TypeForegroundColor = color; break;
170+
case TokenClassification.Number: NumberForegroundColor = color; break;
171+
case TokenClassification.Member: MemberForegroundColor = color; break;
172+
}
173+
}
174+
175+
internal void SetBackgroundColor(TokenClassification tokenKind, ConsoleColor color)
176+
{
177+
switch (tokenKind)
178+
{
179+
case TokenClassification.None: DefaultTokenBackgroundColor = color; break;
180+
case TokenClassification.Comment: CommentBackgroundColor = color; break;
181+
case TokenClassification.Keyword: KeywordBackgroundColor = color; break;
182+
case TokenClassification.String: StringBackgroundColor = color; break;
183+
case TokenClassification.Operator: OperatorBackgroundColor = color; break;
184+
case TokenClassification.Variable: VariableBackgroundColor = color; break;
185+
case TokenClassification.Command: CommandBackgroundColor = color; break;
186+
case TokenClassification.Parameter: ParameterBackgroundColor = color; break;
187+
case TokenClassification.Type: TypeBackgroundColor = color; break;
188+
case TokenClassification.Number: NumberBackgroundColor = color; break;
189+
case TokenClassification.Member: MemberBackgroundColor = color; break;
190+
}
191+
}
192+
}
193+
7194
[Cmdlet("Set", "PSReadlineOption")]
8195
public class SetPSReadlineOption : PSCmdlet
9196
{

0 commit comments

Comments
 (0)