|
4 | 4 |
|
5 | 5 | using System.Linq; |
6 | 6 | using XenoAtom.Terminal; |
| 7 | +using XenoAtom.Terminal.UI.Commands; |
7 | 8 | using XenoAtom.Terminal.UI.Controls; |
8 | 9 | using XenoAtom.Terminal.UI.Hosting; |
9 | 10 | using XenoAtom.Terminal.UI.Input; |
@@ -140,6 +141,80 @@ static PromptEditorCompletion Complete(in PromptEditorCompletionRequest request) |
140 | 141 | driver.TickUntil(() => editor.Text == "hello"); |
141 | 142 | } |
142 | 143 |
|
| 144 | + [TestMethod] |
| 145 | + public void PromptEditor_Escape_Cancels_By_Default() |
| 146 | + { |
| 147 | + var canceled = false; |
| 148 | + |
| 149 | + var editor = new PromptEditor() |
| 150 | + .Canceled((_, _) => canceled = true) |
| 151 | + .AutoFocus(true); |
| 152 | + |
| 153 | + var root = new VStack { editor }; |
| 154 | + |
| 155 | + using var driver = new TerminalAppTestDriver(root, TerminalHostKind.Fullscreen, new TerminalSize(40, 6)); |
| 156 | + driver.Tick(); |
| 157 | + |
| 158 | + driver.Backend.PushEvent(new TerminalKeyEvent { Key = TerminalKey.Escape }); |
| 159 | + driver.TickUntil(() => canceled); |
| 160 | + } |
| 161 | + |
| 162 | + [TestMethod] |
| 163 | + public void PromptEditor_Can_Reserve_Escape_Only_While_Completion_Is_Active() |
| 164 | + { |
| 165 | + static PromptEditorCompletion Complete(in PromptEditorCompletionRequest request) |
| 166 | + => new( |
| 167 | + Handled: true, |
| 168 | + Candidates: ["hello", "help"], |
| 169 | + ReplaceStart: 0, |
| 170 | + ReplaceLength: request.CaretIndex); |
| 171 | + |
| 172 | + var canceled = false; |
| 173 | + var customEscapeCount = 0; |
| 174 | + |
| 175 | + var editor = new PromptEditor() |
| 176 | + .EscapeBehavior(PromptEditorEscapeBehavior.CancelCompletionOnly) |
| 177 | + .CompletionPresentation(PromptEditorCompletionPresentation.InlineCycle) |
| 178 | + .CompletionHandler(Complete) |
| 179 | + .Canceled((_, _) => canceled = true) |
| 180 | + .AutoFocus(true); |
| 181 | + |
| 182 | + editor.AddCommand(new Command |
| 183 | + { |
| 184 | + Id = "Custom.Close", |
| 185 | + LabelMarkup = "Close", |
| 186 | + Gesture = new KeyGesture(TerminalKey.Escape), |
| 187 | + Execute = _ => customEscapeCount++, |
| 188 | + }); |
| 189 | + |
| 190 | + var root = new VStack { editor }; |
| 191 | + |
| 192 | + using var driver = new TerminalAppTestDriver(root, TerminalHostKind.Fullscreen, new TerminalSize(40, 6)); |
| 193 | + driver.Tick(); |
| 194 | + |
| 195 | + driver.Backend.PushEvent(new TerminalTextEvent { Text = "h" }); |
| 196 | + driver.TickUntil(() => editor.Text == "h"); |
| 197 | + |
| 198 | + driver.Backend.PushEvent(new TerminalKeyEvent { Key = TerminalKey.Escape }); |
| 199 | + driver.TickUntil(() => customEscapeCount == 1); |
| 200 | + |
| 201 | + Assert.IsFalse(canceled); |
| 202 | + |
| 203 | + driver.Backend.PushEvent(new TerminalKeyEvent { Key = TerminalKey.Tab }); |
| 204 | + driver.TickUntil(() => editor.Text == "hello"); |
| 205 | + |
| 206 | + driver.Backend.PushEvent(new TerminalKeyEvent { Key = TerminalKey.Escape }); |
| 207 | + driver.Tick(); |
| 208 | + |
| 209 | + Assert.AreEqual(1, customEscapeCount); |
| 210 | + Assert.IsFalse(canceled); |
| 211 | + |
| 212 | + driver.Backend.PushEvent(new TerminalKeyEvent { Key = TerminalKey.Tab }); |
| 213 | + driver.Tick(); |
| 214 | + |
| 215 | + Assert.AreEqual("hello", editor.Text); |
| 216 | + } |
| 217 | + |
143 | 218 | [TestMethod] |
144 | 219 | public void PromptEditor_Uses_Default_Command_Config_When_Config_Is_Null() |
145 | 220 | { |
|
0 commit comments