1
- using Microsoft . Win32 ;
1
+ using CliWrap . Buffered ;
2
+ using CliWrap ;
3
+ using Microsoft . Win32 ;
2
4
using System ;
3
5
using System . Collections . Generic ;
4
6
using System . Diagnostics ;
15
17
using Text_Grab . Properties ;
16
18
using Text_Grab . Services ;
17
19
using Text_Grab . Utilities ;
20
+ using System . Text . RegularExpressions ;
18
21
19
22
namespace Text_Grab . Views ;
20
23
@@ -56,6 +59,22 @@ public QuickSimpleLookup()
56
59
57
60
private static LookupItem ParseStringToLookupItem ( char splitChar , string row )
58
61
{
62
+ LookupItemKind kind = LookupItemKind . Simple ;
63
+ if ( row . StartsWith ( '>' ) )
64
+ {
65
+ kind = LookupItemKind . Command ;
66
+ }
67
+ else if ( row . StartsWith ( "http" ) )
68
+ kind = LookupItemKind . Link ;
69
+ else if ( row . StartsWith ( "🔗" ) )
70
+ {
71
+ kind = LookupItemKind . Link ;
72
+ }
73
+ else if ( row . StartsWith ( '⚡' ) )
74
+ {
75
+ kind = LookupItemKind . Dynamic ;
76
+ }
77
+
59
78
List < string > cells = [ .. row . Split ( splitChar ) ] ;
60
79
LookupItem newRow = new ( ) ;
61
80
if ( cells . FirstOrDefault ( ) is string firstCell )
@@ -64,6 +83,8 @@ private static LookupItem ParseStringToLookupItem(char splitChar, string row)
64
83
newRow . LongValue = "" ;
65
84
if ( cells . Count > 1 && cells [ 1 ] is not null )
66
85
newRow . LongValue = string . Join ( " " , cells . Skip ( 1 ) . ToArray ( ) ) ;
86
+
87
+ newRow . Kind = kind ;
67
88
return newRow ;
68
89
}
69
90
@@ -477,6 +498,11 @@ private async void PutValueIntoClipboard(KeyboardModifiersDown? keysDown = null)
477
498
Process . Start ( new ProcessStartInfo ( lItem . LongValue ) { UseShellExecute = true } ) ;
478
499
openedHistoryItemOrLink = true ;
479
500
break ;
501
+ case LookupItemKind . Command :
502
+ openedHistoryItemOrLink = await RunCli ( lItem . LongValue ) ;
503
+ break ;
504
+ case LookupItemKind . Dynamic :
505
+ break ;
480
506
default :
481
507
stringBuilder . AppendLine ( lItem . LongValue ) ;
482
508
break ;
@@ -534,6 +560,48 @@ private async void PutValueIntoClipboard(KeyboardModifiersDown? keysDown = null)
534
560
WindowUtilities . ShouldShutDown ( ) ;
535
561
}
536
562
563
+ /// <summary>
564
+ ///
565
+ /// </summary>
566
+ /// <param name="longValue"></param>
567
+ /// <returns>returns if output is empty or whitespace</returns>
568
+ private async Task < bool > RunCli ( string longValue )
569
+ {
570
+ string [ ] commands = longValue . Split ( ' ' , StringSplitOptions . RemoveEmptyEntries ) ;
571
+
572
+ if ( commands . Length == 0 )
573
+ return true ;
574
+
575
+ BufferedCommandResult result = await Cli . Wrap ( "powershell" )
576
+ . WithArguments ( commands )
577
+ . WithValidation ( CommandResultValidation . None )
578
+ . ExecuteBufferedAsync ( Encoding . UTF8 ) ;
579
+
580
+ string outputText = result . StandardOutput . Trim ( ) ;
581
+ string errorText = result . StandardError . Trim ( ) ;
582
+
583
+ string shortOutput = "Output" ;
584
+ if ( string . IsNullOrWhiteSpace ( outputText ) )
585
+ {
586
+ if ( string . IsNullOrWhiteSpace ( errorText ) )
587
+ return true ;
588
+
589
+ shortOutput = "ERROR!" ;
590
+ outputText = errorText ;
591
+ }
592
+
593
+ LookupItem newItem = new ( shortOutput , outputText )
594
+ {
595
+ Kind = LookupItemKind . Simple
596
+ } ;
597
+
598
+ MainDataGrid . ItemsSource = null ;
599
+ List < LookupItem > newItems = [ newItem ] ;
600
+ MainDataGrid . ItemsSource = newItems ;
601
+
602
+ return false ;
603
+ }
604
+
537
605
private async void QuickSimpleLookup_PreviewKeyDown ( object sender , KeyEventArgs e )
538
606
{
539
607
switch ( e . Key )
@@ -606,6 +674,13 @@ private async void QuickSimpleLookup_PreviewKeyDown(object sender, KeyEventArgs
606
674
e . Handled = true ;
607
675
}
608
676
break ;
677
+ case Key . R :
678
+ if ( KeyboardExtensions . IsCtrlDown ( ) )
679
+ {
680
+ RegExToggleButton . IsChecked = ! RegExToggleButton . IsChecked ;
681
+ e . Handled = true ;
682
+ }
683
+ break ;
609
684
case Key . End :
610
685
GoToEndOfMainDataGrid ( ) ;
611
686
break ;
@@ -690,12 +765,17 @@ private async void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
690
765
if ( sender is not TextBox searchingBox || ! IsLoaded )
691
766
return ;
692
767
693
- if ( string . IsNullOrEmpty ( searchingBox . Text ) )
768
+ await ReSearch ( searchingBox . Text ) ;
769
+ }
770
+
771
+ private async Task ReSearch ( string searchString )
772
+ {
773
+ if ( string . IsNullOrEmpty ( searchString ) )
694
774
SearchLabel . Visibility = Visibility . Visible ;
695
775
else
696
776
SearchLabel . Visibility = Visibility . Collapsed ;
697
777
698
- if ( searchingBox . Text . Contains ( '\t ' ) )
778
+ if ( searchString . Contains ( '\t ' ) )
699
779
{
700
780
// a tab has been entered and this will be a new entry
701
781
AddItemBtn . Visibility = Visibility . Visible ;
@@ -707,7 +787,7 @@ private async void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
707
787
708
788
MainDataGrid . ItemsSource = null ;
709
789
710
- if ( string . IsNullOrEmpty ( searchingBox . Text ) )
790
+ if ( string . IsNullOrEmpty ( searchString ) )
711
791
{
712
792
MainDataGrid . ItemsSource = ItemsDictionary ;
713
793
MainDataGrid . CanUserAddRows = true ;
@@ -719,9 +799,13 @@ private async void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
719
799
if ( row is null )
720
800
{
721
801
MainDataGrid . UpdateLayout ( ) ;
722
- MainDataGrid . ScrollIntoView ( MainDataGrid . Items [ lastSelectionInt ] ) ;
723
- await Task . Delay ( lastSelectionInt > maxMsDelay ? maxMsDelay : lastSelectionInt ) ;
724
- row = ( DataGridRow ) MainDataGrid . ItemContainerGenerator . ContainerFromIndex ( lastSelectionInt ) ;
802
+
803
+ if ( lastSelectionInt > - 1 )
804
+ {
805
+ MainDataGrid . ScrollIntoView ( MainDataGrid . Items [ lastSelectionInt ] ) ;
806
+ await Task . Delay ( lastSelectionInt > maxMsDelay ? maxMsDelay : lastSelectionInt ) ;
807
+ row = ( DataGridRow ) MainDataGrid . ItemContainerGenerator . ContainerFromIndex ( lastSelectionInt ) ;
808
+ }
725
809
}
726
810
727
811
if ( row is not null )
@@ -740,7 +824,55 @@ private async void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
740
824
else
741
825
MainDataGrid . CanUserAddRows = false ;
742
826
743
- List < string > searchArray = [ .. SearchBox . Text . ToLower ( ) . Split ( ) ] ;
827
+ if ( RegExToggleButton . IsChecked is true )
828
+ RegexSearch ( searchString ) ;
829
+ else
830
+ StandardSearch ( searchString ) ;
831
+
832
+ UpdateRowCount ( ) ;
833
+ }
834
+
835
+ private void RegexSearch ( string searchString )
836
+ {
837
+ Regex searchRegex ;
838
+
839
+ try
840
+ {
841
+ searchRegex = new Regex ( searchString , RegexOptions . IgnoreCase ) ;
842
+ }
843
+ catch
844
+ {
845
+ RegExToggleButton . BorderBrush = Brushes . Red ;
846
+ RegExToggleButton . ToolTip = "Invalid Regular Expression" ;
847
+ return ;
848
+ }
849
+
850
+ RegExToggleButton . BorderBrush = Brushes . Transparent ;
851
+ RegExToggleButton . ToolTip = "Searh using Regular Expression Syntax" ;
852
+
853
+ List < LookupItem > filteredList = [ ] ;
854
+
855
+ foreach ( LookupItem lItem in ItemsDictionary )
856
+ {
857
+ string lItemAsString = lItem . ToString ( ) . ToLower ( ) ;
858
+
859
+ if ( searchRegex . IsMatch ( lItemAsString )
860
+ || lItem . FirstLettersString . Contains ( SearchBox . Text . ToLower ( ) , StringComparison . CurrentCultureIgnoreCase ) )
861
+ filteredList . Add ( lItem ) ;
862
+ }
863
+
864
+ MainDataGrid . ItemsSource = filteredList ;
865
+
866
+ if ( MainDataGrid . Items . Count > 0 )
867
+ MainDataGrid . SelectedIndex = 0 ;
868
+ }
869
+
870
+ private void StandardSearch ( string searchString )
871
+ {
872
+ RegExToggleButton . BorderBrush = Brushes . Transparent ;
873
+ RegExToggleButton . ToolTip = "Searh using Regular Expression Syntax" ;
874
+
875
+ List < string > searchArray = [ .. searchString . ToLower ( ) . Split ( ) ] ;
744
876
searchArray . Sort ( ) ;
745
877
746
878
List < LookupItem > filteredList = [ ] ;
@@ -756,16 +888,15 @@ private async void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
756
888
matchAllSearchWords = false ;
757
889
}
758
890
759
- if ( matchAllSearchWords )
891
+ if ( matchAllSearchWords
892
+ || lItem . FirstLettersString . Contains ( SearchBox . Text . ToLower ( ) , StringComparison . CurrentCultureIgnoreCase ) )
760
893
filteredList . Add ( lItem ) ;
761
894
}
762
895
763
896
MainDataGrid . ItemsSource = filteredList ;
764
897
765
898
if ( MainDataGrid . Items . Count > 0 )
766
899
MainDataGrid . SelectedIndex = 0 ;
767
-
768
- UpdateRowCount ( ) ;
769
900
}
770
901
771
902
private void TextGrabSettingsMenuItem_Click ( object sender , RoutedEventArgs e )
@@ -915,5 +1046,11 @@ private void OpenInETWMenuItem_Click(object sender, RoutedEventArgs e)
915
1046
break ;
916
1047
}
917
1048
}
1049
+
1050
+ private async void RegExToggleButton_Checked ( object sender , RoutedEventArgs e )
1051
+ {
1052
+ await ReSearch ( SearchBox . Text ) ;
1053
+ }
1054
+
918
1055
#endregion Methods
919
1056
}
0 commit comments