-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
1772 lines (1527 loc) · 63.1 KB
/
Program.cs
File metadata and controls
1772 lines (1527 loc) · 63.1 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
using Spectre.Console;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using TextCopy;
using DotNetEnv;
namespace NoteWorthy;
class Program
{
public static Spectre.Console.Style DefaultStyle = new Style(Color.White, Color.Grey46);
public static string RECYCLE_BIN_PATH = Path.Combine(Directory.GetCurrentDirectory(), "recycle_bin");
/// <summary>
/// The path to the root 'notes' directory which contains all the notes
/// </summary>
public static string NOTES_DIR_PATH = Path.Combine(Directory.GetCurrentDirectory(), "notes");
private static NoteTree noteTree = new NoteTree();
private static NoteEditor noteEditor = new NoteEditor(null);
/// <summary>
/// The layout of the display. Gets printed every time an update is required.
/// </summary>
private static Layout display_layout = GenerateEmptyLayout();
/// <summary>
/// Set to true when a new note is loaded (and therefore a new NoteEditor is created) in order to render the new note.
/// </summary>
private static bool noteEditorRequiresUpdate = true;
/// <summary>
/// Set to true when the editor (the right side) is focused. Set to false when the tree (the left side) is focused.
/// </summary>
private static bool editorFocused = false;
/// <summary>
/// Set to true when the tree footer requires an update
/// </summary>
private static bool treeFooterRequiresUpdate = true;
// For allowing ctrl+s as a shortcut
[DllImport("kernel32.dll")]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, int dwMode);
public static void Main()
{
// Setup
Env.Load();
Enable_CtrlC_Shortcut();
Allow_CtrlS_Shortcut();
SetBlockCursor();
InitializeNotesDirectory();
StartResizeEventListener();
// Mainloop with error handling for unknown bugs
try
{
Mainloop();
}
catch (Exception ex)
{
HandleFatalError(ex);
}
}
private static void Enable_CtrlC_Shortcut()
{
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
// If the editor is focused, ctrl+c will copy highlighted text to clipboard or the whole note if no highlighted text
if (editorFocused)
{
if (noteEditor.SomeCharsAreHighlighted())
{
noteEditor.CopyHighlightedText();
Set_NoteEditorRequiresUpdate();
}
}
// If the note tree is focused, ctrl+c will create a copy of the selected file
else
{
bool success = noteTree.CopySelectedTreeItem();
if (!success) return;
TreeItem selected_tree_item = noteTree.GetSelectedTreeItem()!;
if (selected_tree_item.Parent == null)
{
noteTree = new NoteTree();
}
else
{
noteTree = new NoteTree(selected_tree_item.Parent.FilePath);
}
}
};
}
private static void SetBlockCursor()
{
Console.Write("\u001b[2 q");
Console.SetCursorPosition(0, 0);
}
private static void Allow_CtrlS_Shortcut()
{
IntPtr consoleHandle = GetStdHandle(-10);
SetConsoleMode(consoleHandle, 0x0001);
}
/// <summary>
/// Create the notes directory if it doesn't exist.
/// The notes directory is where all the saved notes go.
/// </summary>
private static void InitializeNotesDirectory()
{
if (!Directory.Exists(NOTES_DIR_PATH))
{
Directory.CreateDirectory(NOTES_DIR_PATH);
}
}
private static void StartResizeEventListener()
{
Task.Run(() =>
{
int width = Console.WindowWidth;
int height = Console.WindowHeight;
while (true)
{
if (width != Console.WindowWidth || height != Console.WindowHeight)
{
if (Console.WindowHeight < 10)
{
Console.Clear();
while (Console.WindowHeight < 10)
{
Console.SetCursorPosition(0, 0);
Console.WriteLine("Screen height too small.");
}
}
width = Console.WindowWidth;
height = Console.WindowHeight;
Console.Clear();
// Update buffers to reflect new screen size and regenerate layout component based on the new buffers
noteEditor.UpdateBuffers();
NoteTree.UpdateBuffers();
display_layout = GenerateEmptyLayout();
// Update display
Set_NoteEditorRequiresUpdate();
noteTree.Set_RequiresUpdate();
SetTreeFooterRequiresUpdate();
}
}
});
}
private static void Mainloop()
{
while (true)
{
HandleRendering();
if (!Console.KeyAvailable)
{
// Check for auto save. if auto save is required, save the note. finally, continue.
if (editorFocused && noteEditor.CheckIfAutoSaveRequired())
{
HandleSave();
}
continue;
}
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (editorFocused)
{
HandleEditorInput(keyInfo);
}
else
{
HandleTreeInput(keyInfo);
}
}
}
private static bool last_was_visible = false;
/// <summary>
/// Rerender the screen if it is necessary
/// </summary>
private static void HandleRendering()
{
var treeItemsAndFooterLayout = display_layout.GetLayout("TreeItemsAndFooter");
var treeItemsLayout = display_layout.GetLayout("TreeItems");
var treeFooterLayout = display_layout.GetLayout("TreeFooter");
var noteEditorLayout = display_layout.GetLayout("NoteEditor");
bool note_tree_requires_update = noteTree.Get_RequiresUpdate();
// Note tree
if (note_tree_requires_update)
{
visible:;
if (noteTree.IsVisible() && !last_was_visible)
{
treeItemsAndFooterLayout.Visible();
treeFooterLayout.Visible();
treeItemsLayout.Visible();
last_was_visible = true;
noteEditor = new NoteEditor(noteEditor.GetNotePath());
editorFocused = false;
note_tree_requires_update = true;
SetTreeFooterRequiresUpdate();
Set_NoteEditorRequiresUpdate();
}
else if (!noteTree.IsVisible() && last_was_visible)
{
treeItemsAndFooterLayout.Invisible();
treeFooterLayout.Invisible();
treeItemsLayout.Invisible();
last_was_visible = false;
noteEditor = new NoteEditor(noteEditor.GetNotePath());
if (noteEditor.IsTypingDisabled())
{
// Re-render
noteTree.ToggleVisibility();
goto visible;
}
else
{
editorFocused = true;
}
note_tree_requires_update = true;
SetTreeFooterRequiresUpdate();
Set_NoteEditorRequiresUpdate();
}
var panel = noteTree.GenerateDisplayPanel();
// The tree must be focused if the editor isn't
if (!editorFocused)
{
panel.BorderColor(Color.Aqua);
}
// Will only rewrite if there's been a change to them
treeItemsLayout.Update(panel);
}
// Note editor
if (noteEditorRequiresUpdate)
{
var panel = noteEditor.GenerateDisplayPanel();
if (editorFocused)
{
panel.BorderColor(Color.Aqua);
}
noteEditorLayout.Update(panel);
}
// Tree footer
if (treeFooterRequiresUpdate)
{
var panel = GenerateTreeFooterPanel();
treeFooterLayout.Update(panel);
}
// If any of the panels require an update, then update the display
if (note_tree_requires_update || noteEditorRequiresUpdate || treeFooterRequiresUpdate)
{
AnsiConsole.Cursor.Hide();
noteEditorRequiresUpdate = false;
treeFooterRequiresUpdate = false;
//SetBlockCursor();
Console.SetCursorPosition(0, 0);
AnsiConsole.Write(display_layout);
if (editorFocused)
{
noteEditor.UpdateCursorPosInEditor();
AnsiConsole.Cursor.Show();
}
}
}
private static void HandleTreeInput(ConsoleKeyInfo keyInfo)
{
if (keyInfo.Key == ConsoleKey.J && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))
{
// This function will handle the closing of the window, too.
OpenAIWindow();
treeFooterRequiresUpdate = true;
SetTreeFooterRequiresUpdate();
Set_NoteEditorRequiresUpdate();
return;
}
// For shortcuts with the control key
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))
{
switch (keyInfo.Key)
{
// Ctrl+Q - Quit \ close application
case ConsoleKey.Q:
ExitApplication();
break;
// Ctrl+O - Open the currently selected dir in the file explorer
case ConsoleKey.O:
string path = noteTree.GetSelectedTreeItem()?.Parent == null ? NOTES_DIR_PATH : noteTree.GetSelectedTreeItem()!.Parent!.FilePath;
Process.Start("explorer.exe", path);
break;
// Ctrl+Up Arrow - Move up one and open the file. Same as Ctrl+UpArrow in the editor
case ConsoleKey.UpArrow:
case ConsoleKey.PageUp:
HandleCtrlUpArrow();
break;
// Ctrl+Down Arrow - Move down one and open the file. Same as Ctrl+DownArrow in the editor
case ConsoleKey.DownArrow:
case ConsoleKey.PageDown:
HandleCtrlDownArrow();
break;
// Ctrl+W - Close the note in the editor if there's a note, otherwise close the app
case ConsoleKey.W:
if (noteEditor.GetNotePath() == null)
{
// If the user presses Ctrl+W when there is no note, close the app
ExitApplication();
}
else
{
noteEditor = new NoteEditor(null);
noteTree.SetVisible();
Set_NoteEditorRequiresUpdate();
// Move focus to the tree
if (noteTree.IsVisible()) editorFocused = false;
noteTree.Set_RequiresUpdate();
SetTreeFooterRequiresUpdate();
}
break;
// Ctrl+N - Create new file
case ConsoleKey.N:
HandleCreateNewFile();
break;
// Ctrl+M - Create new folder
case ConsoleKey.M:
HandleCreateNewFolder();
break;
// Ctrl+R - Reload the tree
case ConsoleKey.R:
noteEditor = new NoteEditor(null);
TreeItem selected_tree_item = noteTree.GetSelectedTreeItem()!;
if (selected_tree_item.Parent == null)
{
noteTree = new NoteTree();
}
else
{
if (Directory.Exists(selected_tree_item.Parent.FilePath))
{
noteTree = new NoteTree(selected_tree_item.Parent.FilePath);
}
else
{
noteTree = new NoteTree();
}
}
noteTree.SetVisible();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+D - Delete the selected tree item
case ConsoleKey.D:
HandleDeleteTreeItem();
break;
// Ctrl+8 - Open the settings file
// Ctrl+Shift+8 - Reload the settings file
case ConsoleKey.D8:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
Settings.ReloadSettings();
noteTree.Set_RequiresUpdate();
noteEditorRequiresUpdate = true;
SetTreeFooterRequiresUpdate();
}
else
{
Settings.OpenSettingsFile();
}
break;
// Ctrl+1 - Toggle the noteTree widget
case ConsoleKey.D1:
if (noteEditor.GetNotePath() == null) break;
noteTree.ToggleVisibility();
SetTreeFooterRequiresUpdate();
Set_NoteEditorRequiresUpdate();
break;
// Ctrl+H - Toggle the help panel
case ConsoleKey.H:
noteEditor.ToggleHelpPanel();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
}
}
// For functionality with regular keypresses
else
{
TreeItem? selected_tree_item = noteTree.GetSelectedTreeItem();
switch (keyInfo.Key)
{
// Tab - Toggle focus to the editor
case ConsoleKey.Tab:
if (noteEditor.GetNotePath() == null) break;
if (noteEditor.IsTypingDisabled()) break;
editorFocused = true;
AnsiConsole.Cursor.Show();
noteTree.Set_RequiresUpdate();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Up Arrow - Move selection up
case ConsoleKey.UpArrow:
case ConsoleKey.PageUp:
noteTree.MoveSelectionUp();
break;
// Down Arrow - Move selection down
case ConsoleKey.DownArrow:
case ConsoleKey.PageDown:
noteTree.MoveSelectionDown();
break;
// Home - Move selection to top
case ConsoleKey.Home:
noteTree.MoveSelectionToTop();
break;
// End - Move selection to bottom
case ConsoleKey.End:
noteTree.MoveSelectionToBottom();
break;
// Escape | B | Tab - Go to parent directory (go back)
case ConsoleKey.Escape:
case ConsoleKey.B:
noteTree.GoToParentIfPossible();
break;
// Enter | Space - Open the note in the editor.
// If space is pressed, keep the tree focused. If enter is pressed, focus the editor.
case ConsoleKey.Enter:
case ConsoleKey.Spacebar:
if (selected_tree_item == null) break;
if (selected_tree_item.IsDir)
{
noteTree.NavigateToTreeItemDirectory(selected_tree_item);
}
else
{
if (noteEditor.HasUnsavedChanges())
{
AskToSaveUnsavedChanges("[yellow]Opening note[/]... but first:");
}
try
{
noteEditor = new NoteEditor(selected_tree_item.FilePath);
}
catch (FileNotFoundException)
{
HandleFileNotFoundException(selected_tree_item.FilePath);
break;
}
// Keep the tree focus if the space key was pressed.
// Focus the editor if enter was pressed.
if (keyInfo.Key == ConsoleKey.Enter)
{
editorFocused = true;
AnsiConsole.Cursor.Show();
}
if (noteEditor.IsTypingDisabled())
{
if (noteTree.IsVisible()) editorFocused = false;
AnsiConsole.Cursor.Hide();
}
SetTreeFooterRequiresUpdate();
Set_NoteEditorRequiresUpdate();
noteTree.Set_RequiresUpdate();
}
Set_NoteEditorRequiresUpdate();
break;
// F2 - Rename the selected item
case ConsoleKey.F2:
RenameSelectedTreeItem();
noteTree.Set_RequiresUpdate();
Set_NoteEditorRequiresUpdate();
break;
// N - Create new file (Ctrl+N also does this)
case ConsoleKey.N:
HandleCreateNewFile();
break;
// M - Create new folder (Ctrl+M also does this)
case ConsoleKey.M:
HandleCreateNewFolder();
break;
// Delete | Backspace - Delete the selected tree item (Ctrl+D also does this)
case ConsoleKey.Delete:
case ConsoleKey.Backspace:
HandleDeleteTreeItem();
break;
}
}
}
private static void HandleEditorInput(ConsoleKeyInfo keyInfo)
{
if (keyInfo.Key == ConsoleKey.V && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Alt))
{
HandlePaste();
return;
}
if (keyInfo.Key == ConsoleKey.J && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))
{
// This function will handle the closing of the window, too.
OpenAIWindow();
treeFooterRequiresUpdate = true;
SetTreeFooterRequiresUpdate();
Set_NoteEditorRequiresUpdate();
return;
}
// For shortcuts with the control key
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))
{
switch (keyInfo.Key)
{
// Ctrl+Q - Quit \ close application
case ConsoleKey.Q:
ExitApplication();
break;
// Ctrl+L - Insert dashed line
case ConsoleKey.L:
noteEditor.InsertDashedLine();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+S - Save note
case ConsoleKey.S:
HandleSave();
break;
// Ctrl+R - Reload note
case ConsoleKey.R:
// Ask if they want a reload while there are unsaved changes
if (noteEditor.HasUnsavedChanges())
{
AskToSaveUnsavedChanges("[yellow]Reloading note...[/] but first:");
}
// Can't reload if there's no note open
string? path = noteEditor.GetNotePath();
if (path == null) break;
// Reload the note
try
{
noteEditor = new NoteEditor(path);
}
catch (FileNotFoundException)
{
HandleFileNotFoundException(path);
break;
}
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+D - Delete line
case ConsoleKey.D:
noteEditor.DeleteLine();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+W - Close currently displayed note
case ConsoleKey.W:
if (noteEditor.HasUnsavedChanges())
{
AskToSaveUnsavedChanges("[yellow]Closing note... [/]but first:");
}
noteEditor = new NoteEditor(null);
Set_NoteEditorRequiresUpdate();
// Move focus to the tree
if (noteTree.IsVisible()) editorFocused = false;
noteTree.Set_RequiresUpdate();
noteTree.SetVisible();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+Z - Undo
case ConsoleKey.Z:
noteEditor.Undo();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+Y - Redo
case ConsoleKey.Y:
noteEditor.Redo();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+End - Navigate to end of file
case ConsoleKey.End:
noteEditor.MoveCursorToEndOfEditor();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+Home | Navigate to start of file
case ConsoleKey.Home:
noteEditor.MoveCursorToStartOfEditor();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+RightArrow - Navigate one word to the right
// Ctrl+Shift+RightArrow - Highlight one word to the right
case ConsoleKey.RightArrow:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
noteEditor.HighlightNextWord();
}
else
{
noteEditor.NavigateToNextWord();
}
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+LeftArrow - Navigate one word to the left
// Ctrl+Shift+LeftArrow - Highlight one word to the left
case ConsoleKey.LeftArrow:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
noteEditor.HighlightPreviousWord();
}
else
{
noteEditor.NavigateToPreviousWord();
}
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+A - Select all
case ConsoleKey.A:
noteEditor.HighlightWholeNote();
Set_NoteEditorRequiresUpdate();
break;
// Ctrl+K - Toggle insert mode
case ConsoleKey.K:
noteEditor.ToggleInsertMode();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+Backspace - Delete word with backspace
case ConsoleKey.Backspace:
noteEditor.DeleteWordWithBackspace();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+Delete - delete word with delete key
case ConsoleKey.Delete:
noteEditor.DeleteWordWithDeleteKey();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+N - Create new file
case ConsoleKey.N:
HandleCreateNewFile();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+M - Create new folder
case ConsoleKey.M:
HandleCreateNewFolder();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+O - Open the currently selected dir in the file explorer
case ConsoleKey.O:
string file_path = noteTree.GetSelectedTreeItem()?.Parent == null ? NOTES_DIR_PATH : noteTree.GetSelectedTreeItem()!.Parent!.FilePath;
Process.Start("explorer.exe", file_path);
break;
// Ctrl+8 - Open the settings file
// Ctrl+Shift+8 - Reload the settings file
case ConsoleKey.D8:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
Settings.ReloadSettings();
noteTree.Set_RequiresUpdate();
noteEditorRequiresUpdate = true;
SetTreeFooterRequiresUpdate();
}
else
{
Settings.OpenSettingsFile();
}
break;
// Ctrl+DownArrow - Navigate to the next note (downwards)
case ConsoleKey.DownArrow:
case ConsoleKey.PageDown:
HandleCtrlDownArrow();
break;
// Ctrl+UpArrow - Navigate to previous note (upwards)
case ConsoleKey.UpArrow:
case ConsoleKey.PageUp:
HandleCtrlUpArrow();
break;
// Ctrl+B - Toggle primary color
// Ctrl+Shift+B - Set primary color for only one character
case ConsoleKey.B:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
if (noteEditor.SomeCharsAreHighlighted())
{
if (noteEditor.SelectionIsColor(Settings.PrimaryColor))
{
noteEditor.RemoveColorFromHighlightedChars();
}
else
{
noteEditor.ColorHighlightedChars(Settings.PrimaryColor);
}
Set_NoteEditorRequiresUpdate();
}
else
{
noteEditor.SetPrimaryColorForOneChar();
}
}
else
{
noteEditor.TogglePrimaryColor();
}
SetTreeFooterRequiresUpdate();
break;
// Ctrl+U - Toggle secondary color
// Ctrl+Shift+U - Set secondary color for only one character
case ConsoleKey.U:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
if (noteEditor.SomeCharsAreHighlighted())
{
noteEditor.ColorHighlightedChars(Settings.SecondaryColor);
Set_NoteEditorRequiresUpdate();
}
else
{
noteEditor.SetSecondaryColorForOneChar();
}
}
else
{
noteEditor.ToggleSecondaryColor();
}
SetTreeFooterRequiresUpdate();
break;
// Ctrl+I - Toggle tertiary color
// Ctrl+Shift+I - Set tertiary color for only one character
case ConsoleKey.I:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
if (noteEditor.SomeCharsAreHighlighted())
{
noteEditor.ColorHighlightedChars(Settings.TertiaryColor);
Set_NoteEditorRequiresUpdate();
}
else
{
noteEditor.SetTertiaryColorForOneChar();
}
}
else
{
noteEditor.ToggleTertiaryColor();
}
SetTreeFooterRequiresUpdate();
break;
// Ctrl+1 - Toggle the noteTree widget
case ConsoleKey.D1:
if (noteEditor.GetNotePath() == null) break;
noteTree.ToggleVisibility();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+G - Go to line
case ConsoleKey.G:
int? navigate_to = GetLineToNavigateTo();
if (navigate_to != null)
{
noteEditor.GoToLine((int)navigate_to);
}
Set_NoteEditorRequiresUpdate();
noteTree.Set_RequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+H - Toggle the help panel
case ConsoleKey.H:
noteEditor.ToggleHelpPanel();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Ctrl+Space - Set all the colors Ctrl+(B | U | I ) to off. Color will be white
case ConsoleKey.Spacebar:
noteEditor.SetAllColorsToOff();
SetTreeFooterRequiresUpdate();
break;
}
}
// For functionality with regular keypresses
else
{
switch (keyInfo.Key)
{
// Escape - Unfocus the editor, therefore focusing the tree
// If text is highlighted when escape is pressed, escape will unhighlight the text
case ConsoleKey.Escape:
if (noteEditor.SomeCharsAreHighlighted())
{
noteEditor.UnhighlightAllChars();
}
else
{
if (noteTree.IsVisible()) editorFocused = false;
AnsiConsole.Cursor.Hide();
}
Set_NoteEditorRequiresUpdate();
noteTree.Set_RequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Enter - Add new line at current position
// Shift+Enter - Move to next line without breaking current line. Like a normal enter but without splitting the line up if the cursor is in the middle of it
case ConsoleKey.Enter:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
noteEditor.MoveCursorDown();
}
else
{
noteEditor.InsertLine();
}
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// UpArrow - Navigate the cursor up in the editor
// Shift+UpArrow - Move line up
case ConsoleKey.UpArrow:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
noteEditor.MoveLineUp();
else
noteEditor.MoveCursorUp();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// DownArrow - Navigate the cursor down in the editor
// Shift+DownArrow - Move line down
case ConsoleKey.DownArrow:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
noteEditor.MoveLineDown();
else
noteEditor.MoveCursorDown();
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// LeftArrow - Navigate the cursor left in the editor
// Shift+LeftArrow - Highlight one char to the left in the editor
case ConsoleKey.LeftArrow:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
noteEditor.HighlightPreviousChar();
}
else
{
noteEditor.MoveCursorLeft();
}
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// RightArrow - Navigate the cursor right in the editor
// Shift+RightArrow - Highlight on char to the right in the editor
case ConsoleKey.RightArrow:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
noteEditor.HighlightNextChar();
}
else
{
noteEditor.MoveCursorRight();
}
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// End - Navigate to end of line
// Shift+End - Highlight to end of line
case ConsoleKey.End:
if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
noteEditor.HighlightToEndOfLine();
}
else
{
noteEditor.MoveCursorToEndOfLine();
}
Set_NoteEditorRequiresUpdate();
SetTreeFooterRequiresUpdate();
break;
// Home - Navigate to start of line
// Shift+Home - Highlight to start of line