11using Spectre . Console ;
22using Spectre . Console . Cli ;
3+ using System . Collections . Generic ;
34using System . ComponentModel ;
45using System . Diagnostics ;
6+ using System . Text ;
57using TinyCity . BookmarkEngines ;
68using TinyCity . Model ;
79
@@ -22,6 +24,11 @@ public class SearchCommandSettings : BaseSettings
2224 [ CommandArgument ( 0 , "<query>" ) ]
2325 [ Description ( "The search term to look for in bookmarks. Enclose your search inside quotes, e.g. \" my search words\" " ) ]
2426 public required string Query { get ; set ; }
27+
28+ [ CommandOption ( "-e|--export" ) ]
29+ [ Description ( "Exports the results as 'exported-bookmarks.md' to the same directory as tinycity." ) ]
30+ [ DefaultValue ( false ) ]
31+ public bool Export { get ; set ; }
2532 }
2633
2734 public class SearchCommand : Command < SearchCommandSettings >
@@ -35,6 +42,7 @@ public SearchCommand(BookmarkAggregator bookmarkAggregator)
3542
3643 public override int Execute ( CommandContext context , SearchCommandSettings settings )
3744 {
45+ var stringBuilder = new StringBuilder ( ) ;
3846 var filteredBookmarks = Search ( settings . Query , settings . SearchUrls ) ;
3947 int count = filteredBookmarks . Count ;
4048 if ( count == 0 )
@@ -54,6 +62,7 @@ public override int Execute(CommandContext context, SearchCommandSettings settin
5462 string link = $ "[link={ bookmarkUrl } ]{ bookmarkName } [/]";
5563 string urlHost = new Uri ( bookmark . Url ) . Host ;
5664 AnsiConsole . MarkupLine ( $ " • [bold chartreuse1]{ link } [/] ({ urlHost } )") ;
65+ stringBuilder . AppendLine ( $ "- [{ Markup . Escape ( bookmark . Name ) } ]({ bookmark . Url } ) ({ urlHost } )") ;
5766 }
5867 }
5968
@@ -70,6 +79,11 @@ public override int Execute(CommandContext context, SearchCommandSettings settin
7079
7180 Process . Start ( startInfo ) ;
7281 }
82+ else if ( settings . Export )
83+ {
84+ File . WriteAllText ( "exported-bookmarks.md" , stringBuilder . ToString ( ) ) ;
85+ AnsiConsole . MarkupLine ( $ "[bold green]Exported search results to 'exported-bookmarks.md'[/].") ;
86+ }
7387
7488 return 0 ;
7589 }
@@ -82,12 +96,14 @@ private List<BookmarkNode> Search(string searchTerm, bool searchUrls)
8296 {
8397 return _combinedBookmarks
8498 . Where ( b => b . Name . ToLower ( ) . Contains ( searchTerm ) || ( b . Url != null && b . Url . ToLower ( ) . Contains ( searchTerm ) ) )
99+ . OrderBy ( x => x . Name )
85100 . ToList ( ) ;
86101 }
87102 else
88103 {
89104 return _combinedBookmarks
90105 . Where ( b => b . Name . ToLower ( ) . Contains ( searchTerm ) )
106+ . OrderBy ( x => x . Name )
91107 . ToList ( ) ;
92108 }
93109 }
0 commit comments