Skip to content

Commit 3f28dfa

Browse files
committed
Switching to a "Terminal" palette instead of ConsoleColor
1 parent d6a8740 commit 3f28dfa

File tree

5 files changed

+129
-108
lines changed

5 files changed

+129
-108
lines changed

Source/Assembly/Palettes/ConsolePalette.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ public class ConsolePalette : Palette<RgbColor>
1010
public ConsolePalette()
1111
{
1212
// The ConsolePalette needs to be in the .NET ConsoleColors enum order
13-
Add(new RgbColor(0x00, 0x00, 0x00));
14-
Add(new RgbColor(0x00, 0x00, 0x80));
15-
Add(new RgbColor(0x00, 0x80, 0x00));
16-
Add(new RgbColor(0x00, 0x80, 0x80));
17-
Add(new RgbColor(0x80, 0x00, 0x00));
18-
Add(new RgbColor(0x80, 0x00, 0x80));
19-
Add(new RgbColor(0x80, 0x80, 0x00));
20-
Add(new RgbColor(0xc0, 0xc0, 0xc0));
21-
Add(new RgbColor(0x80, 0x80, 0x80));
22-
Add(new RgbColor(0x00, 0x00, 0xff));
23-
Add(new RgbColor(0x00, 0xff, 0x00));
24-
Add(new RgbColor(0x00, 0xff, 0xff));
25-
Add(new RgbColor(0xff, 0x00, 0x00));
26-
Add(new RgbColor(0xff, 0x00, 0xff));
27-
Add(new RgbColor(0xff, 0xff, 0x00));
28-
Add(new RgbColor(0xff, 0xff, 0xff));
13+
Add(new RgbColor(0x00, 0x00, 0x00, ColorMode.ConsoleColor));
14+
Add(new RgbColor(0x00, 0x00, 0x80, ColorMode.ConsoleColor));
15+
Add(new RgbColor(0x00, 0x80, 0x00, ColorMode.ConsoleColor));
16+
Add(new RgbColor(0x00, 0x80, 0x80, ColorMode.ConsoleColor));
17+
Add(new RgbColor(0x80, 0x00, 0x00, ColorMode.ConsoleColor));
18+
Add(new RgbColor(0x80, 0x00, 0x80, ColorMode.ConsoleColor));
19+
Add(new RgbColor(0x80, 0x80, 0x00, ColorMode.ConsoleColor));
20+
Add(new RgbColor(0xc0, 0xc0, 0xc0, ColorMode.ConsoleColor));
21+
Add(new RgbColor(0x80, 0x80, 0x80, ColorMode.ConsoleColor));
22+
Add(new RgbColor(0x00, 0x00, 0xff, ColorMode.ConsoleColor));
23+
Add(new RgbColor(0x00, 0xff, 0x00, ColorMode.ConsoleColor));
24+
Add(new RgbColor(0x00, 0xff, 0xff, ColorMode.ConsoleColor));
25+
Add(new RgbColor(0xff, 0x00, 0x00, ColorMode.ConsoleColor));
26+
Add(new RgbColor(0xff, 0x00, 0xff, ColorMode.ConsoleColor));
27+
Add(new RgbColor(0xff, 0xff, 0x00, ColorMode.ConsoleColor));
28+
Add(new RgbColor(0xff, 0xff, 0xff, ColorMode.ConsoleColor));
2929
}
3030
}
3131
}

