-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput_handler.hpp
More file actions
1222 lines (1075 loc) · 48.2 KB
/
input_handler.hpp
File metadata and controls
1222 lines (1075 loc) · 48.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// g++ -std=c++17 main.cpp -o main.exe
#ifndef LINUXIFY_INPUT_HANDLER_HPP
#define LINUXIFY_INPUT_HANDLER_HPP
#include <string>
#include <vector>
#include <algorithm>
#include <filesystem>
#include <chrono>
#include "io_handler.hpp"
#include "signal_handler.hpp"
#include "cmds-src/auto-suggest.hpp"
#include "shell_streams.hpp"
#include <map>
#include <unordered_map>
namespace fs = std::filesystem;
namespace PromptConfig {
inline std::string firstColor = "\033[92m";
inline std::string secondColor = "\033[94m";
inline std::string resetColor = "\033[0m";
}
class InputHandler {
private:
std::string currentDir;
std::vector<std::string> history;
int historyIndex;
// Internal State
std::string inputBuffer;
int cursorPos;
int promptStartRow;
int lastNumLines; // Track previous height to clear garbage only when shrinking
int selectionAnchor; // -1 if no selection, otherwise index where selection started
// Key Handling State
char lastCharInput;
std::chrono::time_point<std::chrono::steady_clock> lastTimeInput;
bool initialized;
bool isAdmin;
std::function<bool(const std::string&)> commandValidator;
// Auto-Suggestion State
struct SuggestionEntry {
std::string cmd;
int frequency;
int lastIndex; // Recency tie-breaker
// For binary search comparison
bool operator<(const SuggestionEntry& other) const {
return cmd < other.cmd;
}
bool operator<(const std::string& otherCmd) const {
return cmd < otherCmd;
}
};
std::vector<SuggestionEntry> frequencyIndex;
std::string currentSuggestion;
enum GitStatus { GIT_NONE, GIT_CLEAN, GIT_STAGED, GIT_UNSTAGED };
GitStatus currentGitStatus;
GitStatus checkGitStatus() {
// Fast check using git status --porcelain
// We create a pipe to capture output
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
HANDLE hChildStd_OUT_Rd = NULL;
HANDLE hChildStd_OUT_Wr = NULL;
if (!CreatePipe(&hChildStd_OUT_Rd, &hChildStd_OUT_Wr, &saAttr, 0)) return GIT_CLEAN; // Asume clean on fail
SetHandleInformation(hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0);
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.hStdError = NULL; // Ignore stderr
si.hStdOutput = hChildStd_OUT_Wr;
si.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ZeroMemory(&pi, sizeof(pi));
// Command: git status --porcelain
std::string cmd = "git status --porcelain";
char cmdBuf[128];
strcpy(cmdBuf, cmd.c_str());
// Create the child process.
if (!CreateProcessA(NULL, cmdBuf, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, currentDir.c_str(), &si, &pi)) {
CloseHandle(hChildStd_OUT_Wr);
CloseHandle(hChildStd_OUT_Rd);
return GIT_CLEAN;
}
CloseHandle(hChildStd_OUT_Wr); // Close write end so read ends
// Read output
std::string output;
DWORD bytesRead;
CHAR buffer[256];
bool hasStaged = false;
bool hasUnstaged = false;
while (ReadFile(hChildStd_OUT_Rd, buffer, 255, &bytesRead, NULL) && bytesRead != 0) {
output.append(buffer, bytesRead);
if (output.length() > 2048) break; // Limit read
}
WaitForSingleObject(pi.hProcess, 100); // Wait bit
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hChildStd_OUT_Rd);
if (output.empty()) return GIT_CLEAN;
std::istringstream iss(output);
std::string line;
while (std::getline(iss, line)) {
if (line.length() < 2) continue;
char x = line[0];
char y = line[1];
if (x == '?' && y == '?') hasUnstaged = true; // Untracked
else {
if (x != ' ' && x != '?') hasStaged = true;
if (y != ' ' && y != '?') hasUnstaged = true;
}
}
if (hasUnstaged) return GIT_UNSTAGED;
if (hasStaged) return GIT_STAGED;
return GIT_CLEAN;
}
void rebuildSuggestions() {
if (history.empty()) return;
std::unordered_map<std::string, std::pair<int, int>> stats; // cmd -> {freq, lastIndex}
for (int i = 0; i < (int)history.size(); ++i) {
const std::string& cmd = history[i];
if (cmd.empty()) continue;
stats[cmd].first++;
stats[cmd].second = i;
}
frequencyIndex.clear();
frequencyIndex.reserve(stats.size());
for (const auto& kv : stats) {
frequencyIndex.push_back({kv.first, kv.second.first, kv.second.second});
}
// Sort alphabetically to allow binary search by prefix
std::sort(frequencyIndex.begin(), frequencyIndex.end());
}
void updateSuggestion() {
currentSuggestion.clear();
if (inputBuffer.empty()) return;
// Binary search for first command starting with inputBuffer
auto it = std::lower_bound(frequencyIndex.begin(), frequencyIndex.end(), inputBuffer);
int bestFreq = -1;
int bestRecency = -1;
const SuggestionEntry* bestMatch = nullptr;
// Iterate while the command still starts with inputBuffer
while (it != frequencyIndex.end()) {
// Check prefix
if (it->cmd.length() < inputBuffer.length() ||
it->cmd.compare(0, inputBuffer.length(), inputBuffer) != 0) {
break;
}
// Candidate found. Check if it's "better" (higher freq, or same freq & more recent)
if (it->cmd != inputBuffer) { // Don't suggest what is already fully typed
if (it->frequency > bestFreq || (it->frequency == bestFreq && it->lastIndex > bestRecency)) {
bestFreq = it->frequency;
bestRecency = it->lastIndex;
bestMatch = &(*it);
}
}
++it;
}
if (bestMatch) {
currentSuggestion = bestMatch->cmd;
} else {
// Fallback: AutoSuggest (Filesystem)
auto result = AutoSuggest::getSuggestions(inputBuffer, (int)inputBuffer.length(), currentDir);
if (!result.suggestions.empty()) {
std::string best = result.suggestions[0];
// Reconstruct full command line from suggestion
std::string prefix = inputBuffer.substr(0, result.replaceStart);
std::string token = inputBuffer.substr(result.replaceStart);
if (result.isPath) {
// Token might be partial path, best is filename
// We need to keep the folder part of the token
fs::path p(token);
std::string parent = p.parent_path().string();
// Add separator if parent exists and lacks it
if (!parent.empty()) {
// Windows/Linux separator check
char last = parent.back();
if (last != '/' && last != '\\') parent += "/";
} else if (token == "/" || token == "\\") {
// Root handling
parent = token;
} else if (token.find('/') != std::string::npos || token.find('\\') != std::string::npos) {
// Handle cases like "./" where parent_path returns "."
if (token.back() == '/' || token.back() == '\\') parent = token;
}
currentSuggestion = prefix + parent + best;
} else {
// Command
currentSuggestion = prefix + best;
}
}
}
}
public:
enum class PollResult {
Continue,
LineReady,
Cancelled
};
private:
void copyToClipboard(const std::string& text) {
if (OpenClipboard(NULL)) {
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, text.size() + 1);
if (hg) {
memcpy(GlobalLock(hg), text.c_str(), text.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
}
CloseClipboard();
}
}
std::string getClipboardText() {
if (!OpenClipboard(NULL)) return "";
HANDLE hData = GetClipboardData(CF_TEXT);
if (hData == NULL) {
CloseClipboard();
return "";
}
char* pszText = static_cast<char*>(GlobalLock(hData));
if (pszText == NULL) {
CloseClipboard();
return "";
}
std::string text(pszText);
GlobalUnlock(hData);
CloseClipboard();
// Remove carriage returns to keep single-line if mostly used for commands,
// or handle them. Shell usually expects single line or specific handling.
// For now, let's strip \r to play nice with linux-style line endings if needed,
// or just paste as is.
// Actually, simple paste is fine. shell might handle \n later or we strip it.
// Most shells strip newlines or execute immediately.
// Let's strip newlines for safety in this simple shell to avoid multi-command injection weirdness
text.erase(std::remove(text.begin(), text.end(), '\r'), text.end());
text.erase(std::remove(text.begin(), text.end(), '\n'), text.end());
return text;
}
void deleteSelection() {
if (selectionAnchor == -1) return;
int start = std::min(selectionAnchor, cursorPos);
int end = std::max(selectionAnchor, cursorPos);
if (start != end) {
inputBuffer.erase(start, end - start);
cursorPos = start;
}
selectionAnchor = -1;
}
std::string getDisplayPath() {
char* home = getenv("USERPROFILE");
if (!home) return currentDir;
std::string homePath = home;
std::string dirLower = currentDir;
std::string homeLower = homePath;
std::transform(dirLower.begin(), dirLower.end(), dirLower.begin(), ::tolower);
std::transform(homeLower.begin(), homeLower.end(), homeLower.begin(), ::tolower);
if (dirLower == homeLower) {
return "~";
}
if (homeLower.back() != '\\' && homeLower.back() != '/') {
homeLower += '\\';
}
if (dirLower.length() > homeLower.length() &&
dirLower.substr(0, homeLower.length()) == homeLower) {
std::string relativePath = currentDir.substr(homePath.length());
if (relativePath.front() == '\\' || relativePath.front() == '/') {
relativePath = relativePath.substr(1);
}
for (char& c : relativePath) {
if (c == '\\') c = '/';
}
return "~/" + relativePath;
}
return currentDir;
}
std::string getGitBranch() {
namespace fs = std::filesystem;
try {
fs::path current(currentDir);
while (true) {
fs::path gitDir = current / ".git";
if (fs::exists(gitDir) && fs::is_directory(gitDir)) {
fs::path headPath = gitDir / "HEAD";
std::ifstream headFile(headPath);
if (headFile.is_open()) {
std::string line;
std::getline(headFile, line);
if (line.substr(0, 5) == "ref: ") {
std::string ref = line.substr(5);
size_t lastSlash = ref.find_last_of('/');
if (lastSlash != std::string::npos) {
return ref.substr(lastSlash + 1);
}
return ref;
} else {
return line.substr(0, 7);
}
}
}
if (current.has_parent_path() && current.parent_path() != current) {
current = current.parent_path();
} else {
break;
}
}
} catch (...) {}
return "";
}
void printPrompt() {
std::string displayPath = getDisplayPath();
IO::get().write(PromptConfig::firstColor);
IO::get().write("linuxify[");
IO::get().write(sessionName);
IO::get().write("]");
IO::get().write(PromptConfig::resetColor);
IO::get().write(":");
std::string branch = getGitBranch();
if (!branch.empty()) {
IO::get().write("\033[95m");
IO::get().write(displayPath);
IO::get().write(PromptConfig::resetColor);
IO::get().write("@");
std::string statusColor = "\033[93m";
if (currentGitStatus == GIT_UNSTAGED) {
statusColor = "\033[91m";
} else if (currentGitStatus == GIT_STAGED) {
statusColor = "\033[92m";
}
IO::get().write(statusColor);
IO::get().write(branch);
} else {
IO::get().write(PromptConfig::secondColor);
IO::get().write(displayPath);
}
IO::get().write(PromptConfig::resetColor);
if (isAdmin) {
IO::get().write("# ");
} else {
IO::get().write("$ ");
}
}
void render() {
IO::Console& io = IO::get();
int width = io.getWidth();
int height = io.getHeight();
std::string displayPath = getDisplayPath();
// Base length: "linuxify[" (9) + sessionName.length() + "]:" (2) + displayPath + "$ " (2)
int promptLen = 11 + (int)sessionName.length() + (int)displayPath.length() + 2;
std::string branch = getGitBranch();
if (!branch.empty()) {
promptLen += 1 + (int)branch.length();
}
// Handle Scrolling (pre-calc)
int startRow = promptStartRow;
if (startRow < 0) startRow = 0;
int cx = promptLen;
int cy = startRow;
for (size_t i = 0; i < inputBuffer.length(); i++) {
if (inputBuffer[i] == '\n') {
cx = 2; // Length of "> "
cy++;
} else {
cx++;
if (cx >= width) { cx = 0; cy++; }
}
}
int numLines = cy - startRow + 1;
int linesNeeded = startRow + numLines;
if (linesNeeded > height) {
int scrollAmount = linesNeeded - height;
io.setColor(IO::Console::COLOR_DEFAULT);
io.setCursorPos(0, (SHORT)(height - 1));
for (int i = 0; i < scrollAmount; i++) {
io.write("\n");
}
startRow = std::max(0, startRow - scrollAmount);
promptStartRow = startRow;
}
// Clear and Reset
io.setCursorPos(0, (SHORT)startRow);
printPrompt();
// Syntax Highlighting Loop
bool inQuotes = false;
char quoteChar = '\0';
bool isFirstToken = true;
size_t tokenStart = 0;
int selStart = -1, selEnd = -1;
if (selectionAnchor != -1) {
selStart = std::min(selectionAnchor, cursorPos);
selEnd = std::max(selectionAnchor, cursorPos);
}
std::string firstTokenStr;
if (!inputBuffer.empty()) {
size_t firstSpace = inputBuffer.find(' ');
if (firstSpace != std::string::npos) {
firstTokenStr = inputBuffer.substr(0, firstSpace);
} else {
firstTokenStr = inputBuffer;
}
}
bool isValidCommand = commandValidator ? commandValidator(firstTokenStr) : true;
for (size_t i = 0; i < inputBuffer.length(); i++) {
char c = inputBuffer[i];
// Selection Highlight Override
if (selStart != -1 && (int)i >= selStart && (int)i < selEnd) {
io.setColor(BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::string s(1, c); io.write(s);
continue; // Skip syntax highlighting for selected text
}
if (c == '\n') {
io.clearFromCursor();
io.setColor(IO::Console::COLOR_DEFAULT);
io.write("\n> ");
isFirstToken = true;
if (inQuotes) io.setColor(IO::Console::COLOR_STRING);
continue;
}
if ((c == '"' || c == '\'') && !inQuotes) {
inQuotes = true;
quoteChar = c;
io.setColor(IO::Console::COLOR_STRING);
std::string s(1, c); io.write(s);
continue;
}
if (c == quoteChar && inQuotes) {
std::string s(1, c); io.write(s);
inQuotes = false;
quoteChar = '\0';
io.setColor(IO::Console::COLOR_DEFAULT);
continue;
}
if (inQuotes) {
std::string s(1, c); io.write(s);
continue;
}
if (c == ' ') {
io.setColor(IO::Console::COLOR_DEFAULT);
io.write(" ");
isFirstToken = false;
tokenStart = i + 1;
continue;
}
// Color Logic
if (isFirstToken) {
if (!isValidCommand) {
io.setColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
} else {
io.setColor(IO::Console::COLOR_COMMAND);
}
} else if (c == '-') {
io.setColor(IO::Console::COLOR_FLAG);
} else {
bool isInFlag = false;
for (size_t j = tokenStart; j < i; j++) {
if (inputBuffer[j] == '-') { isInFlag = true; break; }
}
if (isInFlag) io.setColor(IO::Console::COLOR_FLAG);
else io.setColor(IO::Console::COLOR_ARG);
}
std::string s(1, c); io.write(s);
}
io.resetColor();
int endCx = promptLen;
int endCy = startRow;
for (size_t i = 0; i < inputBuffer.length(); i++) {
if (inputBuffer[i] == '\n') {
endCx = 2;
endCy++;
} else {
endCx++;
if (endCx >= width) { endCx = 0; endCy++; }
}
}
int ghostLines = 0;
if (!currentSuggestion.empty() && currentSuggestion.length() > inputBuffer.length()) {
std::string bufLower = inputBuffer;
std::string sugLower = currentSuggestion.substr(0, inputBuffer.length());
std::transform(bufLower.begin(), bufLower.end(), bufLower.begin(), ::tolower);
std::transform(sugLower.begin(), sugLower.end(), sugLower.begin(), ::tolower);
if (bufLower == sugLower) {
std::string suffix = currentSuggestion.substr(inputBuffer.length());
int remaining = width - endCx - 1; // Leave 1 char margin to prevent auto-scrolling
if (remaining <= 0) remaining = 0;
if ((int)suffix.length() > remaining) {
suffix = suffix.substr(0, remaining);
}
if (!suffix.empty()) {
io.setColor(IO::Console::COLOR_FAINT);
io.write(suffix);
io.resetColor();
}
}
}
io.clearFromCursor();
int totalVisualLines = numLines + ghostLines;
if (lastNumLines > totalVisualLines) {
io.clearArea(startRow + totalVisualLines, lastNumLines - totalVisualLines);
}
lastNumLines = totalVisualLines;
// Set Cursor
cx = promptLen;
cy = startRow;
for (int i = 0; i < cursorPos; i++) {
if (inputBuffer[i] == '\n') {
cx = 2; // Length of "> "
cy++;
} else {
cx++;
if (cx >= width) { cx = 0; cy++; }
}
}
io.setCursorPos((SHORT)cx, (SHORT)cy);
}
std::string sessionName;
public:
InputHandler(const std::string& cwd, const std::vector<std::string>& hist, bool admin = false, const std::string& sessName = "0")
: currentDir(cwd), history(hist), historyIndex(-1), cursorPos(0), lastNumLines(1), selectionAnchor(-1),
lastCharInput(0), initialized(false), isAdmin(admin), commandValidator(nullptr), sessionName(sessName) {
// Init row
promptStartRow = IO::get().getCursorPos().Y;
// Check Git Status once on init
currentGitStatus = GIT_NONE;
if (!getGitBranch().empty()) {
currentGitStatus = checkGitStatus();
}
rebuildSuggestions(); // Build the frequency index logic
}
void setCommandValidator(std::function<bool(const std::string&)> validator) {
commandValidator = validator;
}
std::string getInputBuffer() const { return inputBuffer; }
PollResult poll() {
if (!initialized) {
// Initial Render
render();
ShellIO::sout.registerPromptCallback([this](){
this->render();
});
ShellIO::sout.setPromptActive(true);
// REMOVE ENABLE_PROCESSED_INPUT manually just in case
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hIn, &mode);
// We assume InputDispatcher has already set raw mode or equivalent
lastTimeInput = std::chrono::steady_clock::now();
initialized = true;
}
SignalHandler::signalHeartbeat();
SignalHandler::poll();
INPUT_RECORD ir;
if (!SignalHandler::InputDispatcher::getInstance().getNextBufferedEvent(ir)) {
Sleep(1); // Yield CPU (Reduced latency)
return PollResult::Continue;
}
if (ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown) return PollResult::Continue;
WORD vk = ir.Event.KeyEvent.wVirtualKeyCode;
char ch = ir.Event.KeyEvent.uChar.AsciiChar;
DWORD ctrl = ir.Event.KeyEvent.dwControlKeyState;
// Unconditional Dead Key / Quote Fix
// Check if the key maps to a quote character physically
UINT mapped = MapVirtualKeyA(vk, 2) & 0xFF; // MAPVK_VK_TO_CHAR
// Check for single quote (') or backtick (`)
// We force the character if the key *physically* maps to it, ignoring dead state.
if (mapped == '\'' || mapped == '"') {
ch = (ctrl & SHIFT_PRESSED) ? '"' : '\'';
}
else if (mapped == '`' || mapped == '~') {
ch = (ctrl & SHIFT_PRESSED) ? '~' : '`';
}
else if (mapped == 0 && (vk == VK_OEM_7 || vk == VK_OEM_3)) {
// Fallback for layouts where MapVirtualKey might fail on dead keys
if (vk == VK_OEM_7) ch = (ctrl & SHIFT_PRESSED) ? '"' : '\'';
else if (vk == VK_OEM_3) ch = (ctrl & SHIFT_PRESSED) ? '~' : '`';
}
if (vk == VK_RETURN) {
// Fix: Clear any ghost text/suggestions before moving to next line
currentSuggestion.clear();
// History Expansion: !! and !$ (only handle this if we're not inside a multiline string from the start)
if (!history.empty() && !inputBuffer.empty()) {
std::string expanded;
size_t i = 0;
bool changed = false;
while (i < inputBuffer.length()) {
if (inputBuffer[i] == '!' && i + 1 < inputBuffer.length()) {
if (inputBuffer[i + 1] == '!') {
expanded += history.back();
changed = true;
i += 2;
continue;
} else if (inputBuffer[i + 1] == '$') {
std::string lastCmd = history.back();
// Find last token (space separated)
size_t lastSpace = lastCmd.find_last_of(" \t");
if (lastSpace != std::string::npos) {
expanded += lastCmd.substr(lastSpace + 1);
} else {
expanded += lastCmd;
}
changed = true;
i += 2;
continue;
}
}
expanded += inputBuffer[i];
i++;
}
if (changed) {
inputBuffer = expanded;
}
}
// Check for unclosed quotes
bool openSingle = false;
bool openDouble = false;
for (char c : inputBuffer) {
if (c == '\'' && !openDouble) openSingle = !openSingle;
else if (c == '"' && !openSingle) openDouble = !openDouble;
}
if (openSingle || openDouble) {
inputBuffer += '\n'; // Add newline and continue accepting input
cursorPos++;
render();
return PollResult::Continue;
}
render();
ShellIO::sout.setPromptActive(false);
ShellIO::sout << ShellIO::endl;
return PollResult::LineReady;
}
else if (vk == 'A' && (ctrl & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
selectionAnchor = 0;
cursorPos = (int)inputBuffer.length();
render();
}
else if (vk == 'L' && (ctrl & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
IO::get().clearScreen();
promptStartRow = 0;
render();
}
else if (vk == VK_BACK) {
lastCharInput = 0;
if (selectionAnchor != -1) {
deleteSelection();
updateSuggestion();
render();
}
else if ((ctrl & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) && cursorPos > 0) {
// Ctrl+Backspace: Delete word left
int i = cursorPos - 1;
while (i > 0 && inputBuffer[i] == ' ') i--; // Skip trailing spaces
while (i > 0 && inputBuffer[i] != ' ') i--; // Skip word characters
if (inputBuffer[i] == ' ') i++; // Keep the boundary space
int len = cursorPos - i;
inputBuffer.erase(i, len);
cursorPos = i;
updateSuggestion();
render();
}
else if (cursorPos > 0) {
// Smart Backspace: Delete matched empty pair
if (cursorPos < (int)inputBuffer.length()) {
char prev = inputBuffer[cursorPos - 1];
char next = inputBuffer[cursorPos];
bool isPair = (prev == '(' && next == ')') ||
(prev == '[' && next == ']') ||
(prev == '{' && next == '}') ||
(prev == '"' && next == '"') ||
(prev == '\'' && next == '\'') ||
(prev == '`' && next == '`');
if (isPair) {
inputBuffer.erase(cursorPos - 1, 2);
cursorPos--;
updateSuggestion();
render();
return PollResult::Continue; // Event handled
}
}
inputBuffer.erase(cursorPos - 1, 1);
cursorPos--;
updateSuggestion();
render();
}
}
else if (vk == VK_DELETE) {
lastCharInput = 0;
if (selectionAnchor != -1) {
deleteSelection();
updateSuggestion();
render();
} else if (cursorPos < (int)inputBuffer.length()) {
inputBuffer.erase(cursorPos, 1);
updateSuggestion();
render();
}
}
else if (vk == VK_HOME) {
lastCharInput = 0;
if (selectionAnchor != -1) selectionAnchor = -1;
cursorPos = 0;
render();
}
else if (vk == VK_END) {
lastCharInput = 0;
if (selectionAnchor != -1) selectionAnchor = -1;
cursorPos = (int)inputBuffer.length();
render();
}
else if (vk == VK_LEFT) {
lastCharInput = 0;
if (selectionAnchor != -1) {
// Normalize cursor to start of selection
cursorPos = std::min(selectionAnchor, cursorPos);
selectionAnchor = -1;
render();
}
else if (ctrl & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
// Word Jump Left
if (cursorPos > 0) {
int i = cursorPos - 1;
while (i > 0 && inputBuffer[i] == ' ') i--; // Skip trailing spaces
while (i > 0 && inputBuffer[i] != ' ') i--; // Skip word characters
if (inputBuffer[i] == ' ') i++; // Stop at boundary
cursorPos = i;
render();
}
}
else if (cursorPos > 0) {
cursorPos--; render();
}
}
else if (vk == VK_RIGHT) {
lastCharInput = 0;
// Accept Ghost Suggestion if at end of line and suggestion exists
if (cursorPos == inputBuffer.length() && !currentSuggestion.empty()) {
inputBuffer = currentSuggestion;
cursorPos = (int)inputBuffer.length();
updateSuggestion(); // Likely clears it or finds next
render();
}
else if (selectionAnchor != -1) {
// Normalize cursor to end of selection
cursorPos = std::max(selectionAnchor, cursorPos);
selectionAnchor = -1;
render();
}
else if (ctrl & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
// Word Jump Right
if (cursorPos < (int)inputBuffer.length()) {
int i = cursorPos;
while (i < (int)inputBuffer.length() && inputBuffer[i] != ' ') i++; // Skip word chars
while (i < (int)inputBuffer.length() && inputBuffer[i] == ' ') i++; // Skip spaces
cursorPos = i;
render();
}
}
else if (cursorPos < (int)inputBuffer.length()) {
cursorPos++; render();
} else {
// Accept Autosuggest
auto result = AutoSuggest::getSuggestions(inputBuffer, (int)inputBuffer.length(), currentDir);
if (!result.suggestions.empty()) {
std::string bestMatch = result.suggestions[0];
std::string suggestionSuffix;
if (result.isPath) {
// For paths, get the completion part
std::string currentToken = inputBuffer.substr(result.replaceStart, result.replaceLength);
fs::path tokenPath(currentToken);
std::string prefix = tokenPath.filename().string();
std::string lowerPrefix = prefix;
std::string lowerBest = bestMatch;
std::transform(lowerPrefix.begin(), lowerPrefix.end(), lowerPrefix.begin(), ::tolower);
std::transform(lowerBest.begin(), lowerBest.end(), lowerBest.begin(), ::tolower);
if (bestMatch.length() > prefix.length() &&
lowerBest.substr(0, prefix.length()) == lowerPrefix) {
suggestionSuffix = bestMatch.substr(prefix.length());
}
} else {
// For commands
std::string lowerInput = inputBuffer;
std::string lowerBest = bestMatch;
std::transform(lowerInput.begin(), lowerInput.end(), lowerInput.begin(), ::tolower);
std::transform(lowerBest.begin(), lowerBest.end(), lowerBest.begin(), ::tolower);
if (bestMatch.length() > inputBuffer.length() &&
lowerBest.substr(0, inputBuffer.length()) == lowerInput) {
suggestionSuffix = bestMatch.substr(inputBuffer.length());
}
}
if (!suggestionSuffix.empty()) {
inputBuffer += suggestionSuffix;
cursorPos = (int)inputBuffer.length();
render();
}
}
}
}
else if (vk == VK_UP) {
lastCharInput = 0;
if (!history.empty() && historyIndex < (int)history.size() - 1) {
historyIndex++;
inputBuffer = history[history.size() - 1 - historyIndex];
cursorPos = (int)inputBuffer.length();
updateSuggestion();
render();
}
}
else if (vk == VK_DOWN) {
lastCharInput = 0;
if (historyIndex > 0) {
historyIndex--;
inputBuffer = history[history.size() - 1 - historyIndex];
cursorPos = (int)inputBuffer.length();
updateSuggestion();
render();
} else if (historyIndex == 0) {
historyIndex = -1;
inputBuffer.clear(); cursorPos = 0;
updateSuggestion();
render();
}
}
else if (vk == VK_TAB) {
lastCharInput = 0;
// Tab - auto-complete
auto result = AutoSuggest::getSuggestions(inputBuffer, cursorPos, currentDir);
if (!result.suggestions.empty()) {
if (result.suggestions.size() == 1) {
// Single match - complete it
std::string completion = result.suggestions[0];
if (result.isPath) {
// Replace from replaceStart to cursorPos
std::string before = inputBuffer.substr(0, result.replaceStart);
std::string after = (cursorPos < (int)inputBuffer.length()) ? inputBuffer.substr(cursorPos) : "";
// Get parent path if any
std::string currentToken = inputBuffer.substr(result.replaceStart, result.replaceLength);
fs::path tokenPath(currentToken);
std::string parentPart;
if (!currentToken.empty() && currentToken.back() != '/' && currentToken.back() != '\\') {
fs::path parent = tokenPath.parent_path();
if (!parent.empty()) {
parentPart = parent.string();
if (parentPart.back() != '/' && parentPart.back() != '\\') {
parentPart += "/";
}
}
} else {
parentPart = currentToken;
}
inputBuffer = before + parentPart + completion + after;
cursorPos = (int)(before.length() + parentPart.length() + completion.length());
} else {
// Command completion
std::string after = (cursorPos < (int)inputBuffer.length()) ? inputBuffer.substr(cursorPos) : "";
inputBuffer = completion + " " + after;
cursorPos = (int)completion.length() + 1;
}
render();
} else {
// Multiple matches - show them and complete to common prefix
std::cout << std::endl;
// Display suggestions in columns
IO::Console& io = IO::get();
int termWidth = io.getWidth();
size_t maxLen = 0;
for (const auto& s : result.suggestions) {
if (s.length() > maxLen) maxLen = s.length();
}
int colWidth = (int)maxLen + 2;
int numCols = std::max(1, termWidth / colWidth);
int col = 0;
for (const auto& s : result.suggestions) {
io.setColor(FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
io.write(s);
io.resetColor();
col++;
if (col >= numCols) {
io.write("\n");
col = 0;
} else {
int padding = colWidth - (int)s.length();
io.write(std::string(padding, ' '));