-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
290 lines (269 loc) · 10.7 KB
/
Program.cs
File metadata and controls
290 lines (269 loc) · 10.7 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
using System.Runtime.InteropServices;
using ExcelConsole;
public class Program
{
[STAThread]
public static void Main(string[] args)
{
if (args.Contains("--help") || args.Contains("-h"))
{
PrintHelp();
return;
}
if (args.Contains("--version") || args.Contains("-v"))
{
PrintVersion();
return;
}
if (args.Contains("--list-extensions"))
{
PrintInstalledExtensions();
return;
}
string? csvPath = args.FirstOrDefault(a => !a.StartsWith("--"));
int exportIdx = Array.IndexOf(args, "--export-md");
if (exportIdx >= 0)
{
if (csvPath == null || exportIdx + 1 >= args.Length)
{
Console.Error.WriteLine("Usage: ExcelConsole <input.csv> --export-md <output.md>");
Environment.Exit(2);
return;
}
string outPath = args[exportIdx + 1];
if (!File.Exists(csvPath))
{
Console.Error.WriteLine($"Input CSV not found: {csvPath}");
Environment.Exit(1);
return;
}
// Probe CSV for dimensions so the headless GridManager is big enough.
var lines = File.ReadAllLines(csvPath);
int rows = Math.Max(1, lines.Length);
int cols = 1;
foreach (var line in lines)
{
int n = 1;
bool inQuotes = false;
foreach (char ch in line)
{
if (ch == '"') inQuotes = !inQuotes;
else if (ch == ',' && !inQuotes) n++;
}
if (n > cols) cols = n;
}
// Constructor derives ColumnCount from (availableWidth - 4) / columnWidth (default 20).
var grid = new GridManager(availableWidth: cols * 20 + 4, availableHeight: rows);
grid.LoadFromCsv(csvPath);
if (outPath == "-")
{
grid.WriteMarkdownTo(Console.Out);
}
else
{
grid.SaveToMarkdown(outPath);
Console.WriteLine($"Wrote markdown: {outPath}");
}
return;
}
int htmlIdx = Array.IndexOf(args, "--export-html");
if (htmlIdx >= 0)
{
if (csvPath == null || htmlIdx + 1 >= args.Length)
{
Console.Error.WriteLine("Usage: ExcelConsole <input.csv> --export-html <output.html>");
Environment.Exit(2);
return;
}
string htmlOut = args[htmlIdx + 1];
if (!File.Exists(csvPath))
{
Console.Error.WriteLine($"Input CSV not found: {csvPath}");
Environment.Exit(1);
return;
}
var lines = File.ReadAllLines(csvPath);
int rows = Math.Max(1, lines.Length);
int cols = 1;
foreach (var line in lines)
{
int n = 1;
bool inQuotes = false;
foreach (char ch in line)
{
if (ch == '"') inQuotes = !inQuotes;
else if (ch == ',' && !inQuotes) n++;
}
if (n > cols) cols = n;
}
var grid = new GridManager(availableWidth: cols * 20 + 4, availableHeight: rows);
grid.LoadFromCsv(csvPath);
if (htmlOut == "-")
{
grid.WriteHtmlTo(Console.Out);
}
else
{
grid.SaveToHtml(htmlOut);
Console.WriteLine($"Wrote HTML: {htmlOut}");
}
return;
}
int jsonIdx = Array.IndexOf(args, "--export-json");
if (jsonIdx >= 0)
{
if (csvPath == null || jsonIdx + 1 >= args.Length)
{
Console.Error.WriteLine("Usage: ExcelConsole <input.csv> --export-json <output.json>");
Environment.Exit(2);
return;
}
string jsonOut = args[jsonIdx + 1];
if (!File.Exists(csvPath))
{
Console.Error.WriteLine($"Input CSV not found: {csvPath}");
Environment.Exit(1);
return;
}
var lines = File.ReadAllLines(csvPath);
int rows = Math.Max(1, lines.Length);
int cols = 1;
foreach (var line in lines)
{
int n = 1;
bool inQuotes = false;
foreach (char ch in line)
{
if (ch == '"') inQuotes = !inQuotes;
else if (ch == ',' && !inQuotes) n++;
}
if (n > cols) cols = n;
}
var grid = new GridManager(availableWidth: cols * 20 + 4, availableHeight: rows);
grid.LoadFromCsv(csvPath);
if (jsonOut == "-")
{
grid.WriteJsonTo(Console.Out);
}
else
{
grid.SaveToJson(jsonOut);
Console.WriteLine($"Wrote JSON: {jsonOut}");
}
return;
}
#if PLATFORM_WINDOWS
HideConsoleWindow();
using var host = new ExcelConsole.Platform.Windows.WindowsDesktopHost();
host.Run(csvPath);
#elif PLATFORM_LINUX
string sessionType = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE") ?? "";
if (sessionType.Equals("wayland", StringComparison.OrdinalIgnoreCase))
{
Console.Error.WriteLine("Warning: QuickSheet requires an X11 session.");
Console.Error.WriteLine("You are running Wayland. Select 'GNOME on Xorg' at the login screen,");
Console.Error.WriteLine("or set DISPLAY and try with XWayland (may not work as true desktop layer).");
Console.Error.WriteLine();
Console.Error.WriteLine("Attempting via XWayland anyway...");
}
using var host = new ExcelConsole.Platform.Linux.LinuxDesktopHost();
host.Run(csvPath);
#else
Console.Error.WriteLine("QuickSheet is only supported on Windows and Linux (X11).");
#endif
}
private const string Version = "0.36.0";
private static void PrintInstalledExtensions()
{
string root = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".quicksheet", "extensions");
if (!Directory.Exists(root))
{
Console.WriteLine("(no extensions installed)");
Console.WriteLine($"Install one with `ext: github:user/repo` from inside QuickSheet.");
Console.WriteLine($"Extensions live under {root}");
return;
}
var dirs = Directory.GetDirectories(root).OrderBy(d => d).ToList();
if (dirs.Count == 0)
{
Console.WriteLine("(no extensions installed)");
return;
}
Console.WriteLine($"Installed extensions ({root}):");
foreach (var dir in dirs)
{
string manifestPath = Path.Combine(dir, "quicksheet-extension.json");
string name = Path.GetFileName(dir);
if (File.Exists(manifestPath))
{
try
{
using var doc = System.Text.Json.JsonDocument.Parse(File.ReadAllText(manifestPath));
string prefix = doc.RootElement.TryGetProperty("prefix", out var p) ? p.GetString() ?? "?" : "?";
string version = doc.RootElement.TryGetProperty("version", out var v) ? v.GetString() ?? "?" : "?";
Console.WriteLine($" {prefix,-8} {name} v{version}");
}
catch
{
Console.WriteLine($" ? {name} (bad manifest)");
}
}
else
{
Console.WriteLine($" ? {name} (no manifest)");
}
}
}
private static void PrintVersion()
{
string platform =
#if PLATFORM_WINDOWS
"windows";
#elif PLATFORM_LINUX
"linux";
#else
"unknown";
#endif
Console.WriteLine($"QuickSheet {Version} ({platform}, .NET {Environment.Version})");
}
private static void PrintHelp()
{
Console.WriteLine("QuickSheet — interactive spreadsheet as your desktop wallpaper");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" ExcelConsole [<file.csv>] Run as desktop wallpaper");
Console.WriteLine(" ExcelConsole <file.csv> --export-md <out.md> Headless: CSV → Markdown table (use - for stdout)");
Console.WriteLine(" ExcelConsole <file.csv> --export-html <out.html> Headless: CSV → styled HTML table (use - for stdout)");
Console.WriteLine(" ExcelConsole <file.csv> --export-json <out.json> Headless: CSV → JSON array of objects (use - for stdout)");
Console.WriteLine(" ExcelConsole --help Show this help");
Console.WriteLine(" ExcelConsole --version Show version");
Console.WriteLine(" ExcelConsole --list-extensions List installed extensions");
Console.WriteLine();
Console.WriteLine("Cell prefixes:");
Console.WriteLine(" r: <cmd> Runnable command. Press Enter to launch.");
Console.WriteLine(" i: <cmd> Inline subprocess. Output streams back into the cell.");
Console.WriteLine(" s: 1,2,3,... Sparkline (unicode block bars). Also accepts range: s: A1::A10");
Console.WriteLine(" c:<color>: <text> Cell color. Highlights background (red/green/blue/yellow/cyan/magenta/white/gray).");
Console.WriteLine(" L: <cellRef>,<N>m Loop the target cell every N minutes.");
Console.WriteLine(" ext: github:u/r Install an extension repo (registers a new prefix).");
Console.WriteLine(" http(s)://... Hyperlink. Highlighted, opens in browser on Enter.");
Console.WriteLine();
Console.WriteLine("Range references work inside text: {A1::C10}");
Console.WriteLine("Tour: docs/tour.md · Issues: github.com/cemheren/QuickSheet/issues");
}
#if PLATFORM_WINDOWS
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
private static void HideConsoleWindow()
{
IntPtr console = GetConsoleWindow();
if (console != IntPtr.Zero)
ShowWindow(console, 0); // SW_HIDE
}
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
#endif
}