Source/Assembly/Palettes/NamedPalette.cs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Collections.Specialized;
45
using System.Linq;
@@ -12,22 +13,20 @@ namespace PoshCode.Pansies.Palettes
1213
{
1314
public class NamedPalette<T> : Palette<T>, IReadOnlyDictionary<string, T>, IArgumentCompleter where T : IColorSpace, new()
1415
{
15-
private IList<string> names = new List<string>();
16+
private List<string> names = new List<string>();
1617

1718
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
1819
{
1920
WildcardPattern wildcard = new WildcardPattern(wordToComplete + "*", WildcardOptions.IgnoreCase);
2021

21-
foreach (var name in Keys.Where(s => wildcard.IsMatch(s.ToString())))
22+
foreach (var name in names.Where(wildcard.IsMatch))
2223
{
23-
yield return new CompletionResult(name.ToString(), dictionary[name].ToVTEscapeSequence(true) + " \u001B[0m " + name.ToString(), CompletionResultType.ParameterValue, name.ToString());
24+
yield return new CompletionResult(name.ToString(), nativeColors[names.IndexOf(name)].ToVTEscapeSequence(true) + " \u001B[0m " + name, CompletionResultType.ParameterValue, name);
2425
}
2526
}
2627

27-
private readonly IDictionary<string, T> dictionary;
2828
public NamedPalette()
2929
{
30-
dictionary = new Dictionary<string, T>();
3130
}
3231

3332
public NamedPalette(IColorSpaceComparison comparisonAlgorithm) : this()
@@ -42,18 +41,8 @@ public virtual void Add(string key, T value) {
4241
Add(value);
4342
}
4443

45-
public virtual bool ContainsKey(string key) => names.Contains(key);
44+
public virtual bool ContainsKey(string key) => names.Contains(key, StringComparer.OrdinalIgnoreCase);
4645

47-
public virtual bool TryGetValue(string key, out T value) {
48-
var index = names.IndexOf(key);
49-
if (index >= 0)
50-
{
51-
value = nativeColors[index];
52-
return true;
53-
}
54-
value = default;
55-
return false;
56-
}
5746

5847
// public virtual void Add(KeyValuePair<string, T> items) => dictionary.Add(items);
5948

@@ -83,15 +72,14 @@ IEnumerator<KeyValuePair<string, T>> IEnumerable<KeyValuePair<string, T>>.GetEnu
8372
}
8473
}
8574

86-
public int IndexOf(string key) => names.IndexOf(key);
87-
75+
public int IndexOf(string key) => names.FindIndex(x => x.Equals(key, StringComparison.OrdinalIgnoreCase));
8876
public string FindClosestColorName(IColorSpace color)
8977
{
9078
return names[FindClosestColor<T>(color).Index];
9179
}
9280

