-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cs
More file actions
208 lines (187 loc) · 6.36 KB
/
Copy pathcore.cs
File metadata and controls
208 lines (187 loc) · 6.36 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
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
using Spectre.Console;
public static class Core
{
public class DeviceInfo
{
public string Path;
public string Model;
public long SizeBytes;
public bool IsSystem;
}
public static string[] SplitSpaces(string s)
{
return s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
public static string[] SplitCsvLine(string line)
{
var list = new List<string>();
bool inQ = false;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
if (c == '\"') { inQ = !inQ; continue; }
if (c == ',' && !inQ) { list.Add(sb.ToString()); sb.Clear(); continue; }
sb.Append(c);
}
list.Add(sb.ToString());
return list.ToArray();
}
public static string JoinFrom(string[] arr, int start)
{
if (arr == null || start >= arr.Length) return "";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = start; i < arr.Length; i++)
{
if (i > start) sb.Append(" ");
sb.Append(arr[i]);
}
return sb.ToString();
}
public static long ParseLong(string s)
{
try { return long.Parse(s.Trim(), CultureInfo.InvariantCulture); } catch { return -1; }
}
public static long ParseHumanSizeSI(string s)
{
try
{
s = s.Trim();
string[] parts = SplitSpaces(s);
if (parts.Length < 2) return -1;
double val = double.Parse(parts[0], CultureInfo.InvariantCulture);
string u = parts[1].ToUpperInvariant();
double mul = 1.0;
if (u.StartsWith("TB")) mul = 1e12;
else if (u.StartsWith("GB")) mul = 1e9;
else if (u.StartsWith("MB")) mul = 1e6;
else if (u.StartsWith("KB")) mul = 1e3;
return (long)(val * mul);
}
catch { return -1; }
}
public static string FormatBytes(long bytes)
{
if (bytes < 0) return "unknown";
string[] u = new string[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };
double v = bytes;
int i = 0;
while (v >= 1024.0 && i < u.Length - 1) { v /= 1024.0; i++; }
return v.ToString("0.##", CultureInfo.InvariantCulture) + " " + u[i];
}
public static string FormatDevice(DeviceInfo d)
{
string m = Safe(d.Model);
string sz = FormatBytes(d.SizeBytes);
return m + " (" + sz + ") — " + d.Path;
}
public static string Safe(string s) { return string.IsNullOrWhiteSpace(s) ? "-" : s; }
public static string GetDesktopPath()
{
try
{
string p = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
if (!string.IsNullOrWhiteSpace(p)) return p;
}
catch { }
try { return Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } catch { }
return "";
}
public static void EnsureDirectory(string path)
{
try
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine("[red]Failed to create directory:[/] " + ex.Message);
Environment.Exit(5);
}
}
public static string RunProcess(string file, string args)
{
try
{
var psi = new ProcessStartInfo();
psi.FileName = file;
psi.Arguments = args;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
using (var p = Process.Start(psi))
{
string stdout = p.StandardOutput.ReadToEnd();
p.WaitForExit(5000);
return stdout;
}
}
catch { return ""; }
}
public static string BrowseForDirectory(string startPath)
{
string current = string.IsNullOrWhiteSpace(startPath) ? Directory.GetCurrentDirectory() : startPath;
while (true)
{
if (!Directory.Exists(current))
current = Directory.GetCurrentDirectory();
var options = new List<string>();
options.Add(".. (Go up)");
try
{
foreach (var dir in Directory.EnumerateDirectories(current))
{
options.Add(Path.GetFileName(dir));
}
}
catch { }
options.Add("✔ Select this directory");
options.Add("+ Create new folder");
options.Add("⌨ Enter path");
var prompt = new SelectionPrompt<string>()
.Title($"[cyan]Output directory[/]\n[grey]{current}[/]")
.PageSize(15)
.MoreChoicesText("[grey](use up/down to navigate)[/]")
.AddChoices(options);
string choice = AnsiConsole.Prompt(prompt);
if (choice == ".. (Go up)")
{
string parent = Directory.GetParent(current)?.FullName;
if (!string.IsNullOrEmpty(parent)) current = parent;
}
else if (choice == "✔ Select this directory")
{
EnsureDirectory(current);
return current;
}
else if (choice == "+ Create new folder")
{
string name = AnsiConsole.Ask<string>("New folder name:");
if (!string.IsNullOrWhiteSpace(name))
{
string newPath = Path.Combine(current, name);
EnsureDirectory(newPath);
current = newPath;
}
}
else if (choice == "⌨ Enter path")
{
string path = AnsiConsole.Ask<string>("Enter path:");
if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path))
current = Path.GetFullPath(path);
}
else
{
string next = Path.Combine(current, choice);
if (Directory.Exists(next)) current = next;
}
}
}
}