Skip to content

Commit d3cfd48

Browse files
Exporting as a markdown file via --export for search and list
1 parent 3a26e42 commit d3cfd48

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,5 @@ FodyWeavers.xsd
396396

397397
# JetBrains Rider
398398
*.sln.iml
399+
400+
exported-bookmarks.md

Commands/ListCommand.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
using Spectre.Console;
22
using Spectre.Console.Cli;
3+
using System.ComponentModel;
4+
using System.Text;
35
using TinyCity.BookmarkEngines;
46
using TinyCity.Model;
57

68
namespace TinyCity.Commands
79
{
8-
public class ListCommand : Command<BaseSettings>
10+
public class ListCommandSettings : BaseSettings
11+
{
12+
[CommandOption("-e|--export")]
13+
[Description("Exports the results as 'exported-bookmarks.md' to the same directory as tinycity.")]
14+
[DefaultValue(false)]
15+
public bool Export { get; set; }
16+
}
17+
18+
public class ListCommand : Command<ListCommandSettings>
919
{
1020
private List<BookmarkNode> _combinedBookmarks;
1121

@@ -14,8 +24,9 @@ public ListCommand(BookmarkAggregator bookmarkAggregator)
1424
_combinedBookmarks = bookmarkAggregator.AllBookmarks;
1525
}
1626

17-
public override int Execute(CommandContext context, BaseSettings settings)
27+
public override int Execute(CommandContext context, ListCommandSettings settings)
1828
{
29+
var stringBuilder = new StringBuilder();
1930
AnsiConsole.MarkupLine($"[bold turquoise2]{_combinedBookmarks.Count} unique bookmarks in total.[/]");
2031

2132
foreach (var bookmark in _combinedBookmarks.OrderBy(x => x.Name))
@@ -28,9 +39,17 @@ public override int Execute(CommandContext context, BaseSettings settings)
2839
string link = $"[link={bookmarkUrl}]{bookmarkName}[/]";
2940
string urlHost = new Uri(bookmark.Url).Host;
3041
AnsiConsole.MarkupLine($" • [bold chartreuse1]{link}[/] ({urlHost})");
42+
43+
stringBuilder.AppendLine($"- [{Markup.Escape(bookmark.Name)}]({bookmark.Url}) ({urlHost})");
3144
}
3245
}
3346

47+
if (settings.Export)
48+
{
49+
File.WriteAllText("exported-bookmarks.md", stringBuilder.ToString());
50+
AnsiConsole.MarkupLine($"[bold green]Exported to all bookmarks 'exported-bookmarks.md'[/].");
51+
}
52+
3453
return 0;
3554
}
3655
}

Commands/SearchCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Spectre.Console;
22
using Spectre.Console.Cli;
3+
using System.Collections.Generic;
34
using System.ComponentModel;
45
using System.Diagnostics;
6+
using System.Text;
57
using TinyCity.BookmarkEngines;
68
using 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

Comments
 (0)