9381
public T GetValue(string key) {
94-
var index = names.IndexOf(key);
82+
var index = IndexOf(key);
9583
if (index >= 0)
9684
{
9785
return nativeColors[index];
@@ -116,7 +104,7 @@ public T GetValue(string key) {
116104
}
117105
public virtual bool Remove(string key)
118106
{
119-
var index = names.IndexOf(key);
107+
var index = IndexOf(key);
120108
if (index >= 0) {
121109
((Palette<T>)this).RemoveAt(index);
122110
names.RemoveAt(index);
@@ -130,14 +118,20 @@ public virtual bool Remove(string key)
130118
names.RemoveAt(index);
131119
}
132120

133-
bool IReadOnlyDictionary<string, T>.ContainsKey(string key)
134-
{
135-
throw new System.NotImplementedException();
136-
}
121+
bool IReadOnlyDictionary<string, T>.ContainsKey(string key) => ContainsKey(key);
122+
123+
bool IReadOnlyDictionary<string, T>.TryGetValue(string key, out T value) => TryGetValue(key, out value);
137124

138-
bool IReadOnlyDictionary<string, T>.TryGetValue(string key, out T value)
125+
public virtual bool TryGetValue(string key, out T value)
139126
{
140-
throw new System.NotImplementedException();
127+
var index = IndexOf(key);
128+
if (index >= 0)
129+
{
130+
value = nativeColors[index];
131+
return true;
132+
}
133+
value = default;
134+
return false;
141135
}
142136

143137
new public KeyValuePair<string,T> this[int index]

Source/Assembly/Palettes/TerminalPalette.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ public class TerminalPalette : NamedPalette<RgbColor>
99

1010
public TerminalPalette()
1111
{
12-
Add("Black", new RgbColor(System.ConsoleColor.Black));
13-
Add("Red", new RgbColor(System.ConsoleColor.DarkRed));
14-
Add("Green", new RgbColor(System.ConsoleColor.DarkGreen));
15-
Add("Yellow", new RgbColor(System.ConsoleColor.DarkYellow));
16-
Add("Blue", new RgbColor(System.ConsoleColor.DarkBlue));
17-
Add("Magenta", new RgbColor(System.ConsoleColor.DarkMagenta));
18-
Add("Cyan", new RgbColor(System.ConsoleColor.DarkCyan));
19-
Add("White", new RgbColor(System.ConsoleColor.Gray));
20-
Add("BrightBlack", new RgbColor(System.ConsoleColor.DarkGray));
21-
Add("BrightRed", new RgbColor(System.ConsoleColor.Red));
22-
Add("BrightGreen", new RgbColor(System.ConsoleColor.Green));
23-
Add("BrightYellow", new RgbColor(System.ConsoleColor.Yellow));
24-
Add("BrightBlue", new RgbColor(System.ConsoleColor.Blue));
25-
Add("BrightMagenta", new RgbColor(System.ConsoleColor.Magenta));
26-
Add("BrightCyan", new RgbColor(System.ConsoleColor.Cyan));
27-
Add("BrightWhite", new RgbColor(System.ConsoleColor.White));
28-
Add("Default", new RgbColor("Default"));
12+
Add("Black", new RgbColor(0x00, 0x00, 0x00, ColorMode.TerminalColor));
13+
Add("Red", new RgbColor(0x80, 0x00, 0x00, ColorMode.TerminalColor));
14+
Add("Green", new RgbColor(0x00, 0x80, 0x00, ColorMode.TerminalColor));
15+
Add("Yellow", new RgbColor(0x80, 0x80, 0x00, ColorMode.TerminalColor));
16+
Add("Blue", new RgbColor(0x00, 0x00, 0x80, ColorMode.TerminalColor));
17+
Add("Magenta", new RgbColor(0x80, 0x00, 0x80, ColorMode.TerminalColor));
18+
Add("Cyan", new RgbColor(0x00, 0x80, 0x80, ColorMode.TerminalColor));
19+
Add("White", new RgbColor(0xc0, 0xc0, 0xc0, ColorMode.TerminalColor));
20+
Add("BrightBlack", new RgbColor(0x80, 0x80, 0x80, ColorMode.TerminalColor));
21+
Add("BrightRed", new RgbColor(0xff, 0x00, 0x00, ColorMode.TerminalColor));
22+
Add("BrightGreen", new RgbColor(0x00, 0xff, 0x00, ColorMode.TerminalColor));
23+
Add("BrightYellow", new RgbColor(0xff, 0xff, 0x00, ColorMode.TerminalColor));
24+
Add("BrightBlue", new RgbColor(0x00, 0x00, 0xff, ColorMode.TerminalColor));
25+
Add("BrightMagenta", new RgbColor(0xff, 0x00, 0xff, ColorMode.TerminalColor));
26+
Add("BrightCyan", new RgbColor(0x00, 0xff, 0xff, ColorMode.TerminalColor));
27+
Add("BrightWhite", new RgbColor(0xff, 0xff, 0xff, ColorMode.TerminalColor));
28+
Add("Default", new RgbColor(-1,-1,-1, ColorMode.TerminalColor));
2929
}
3030
}
3131
}

Source/Assembly/Palettes/X11Palette.cs

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

99
namespace PoshCode.Pansies.Palettes
1010
{
11-
public class X11Palette : NamedPalette<RgbColor>, IArgumentCompleter
11+
public class X11Palette : NamedPalette<RgbColor>
1212
{
1313
public X11Palette()
1414
{

0 commit comments

Comments
 (0)