-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwa.cpp
More file actions
2195 lines (1967 loc) · 111 KB
/
Copy pathwa.cpp
File metadata and controls
2195 lines (1967 loc) · 111 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
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <ctime>
using namespace std;
// Struct untuk menyimpan data pesan
struct Pesan {
string text;
bool isRead;
Pesan() : text(""), isRead(false) {}
Pesan(string t, bool r = false) : text(t), isRead(r) {}
};
// Node untuk Doubly Linked List
struct Node {
Pesan data;
Node* next;
Node* prev;
Node(Pesan p) : data(p), next(nullptr), prev(nullptr) {}
};
// Class Queue menggunakan Doubly Linked List
class QueuePesan {
private:
Node* front; // Depan antrian (untuk dequeue)
Node* rear; // Belakang antrian (untuk enqueue)
int size;
public:
QueuePesan() : front(nullptr), rear(nullptr), size(0) {}
// Destructor untuk membersihkan memory
~QueuePesan() {
while (front != nullptr) {
Node* temp = front;
front = front->next;
delete temp;
}
}
// Mengecek apakah queue kosong
bool isEmpty() {
return front == nullptr;
}
// Mendapatkan jumlah pesan
int getSize() {
return size;
}
// Enqueue - Menambah pesan baru (dengan spam filter)
bool enqueue(string text) {
// Spam Filter: Cek apakah pesan sama dengan pesan yang sudah ada
Node* current = front;
while (current != nullptr) {
if (current->data.text == text) {
return false; // Tolak pesan (spam)
}
current = current->next;
}
Pesan newPesan(text, false);
Node* newNode = new Node(newPesan);
if (isEmpty()) {
front = rear = newNode;
} else {
rear->next = newNode;
newNode->prev = rear;
rear = newNode;
}
size++;
return true;
}
// Dequeue - Menghapus pesan pertama
bool dequeue() {
if (isEmpty()) {
return false;
}
Node* temp = front;
front = front->next;
if (front == nullptr) {
rear = nullptr;
} else {
front->prev = nullptr;
}
delete temp;
size--;
return true;
}
// Membatalkan pesan terakhir (Rear) - Fitur khusus Doubly Linked List
bool cancelLast() {
if (isEmpty()) {
return false;
}
Node* temp = rear;
rear = rear->prev;
if (rear == nullptr) {
front = nullptr;
} else {
rear->next = nullptr;
}
delete temp;
size--;
return true;
}
// Menandai pesan sebagai dibaca
bool markAsRead(int index) {
if (isEmpty() || index < 0 || index >= size) {
return false;
}
Node* current = front;
for (int i = 0; i < index && current != nullptr; i++) {
current = current->next;
}
if (current != nullptr) {
current->data.isRead = true;
return true;
}
return false;
}
// Mendapatkan pointer ke front (untuk display)
Node* getFront() {
return front;
}
};
// Fungsi gotoxy untuk positioning di terminal
void gotoxy(int x, int y) {
printf("\033[%d;%dH", y, x);
}
// Fungsi untuk clear screen
void clearScreen() {
printf("\033[2J");
printf("\033[H");
fflush(stdout);
}
// Fungsi untuk set warna text
void setColor(int color) {
printf("\033[%dm", color);
}
// Reset warna
void resetColor() {
printf("\033[0m");
}
// Fungsi untuk convert string ke lowercase
string toLowerCase(string str) {
string result = str;
for (int i = 0; i < (int)result.length(); i++) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] = result[i] + ('a' - 'A');
}
}
return result;
}
// Fungsi untuk cek apakah string hanya berisi whitespace/newline
bool isEmptyOrWhitespace(const string& str) {
for (int i = 0; i < (int)str.length(); i++) {
if (str[i] != ' ' && str[i] != '\n' && str[i] != '\t' && str[i] != '\r') {
return false; // Ada karakter selain whitespace
}
}
return true; // Hanya whitespace atau kosong
}
// Fungsi untuk parse input multiple message numbers
// Format: "1,3,5" atau "1-3" atau "1,3-5,7" atau "all"
// Return: vector berisi index yang dipilih (0-based)
vector<int> parseMessageNumbers(const string& input, int maxSize) {
vector<int> result;
if (toLowerCase(input) == "all") {
for (int i = 0; i < maxSize; i++) {
result.push_back(i);
}
return result;
}
string current = "";
for (int i = 0; i <= (int)input.length(); i++) {
char ch = (i < (int)input.length()) ? input[i] : ',';
if (ch == ',' || ch == ' ' || i == (int)input.length()) {
if (!current.empty()) {
// Check if it's a range (e.g., "1-3")
int dashPos = -1;
for (int j = 0; j < (int)current.length(); j++) {
if (current[j] == '-') {
dashPos = j;
break;
}
}
if (dashPos != -1) {
// Range format
string startStr = current.substr(0, dashPos);
string endStr = current.substr(dashPos + 1);
int start = atoi(startStr.c_str());
int end = atoi(endStr.c_str());
if (start > 0 && end > 0 && start <= end && end <= maxSize) {
for (int j = start; j <= end; j++) {
result.push_back(j - 1); // Convert to 0-based
}
}
} else {
// Single number
int num = atoi(current.c_str());
if (num > 0 && num <= maxSize) {
result.push_back(num - 1); // Convert to 0-based
}
}
current = "";
}
} else {
current += ch;
}
}
return result;
}
// Fungsi untuk enable mouse tracking
void enableMouseTracking() {
printf("\033[?1000h"); // Enable mouse button tracking
printf("\033[?1002h"); // Enable mouse motion tracking
printf("\033[?1015h"); // Enable urxvt mouse mode
printf("\033[?1006h"); // Enable SGR mouse mode
}
// Fungsi untuk disable mouse tracking
void disableMouseTracking() {
printf("\033[?1000l");
printf("\033[?1002l");
printf("\033[?1015l");
printf("\033[?1006l");
}
// Fungsi untuk set terminal ke raw mode
struct termios orig_termios;
void disableRawMode() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
fflush(stdout);
}
void enableRawMode() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
// Fungsi untuk menampilkan header
void displayHeader() {
setColor(42); // Background hijau
setColor(30); // Text hitam
gotoxy(1, 1);
printf("╔════════════════════════════════════════════════════════════════════════╗");
gotoxy(1, 2);
printf("║ WhatsApp Simulator - Queue Implementation ║");
gotoxy(1, 3);
printf("╚════════════════════════════════════════════════════════════════════════╝");
resetColor();
fflush(stdout);
}
// Fungsi untuk menampilkan menu
void displayMenu(int selected) {
int menuY = 5;
int menuX = 10;
gotoxy(menuX, menuY);
printf("╔══════════════════════════════════════════════╗");
string options[] = {
"1. Tambah Pesan Baru",
"2. Hapus Pesan Pertama",
"3. Batalkan Pesan Terakhir",
"4. Lihat Semua Pesan",
"5. Tandai Sebagai Dibaca",
"6. Keluar"
};
for (int i = 0; i < 6; i++) {
gotoxy(menuX, menuY + 1 + i);
if (i == selected) {
setColor(47); // Background putih
setColor(30); // Text hitam
printf("║ > %-43s║", options[i].c_str());
resetColor();
} else {
printf("║ %-43s║", options[i].c_str());
}
}
gotoxy(menuX, menuY + 7);
printf("╚══════════════════════════════════════════════╝");
gotoxy(menuX, menuY + 9);
setColor(36); // Cyan
printf("Mouse: Klik 2x | Keyboard: ↑↓ Enter");
resetColor();
fflush(stdout);
}
// Fungsi untuk menampilkan tombol kembali
bool displayBackButton(int y, int& lastClickedButton, time_t& lastClickTime) {
int buttonY = y;
int buttonX = 10;
gotoxy(buttonX, buttonY);
setColor(37); // Tanpa highlight karena hanya 1 tombol
printf("╔════════════════════╗");
gotoxy(buttonX, buttonY + 1);
printf("║ [← KEMBALI] ║");
gotoxy(buttonX, buttonY + 2);
printf("╚════════════════════╝");
resetColor();
gotoxy(buttonX, buttonY + 4);
setColor(36);
printf("Enter/Klik 2x untuk kembali");
resetColor();
fflush(stdout);
const int DOUBLE_CLICK_THRESHOLD = 500;
// Read input
char c;
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == '\033') { // ESC sequence
char seq[5];
if (read(STDIN_FILENO, &seq[0], 1) == 1) {
if (seq[0] == '[') {
if (read(STDIN_FILENO, &seq[1], 1) == 1) {
if (seq[1] == '<') { // Mouse input
char mouseData[20];
int idx = 0;
while (read(STDIN_FILENO, &mouseData[idx], 1) == 1 && mouseData[idx] != 'M' && mouseData[idx] != 'm') {
idx++;
}
if (mouseData[idx] == 'M') { // Mouse click
int mouseY = 0;
sscanf(mouseData, "%*d;%*d;%d", &mouseY);
// Check if clicked on back button
if (mouseY >= buttonY && mouseY <= buttonY + 2) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
time_t currentTime = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
if (lastClickedButton == 1 &&
(currentTime - lastClickTime) < DOUBLE_CLICK_THRESHOLD) {
return true; // Double click - go back
} else {
lastClickedButton = 1;
lastClickTime = currentTime;
}
}
}
}
}
}
}
} else if (c == '\n' || c == '\r') { // Enter key
return true; // Confirm - go back
}
}
return false;
}
// Fungsi untuk menampilkan pesan
void displayMessages(QueuePesan& queue) {
int lastClickedButton = -1;
time_t lastClickTime = 0;
bool shouldReturn = false;
while (!shouldReturn) {
clearScreen();
displayHeader();
gotoxy(10, 5);
setColor(33); // Kuning
printf("═══════════════════ DAFTAR PESAN ═══════════════════");
resetColor();
int y = 7;
if (queue.isEmpty()) {
gotoxy(10, y);
setColor(31); // Merah
printf("Tidak ada pesan.");
resetColor();
y++;
} else {
Node* current = queue.getFront();
int index = 0;
while (current != nullptr) {
// Render pesan dengan multi-line support
string pesan = current->data.text;
bool isRead = current->data.isRead;
// Split pesan berdasarkan newline
int startPos = 0;
bool firstLine = true;
for (int i = 0; i <= (int)pesan.length(); i++) {
if (i == (int)pesan.length() || pesan[i] == '\n') {
gotoxy(10, y);
if (firstLine) {
// Baris pertama dengan indikator
if (isRead) {
setColor(37); // Abu-abu (sudah dibaca)
printf("[√] Pesan %d: ", index + 1);
} else {
setColor(32); // Hijau (belum dibaca)
printf("[-] Pesan %d: ", index + 1);
}
} else {
// Baris selanjutnya dengan indentasi
char indent[50];
snprintf(indent, sizeof(indent), "[%c] Pesan %d: ", ' ', index + 1);
for (int j = 0; j < (int)strlen(indent); j++) {
printf(" ");
}
}
// Print substring
string line = pesan.substr(startPos, i - startPos);
if (isRead) {
setColor(37);
} else {
setColor(32);
}
printf("%s", line.c_str());
resetColor();
y++;
startPos = i + 1;
firstLine = false;
}
}
current = current->next;
index++;
}
}
gotoxy(10, y + 2);
setColor(36);
printf("Total pesan: %d", queue.getSize());
resetColor();
shouldReturn = displayBackButton(y + 4, lastClickedButton, lastClickTime);
}
}
// Fungsi untuk input pesan baru
void inputNewMessage(QueuePesan& queue) {
int lastClickedButton = -1;
time_t lastClickTime = 0;
bool shouldReturn = false;
string message = "";
bool messageSent = false;
int selectedButton = 0; // 0 = Kirim, 1 = Kembali
int focusArea = 0; // 0 = text input, 1 = buttons
int cursorPos = 0; // Posisi cursor dalam text
int selectionStart = -1; // -1 berarti tidak ada seleksi
int selectionEnd = -1;
int lastCursorPos = 0; // Untuk deteksi Ctrl+Shift alternative
// For mouse double-click word selection
time_t lastMouseClickTime = 0;
int lastMouseClickPos = -1;
bool isMousePressed = false; // Track apakah mouse sedang ditekan
bool isWordSelectionMode = false; // Mode seleksi per kata dengan drag
int wordSelectionAnchorStart = -1; // Anchor point saat double-click
int wordSelectionAnchorEnd = -1;
const int MOUSE_DOUBLE_CLICK_THRESHOLD = 500; // milliseconds
// For double-click Ctrl+Arrow detection
time_t lastCtrlArrowTime = 0;
char lastCtrlArrowDir = 0; // 'C' for right, 'D' for left
const int CTRL_ARROW_DOUBLE_CLICK_THRESHOLD = 500; // milliseconds
while (!shouldReturn) {
clearScreen();
displayHeader();
gotoxy(10, 5);
setColor(33);
printf("═══════════════════ TAMBAH PESAN BARU ═══════════════════");
resetColor();
if (!messageSent) {
// Tampilkan area input dengan cursor (multi-line)
const int INPUT_WIDTH = 55; // Lebar area input tetap
const int INPUT_START_X = 10;
const int INPUT_START_Y = 7;
gotoxy(INPUT_START_X, INPUT_START_Y);
printf("Masukkan pesan:");
// Hitung jumlah baris yang diperlukan
int totalChars = message.length() + 1; // +1 untuk cursor
int numLines = 1;
// Hitung baris berdasarkan width dan newline
int charCount = 0;
for (int i = 0; i < (int)message.length(); i++) {
if (message[i] == '\n') {
numLines++;
charCount = 0;
} else {
charCount++;
if (charCount >= INPUT_WIDTH) {
numLines++;
charCount = 0;
}
}
}
// Tambah 1 baris untuk cursor jika di akhir
if (charCount > 0 || message.empty()) {
// Sudah termasuk di numLines
}
// Render multi-line text area
int currentLine = 0;
int currentCol = 0;
int charIndex = 0;
for (int line = 0; line < numLines && line < 20; line++) { // Max 20 baris
gotoxy(INPUT_START_X, INPUT_START_Y + 1 + line);
// Set background color
if (focusArea == 0) {
setColor(47); setColor(30); // Highlight area input
} else {
setColor(37);
}
// Render karakter per baris
int renderedChars = 0;
bool lineBreak = false;
while (renderedChars < INPUT_WIDTH && charIndex <= (int)message.length()) {
// Render cursor SEBELUM karakter di posisi ini
if (charIndex == cursorPos && focusArea == 0 &&
(selectionStart == -1 || selectionEnd == -1 || selectionStart == selectionEnd)) {
printf("│");
renderedChars++;
if (renderedChars >= INPUT_WIDTH) break;
}
// Cek apakah posisi ini terseleksi
bool isSelected = false;
if (selectionStart != -1 && selectionEnd != -1 && selectionStart != selectionEnd) {
int selStart = selectionStart < selectionEnd ? selectionStart : selectionEnd;
int selEnd = selectionStart < selectionEnd ? selectionEnd : selectionStart;
if (charIndex >= selStart && charIndex < selEnd) {
isSelected = true;
}
}
// Set warna untuk seleksi
if (isSelected) {
resetColor();
setColor(40); // Background hitam
setColor(37); // Text putih
}
// Render karakter
if (charIndex < (int)message.length()) {
char ch = message[charIndex];
if (ch == '\n') {
// Newline - isi sisa baris dengan spasi lalu break
while (renderedChars < INPUT_WIDTH) {
printf(" ");
renderedChars++;
}
charIndex++;
lineBreak = true;
break;
} else {
printf("%c", ch);
renderedChars++;
charIndex++;
}
} else {
// Sudah di akhir message, padding dengan spasi
printf(" ");
renderedChars++;
charIndex++;
}
// Kembalikan warna jika habis seleksi
if (isSelected) {
resetColor();
if (focusArea == 0) {
setColor(47); setColor(30);
} else {
setColor(37);
}
}
}
// Padding sisa baris jika belum penuh
while (renderedChars < INPUT_WIDTH) {
printf(" ");
renderedChars++;
}
resetColor();
// Jika line break, lanjut ke baris berikutnya
if (lineBreak) continue;
}
// Update posisi tombol berdasarkan jumlah baris
int buttonY = INPUT_START_Y + 1 + numLines + 1;
// Tombol Kirim dan Kembali
gotoxy(10, buttonY);
if (focusArea == 1 && selectedButton == 0) {
setColor(47); setColor(30);
}
printf("╔════════════════╗");
resetColor();
printf(" ");
if (focusArea == 1 && selectedButton == 1) {
setColor(47); setColor(30);
}
printf("╔════════════════════╗");
resetColor();
gotoxy(10, buttonY + 1);
if (focusArea == 1 && selectedButton == 0) {
setColor(47); setColor(30);
}
printf("║ [✓ KIRIM] ║");
resetColor();
printf(" ");
if (focusArea == 1 && selectedButton == 1) {
setColor(47); setColor(30);
}
printf("║ [← KEMBALI] ║");
resetColor();
gotoxy(10, buttonY + 2);
if (focusArea == 1 && selectedButton == 0) {
setColor(47); setColor(30);
}
printf("╚════════════════╝");
resetColor();
printf(" ");
if (focusArea == 1 && selectedButton == 1) {
setColor(47); setColor(30);
}
printf("╚════════════════════╝");
resetColor();
gotoxy(10, buttonY + 4);
setColor(36);
if (focusArea == 0) {
printf("←→ kursor | Ctrl+←→ loncat kata | Shift+←→ blok | Alt+←→ blok kata");
} else {
printf("Tombol: ←→ pilih | Enter/Klik 2x konfirmasi | ↑ ke text");
}
resetColor();
fflush(stdout);
// Read input
char c;
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == '\033') { // ESC sequence
char seq[10];
memset(seq, 0, sizeof(seq));
if (read(STDIN_FILENO, &seq[0], 1) == 1) {
if (seq[0] == '[') {
if (read(STDIN_FILENO, &seq[1], 1) == 1) {
// Check for extended sequences (Shift, Ctrl+Shift)
if (seq[1] == '1') {
if (read(STDIN_FILENO, &seq[2], 1) == 1) {
if (seq[2] == ';') {
if (read(STDIN_FILENO, &seq[3], 1) == 1) {
if (read(STDIN_FILENO, &seq[4], 1) == 1) {
// Modifier values:
// 2 = Shift
// 3 = Alt
// 4 = Shift+Alt
// 5 = Ctrl
// 6 = Ctrl+Shift
// 7 = Ctrl+Alt
// 8 = Ctrl+Shift+Alt
bool isShift = (seq[3] == '2');
bool isCtrl = (seq[3] == '5');
bool isCtrlShift = (seq[3] == '6');
bool isAlt = (seq[3] == '3');
// Handle Alt+Arrow (word selection)
if (focusArea == 0 && isAlt) {
if (seq[4] == 'C') { // Alt+Right - extend selection word right
if (selectionStart == -1) {
// Mulai seleksi dari posisi saat ini
selectionStart = cursorPos;
selectionEnd = cursorPos;
}
// Extend dari ujung seleksi (yang lebih besar)
int extendFrom = (selectionStart < selectionEnd) ? selectionEnd : selectionStart;
cursorPos = extendFrom;
// Skip spasi jika ada di posisi saat ini
while (cursorPos < (int)message.length() && message[cursorPos] == ' ') {
cursorPos++;
}
// Blok 1 kata (sampai ketemu spasi atau akhir)
while (cursorPos < (int)message.length() && message[cursorPos] != ' ') {
cursorPos++;
}
// Update selectionEnd
selectionEnd = cursorPos;
} else if (seq[4] == 'D') { // Alt+Left - extend selection word left
if (selectionStart == -1) {
// Mulai seleksi dari posisi saat ini
selectionStart = cursorPos;
selectionEnd = cursorPos;
}
// Extend dari ujung seleksi (yang lebih kecil)
int extendFrom = (selectionStart < selectionEnd) ? selectionStart : selectionEnd;
cursorPos = extendFrom;
if (cursorPos > 0) {
cursorPos--;
// Skip spasi jika ada di posisi saat ini
while (cursorPos > 0 && message[cursorPos] == ' ') {
cursorPos--;
}
// Blok 1 kata ke kiri (sampai ketemu spasi atau awal)
while (cursorPos > 0 && message[cursorPos - 1] != ' ') {
cursorPos--;
}
}
// Update selectionStart (untuk extend ke kiri)
selectionStart = cursorPos;
}
}
// Handle Ctrl+Arrow (jump per word without selection)
if (focusArea == 0 && isCtrl && !isCtrlShift && !isShift) {
if (seq[4] == 'C') { // Ctrl+Right
// Jump per kata ke kanan (tanpa seleksi)
selectionStart = -1;
selectionEnd = -1;
if (cursorPos < (int)message.length() && message[cursorPos] == ' ') {
while (cursorPos < (int)message.length() && message[cursorPos] == ' ') {
cursorPos++;
}
} else {
while (cursorPos < (int)message.length() && message[cursorPos] != ' ') {
cursorPos++;
}
}
} else if (seq[4] == 'D') { // Ctrl+Left
// Jump per kata ke kiri (tanpa seleksi)
selectionStart = -1;
selectionEnd = -1;
if (cursorPos > 0) {
cursorPos--;
if (message[cursorPos] == ' ') {
while (cursorPos > 0 && message[cursorPos] == ' ') {
cursorPos--;
}
if (cursorPos > 0 || message[0] != ' ') {
cursorPos++;
}
} else {
while (cursorPos > 0 && message[cursorPos - 1] != ' ') {
cursorPos--;
}
}
}
}
}
if (focusArea == 0 && (isShift || isCtrlShift)) {
if (seq[4] == 'C') { // Shift+Right atau Ctrl+Shift+Right
if (selectionStart == -1) {
selectionStart = cursorPos;
}
if (isCtrlShift) {
// Blok per kata ke kanan
// Jika di spasi, skip spasi sampai ketemu huruf
if (cursorPos < (int)message.length() && message[cursorPos] == ' ') {
while (cursorPos < (int)message.length() && message[cursorPos] == ' ') {
cursorPos++;
}
} else {
// Jika di huruf, maju sampai ketemu spasi atau akhir
while (cursorPos < (int)message.length() && message[cursorPos] != ' ') {
cursorPos++;
}
}
} else {
// Geser 1 karakter
if (cursorPos < (int)message.length()) cursorPos++;
}
selectionEnd = cursorPos;
} else if (seq[4] == 'D') { // Shift+Left atau Ctrl+Shift+Left
if (selectionStart == -1) {
selectionStart = cursorPos;
}
if (isCtrlShift) {
// Blok per kata ke kiri
// Mundur satu langkah dulu
if (cursorPos > 0) {
cursorPos--;
// Jika di spasi, skip spasi sampai ketemu huruf
if (message[cursorPos] == ' ') {
while (cursorPos > 0 && message[cursorPos] == ' ') {
cursorPos--;
}
// Posisikan di akhir kata sebelumnya
if (cursorPos > 0 || message[0] != ' ') {
cursorPos++;
}
} else {
// Jika di huruf, mundur sampai ketemu spasi atau awal
while (cursorPos > 0 && message[cursorPos - 1] != ' ') {
cursorPos--;
}
}
}
} else {
// Geser 1 karakter
if (cursorPos > 0) cursorPos--;
}
selectionEnd = cursorPos;
} else if (seq[4] == 'A') { // Shift+Up
if (selectionStart == -1) {
selectionStart = cursorPos;
}
// Geser ke atas 1 baris (55 karakter)
const int INPUT_WIDTH = 55;
if (cursorPos >= INPUT_WIDTH) {
cursorPos -= INPUT_WIDTH;
} else {
cursorPos = 0; // Ke awal text
}
selectionEnd = cursorPos;
} else if (seq[4] == 'B') { // Shift+Down
if (selectionStart == -1) {
selectionStart = cursorPos;
}
// Geser ke bawah 1 baris (55 karakter)
const int INPUT_WIDTH = 55;
if (cursorPos + INPUT_WIDTH <= (int)message.length()) {
cursorPos += INPUT_WIDTH;
} else {
cursorPos = message.length(); // Ke akhir text
}
selectionEnd = cursorPos;
}
}
}
}
}
}
} else if (seq[1] == 'A') { // Up arrow
if (focusArea == 1) {
focusArea = 0; // Kembali ke text input
} else if (focusArea == 0) {
// Navigasi ke atas 1 baris dalam text
const int INPUT_WIDTH = 55;
// Hitung posisi baris saat ini berdasarkan newline
int currentLine = 0;
int charCount = 0;
for (int i = 0; i < cursorPos && i < (int)message.length(); i++) {
if (message[i] == '\n') {
currentLine++;
charCount = 0;
} else {
charCount++;
if (charCount >= INPUT_WIDTH) {
currentLine++;
charCount = 0;
}
}
}
if (currentLine > 0) {
// Ada baris di atas, naik
int targetLine = currentLine - 1;
int newPos = 0;
int line = 0;
charCount = 0;
for (int i = 0; i <= (int)message.length(); i++) {
if (line == targetLine && charCount >= INPUT_WIDTH) {
break;
}
if (line == targetLine + 1) {
break;
}
newPos = i;
if (i < (int)message.length()) {
if (message[i] == '\n') {
line++;
charCount = 0;
} else {
charCount++;
if (charCount >= INPUT_WIDTH) {
line++;
charCount = 0;
}
}
}
}
cursorPos = newPos;
} else {
cursorPos = 0; // Ke awal text
}
// Clear selection
selectionStart = -1;
selectionEnd = -1;
}
} else if (seq[1] == 'B') { // Down arrow
if (focusArea == 0) {
// Hitung total baris dalam message
int totalLines = 1; // Minimal 1 baris
int charCount = 0;
for (int i = 0; i < (int)message.length(); i++) {
if (message[i] == '\n') {
totalLines++;
charCount = 0;
} else {
charCount++;
if (charCount >= INPUT_WIDTH) {
totalLines++;
charCount = 0;
}
}
}
// Hitung baris saat ini
int currentLine = 0;
charCount = 0;
for (int i = 0; i < cursorPos && i < (int)message.length(); i++) {
if (message[i] == '\n') {
currentLine++;
charCount = 0;
} else {