BadScript 2
Loading...
Searching...
No Matches
XTermConsole.cs
Go to the documentation of this file.
1using System.Text;
2
4
5using Newtonsoft.Json;
6
7using XtermBlazor;
9
11{
12 private readonly TerminalOptions _options;
13 private readonly Xterm _terminal;
14 private readonly StringBuilder _inputBuffer = new StringBuilder();
15 private int _cursorPosition;
16 private readonly Queue<string> _inputQueue = new Queue<string>();
17
18 public XTermConsole(TerminalOptions options, Xterm terminal)
19 {
20 _options = options;
21 _terminal = terminal;
22 _terminal.AttachCustomKeyEventHandler((args) =>
23 {
24 if (args.Type != "keydown")
25 return true;
26 if (args.Key == "Enter")
27 {
28 _inputQueue.Enqueue(_inputBuffer.ToString());
29 _inputBuffer.Clear();
31 WriteLine(string.Empty);
32 }
33 else if (args.Key == "Backspace")
34 {
35 if (_inputBuffer.Length > 0 && _cursorPosition > 0)
36 {
37 _inputBuffer.Remove(_cursorPosition - 1, 1);
39 // Move cursor back
40 Write("\b");
41 // Write out the rest of the line with space to clear the character
42 Write(_inputBuffer.ToString().Substring(_cursorPosition) + " ");
43 // Move cursor back to the original position
44 Write(new string('\b', _inputBuffer.Length - _cursorPosition + 1));
45 }
46 }
47 else if(args.Key == "ArrowLeft")
48 {
49 if (_cursorPosition > 0)
50 {
52 Write("\b");
53 }
54 }
55 else if(args.Key == "ArrowRight")
56 {
57 if (_cursorPosition < _inputBuffer.Length)
58 {
60 Write("\u001b[C");
61 }
62 }
63 else if(args.Key.Length == 1)
64 {
65 if(_cursorPosition == _inputBuffer.Length)
66 {
67 _inputBuffer.Append(args.Key);
69 Write(args.Key);
70 }
71 else
72 {
73 _inputBuffer.Insert(_cursorPosition, args.Key);
75 Write(args.Key);
76 Write(_inputBuffer.ToString().Substring(_cursorPosition));
77 Write(new string('\b', _inputBuffer.Length - _cursorPosition));
78 }
79 }
80 else if(args.Key == "Tab")
81 {
82 _inputBuffer.Insert(_cursorPosition, " ");
83 _cursorPosition += 4;
84 Write(" ");
85 }
86 else
87 {
88 Console.WriteLine("Unknown Key: " + args.Key);
89 }
90
91 return true;
92 });
93 }
94
95 private ConsoleColor _foregroundColor;
96 public ConsoleColor ForegroundColor { get => _foregroundColor; set => SetForegroundColor(value); }
97
98
99 private ConsoleColor _backgroundColor;
100 public ConsoleColor BackgroundColor { get => _backgroundColor; set => SetBackgroundColor(value); }
101
102 private static Dictionary<ConsoleColor, string> _colorMap = new Dictionary<ConsoleColor, string>
103 {
104 {ConsoleColor.Black, "#000000"},
105 {ConsoleColor.DarkBlue, "#000080"},
106 {ConsoleColor.DarkGreen, "#008000"},
107 {ConsoleColor.DarkCyan, "#008080"},
108 {ConsoleColor.DarkRed, "#800000"},
109 {ConsoleColor.DarkMagenta, "#800080"},
110 {ConsoleColor.DarkYellow, "#808000"},
111 {ConsoleColor.Gray, "#c0c0c0"},
112 {ConsoleColor.DarkGray, "#808080"},
113 {ConsoleColor.Blue, "#0000ff"},
114 {ConsoleColor.Green, "#00ff00"},
115 {ConsoleColor.Cyan, "#00ffff"},
116 {ConsoleColor.Red, "#ff0000"},
117 {ConsoleColor.Magenta, "#ff00ff"},
118 {ConsoleColor.Yellow, "#ffff00"},
119 {ConsoleColor.White, "#ffffff"}
120 };
121
122 private void SetBackgroundColor(ConsoleColor color)
123 {
124 _backgroundColor = color;
125 _options.Theme.Background = _colorMap[color];
126 _terminal.SetOptions(_options);
127 }
128
129 private void SetForegroundColor(ConsoleColor color)
130 {
131 _foregroundColor = color;
132 _options.Theme.Foreground = _colorMap[color];
133 _terminal.SetOptions(_options);
134 }
135
136 public void Write(string str)
137 {
138 if (str.Contains('\n'))
139 {
140 string[] lines = str.Split('\n');
141 foreach (string line in lines.SkipLast(1))
142 {
143 _terminal.WriteLine(line);
144 }
145 _terminal.Write(lines.Last());
146 return;
147 }
148 _terminal.Write(str);
149 }
150 public void WriteLine(string str)
151 {
152 if (str.Contains('\n'))
153 {
154 string[] lines = str.Split('\n');
155 foreach (string line in lines)
156 {
157 _terminal.WriteLine(line);
158 }
159 return;
160 }
161 _terminal.WriteLine(str);
162 }
163 public string ReadLine()
164 {
165 if(_inputQueue.Count > 0)
166 {
167 return _inputQueue.Dequeue();
168 }
169 throw new NotSupportedException("Can not read line synchronously from XTermConsole");
170 }
171 public async Task<string> ReadLineAsync()
172 {
173 if(_inputQueue.Count > 0)
174 {
175 return _inputQueue.Dequeue();
176 }
177
178 while (_inputQueue.Count == 0)
179 {
180 await Task.Delay(100);
181 }
182
183 return _inputQueue.Dequeue();
184 }
185 public void Clear()
186 {
187 _terminal.Clear();
188 }
189}
string ReadLine()
Reads a line from the console.
void SetForegroundColor(ConsoleColor color)
void WriteLine(string str)
Writes a string to the console and appends a newline.
XTermConsole(TerminalOptions options, Xterm terminal)
ConsoleColor ForegroundColor
The Foreground Color.
ConsoleColor BackgroundColor
The Background Color.
void Write(string str)
Writes a string to the console.
async Task< string > ReadLineAsync()
Reads a line from the console asynchronously.
void SetBackgroundColor(ConsoleColor color)
static Dictionary< ConsoleColor, string > _colorMap
Interface that abstracts the console.
Contains a Console Abstraction Layer to be able to Simulate Console Input/Output over the Network.
Definition BadConsole.cs:6