|
8 | 8 |
|
9 | 9 | Console.OutputEncoding = System.Text.Encoding.UTF8;
|
10 | 10 |
|
| 11 | +Console.CancelKeyPress += (sender, args) => |
| 12 | +{ |
| 13 | + args.Cancel = true; |
| 14 | + ConsoleFeatures.ClearState(); |
| 15 | + Environment.Exit(0); |
| 16 | +}; |
| 17 | + |
11 | 18 | const string searchPattern = "conversations.json";
|
12 | 19 |
|
13 | 20 | var sourceDirectoryOption = new Option<DirectoryInfo[]>("--source", "-s")
|
@@ -66,64 +73,82 @@ You can also specify multiple source directories (eg, -s dir1 -s dir2), and they
|
66 | 73 |
|
67 | 74 | rootCommand.SetAction(parseResult =>
|
68 | 75 | {
|
69 |
| - foreach (ParseError parseError in parseResult.Errors) |
| 76 | + try |
70 | 77 | {
|
71 |
| - Console.Error.WriteLine(parseError.Message); |
72 |
| - } |
| 78 | + foreach (ParseError parseError in parseResult.Errors) |
| 79 | + { |
| 80 | + Console.Error.WriteLine(parseError.Message); |
| 81 | + } |
73 | 82 |
|
74 |
| - var sourceDirectoryInfos = parseResult.GetRequiredValue(sourceDirectoryOption); |
75 |
| - var fileSystem = new FileSystem(); |
76 |
| - var destination = fileSystem.DirectoryInfo.Wrap(parseResult.GetRequiredValue(destinationDirectoryOption)); |
77 |
| - var sources = sourceDirectoryInfos.Select(p => fileSystem.DirectoryInfo.Wrap(p)); |
| 83 | + ConsoleFeatures.StartIndeterminate(); |
78 | 84 |
|
79 |
| - var conversationFiles = sources.Select(p => p.GetFiles(searchPattern, SearchOption.AllDirectories)).SelectMany(s => s).ToList(); |
| 85 | + var sourceDirectoryInfos = parseResult.GetRequiredValue(sourceDirectoryOption); |
| 86 | + var fileSystem = new FileSystem(); |
| 87 | + var destination = fileSystem.DirectoryInfo.Wrap(parseResult.GetRequiredValue(destinationDirectoryOption)); |
| 88 | + var sources = sourceDirectoryInfos.Select(p => fileSystem.DirectoryInfo.Wrap(p)); |
80 | 89 |
|
81 |
| - var conversationsFactory = new ConversationsParser(fileSystem); |
82 |
| - var exporters = new List<IExporter>(); |
83 |
| - if (parseResult.GetRequiredValue(jsonOption)) |
84 |
| - { |
85 |
| - exporters.Add(new JsonExporter(fileSystem, destination)); |
86 |
| - } |
87 |
| - if (parseResult.GetRequiredValue(markdownOption)) |
88 |
| - { |
89 |
| - exporters.Add(new MarkdownExporter(fileSystem, destination)); |
90 |
| - } |
91 |
| - var exporter = new Exporter(fileSystem, exporters); |
| 90 | + var conversationFiles = sources.Select(p => p.GetFiles(searchPattern, SearchOption.AllDirectories)).SelectMany(s => s).ToList(); |
92 | 91 |
|
93 |
| - Conversations GetConversations(IFileInfo p) |
94 |
| - { |
95 |
| - try |
| 92 | + var conversationsFactory = new ConversationsParser(fileSystem); |
| 93 | + var exporters = new List<IExporter>(); |
| 94 | + if (parseResult.GetRequiredValue(jsonOption)) |
96 | 95 | {
|
97 |
| - Console.WriteLine($"Loading conversation " + p.FullName); |
98 |
| - return conversationsFactory.GetConversations(p); |
| 96 | + exporters.Add(new JsonExporter(fileSystem, destination)); |
99 | 97 | }
|
100 |
| - catch (Exception ex) |
| 98 | + if (parseResult.GetRequiredValue(markdownOption)) |
101 | 99 | {
|
102 |
| - Console.Error.WriteLine($"Error parsing file: {p.FullName} {ex.Message}"); |
103 |
| - return null; |
| 100 | + exporters.Add(new MarkdownExporter(fileSystem, destination)); |
104 | 101 | }
|
105 |
| - } |
| 102 | + var exporter = new Exporter(fileSystem, exporters); |
106 | 103 |
|
107 |
| - var directoryConversationsMap = conversationFiles |
108 |
| - .Select(file => new |
| 104 | + Conversations GetConversations(IFileInfo p) |
109 | 105 | {
|
110 |
| - file.Directory, |
111 |
| - Conversations = GetConversations(file) |
112 |
| - }) |
113 |
| - .Where(x => x.Conversations != null) |
114 |
| - .OrderBy(x => x.Conversations.GetUpdateTime()) |
115 |
| - .ToList(); |
116 |
| - |
117 |
| - var groupedByConversationId = directoryConversationsMap |
118 |
| - .SelectMany(entry => entry.Conversations, (entry, Conversation) => (entry.Directory, Conversation)) |
119 |
| - .GroupBy(x => x.Conversation.conversation_id) |
120 |
| - .OrderBy(p => p.Key).ToList(); |
121 |
| - |
122 |
| - foreach (var group in groupedByConversationId) |
| 106 | + try |
| 107 | + { |
| 108 | + Console.WriteLine($"Loading conversation " + p.FullName); |
| 109 | + return conversationsFactory.GetConversations(p); |
| 110 | + } |
| 111 | + catch (Exception ex) |
| 112 | + { |
| 113 | + Console.Error.WriteLine($"Error parsing file: {p.FullName} {ex.Message}"); |
| 114 | + return null; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + var directoryConversationsMap = conversationFiles |
| 119 | + .Select(file => new |
| 120 | + { |
| 121 | + file.Directory, |
| 122 | + Conversations = GetConversations(file) |
| 123 | + }) |
| 124 | + .Where(x => x.Conversations != null) |
| 125 | + .OrderBy(x => x.Conversations.GetUpdateTime()) |
| 126 | + .ToList(); |
| 127 | + |
| 128 | + var groupedByConversationId = directoryConversationsMap |
| 129 | + .SelectMany(entry => entry.Conversations, (entry, Conversation) => (entry.Directory, Conversation)) |
| 130 | + .GroupBy(x => x.Conversation.conversation_id) |
| 131 | + .OrderBy(p => p.Key).ToList(); |
| 132 | + |
| 133 | + var count = groupedByConversationId.Count; |
| 134 | + var position = 0; |
| 135 | + foreach (var group in groupedByConversationId) |
| 136 | + { |
| 137 | + var percent = (int)(position++ * 100.0 / count); |
| 138 | + ConsoleFeatures.SetProgress(percent); |
| 139 | + exporter.Process(group, destination); |
| 140 | + } |
| 141 | + } |
| 142 | + catch (Exception ex) |
123 | 143 | {
|
124 |
| - exporter.Process(group, destination); |
| 144 | + Console.Error.WriteLine(ex.Message); |
| 145 | + } |
| 146 | + finally |
| 147 | + { |
| 148 | + ConsoleFeatures.ClearState(); |
125 | 149 | }
|
126 | 150 | return 0;
|
| 151 | + |
127 | 152 | });
|
128 | 153 |
|
129 | 154 | var parseResult = rootCommand.Parse(args);
|
|
0 commit comments