-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellConfig.cs
More file actions
54 lines (45 loc) · 1.84 KB
/
ShellConfig.cs
File metadata and controls
54 lines (45 loc) · 1.84 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
using System.Text.Json.Nodes;
namespace NShell.Shell.Config;
public class ShellConfig
{
public int HistoryExpirationTime { get; set; }
public int HistoryMaxStorage { get; set; }
public required string SelectedTheme { get; set; }
public static ShellConfig? LoadConfig()
{
var configFile = Directory.GetFiles($"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/.nshell", "nshell.conf.json");
foreach (var filePath in configFile)
{
string json = File.ReadAllText(filePath);
JsonNode? data = JsonNode.Parse(json);
JsonNode? historyNode = data?["configuration"]?["nshell"]?["history"]?[0];
JsonNode? themeNode = data?["configuration"]?["nshell"]?["theme"]?[0];
if (historyNode != null && themeNode != null)
{
string expiration = historyNode["expiration_time"]?.ToString() ?? "0d";
int days = ParseExpirationTime(expiration);
return new ShellConfig
{
HistoryExpirationTime = days,
HistoryMaxStorage = historyNode["max_storage"]?.GetValue<int>() ?? 0,
SelectedTheme = themeNode["selected_theme"]?.ToString() ?? "default"
};
}
}
return null;
}
private static int ParseExpirationTime(string expiration)
{
if (expiration.EndsWith("w") && int.TryParse(expiration[..^1], out int weeks))
{
return weeks*168;
} if (expiration.EndsWith("d") && int.TryParse(expiration[..^1], out int days))
{
return days*24;
} if (expiration.EndsWith("h") && int.TryParse(expiration[..^1], out int hours))
{
return hours;
}
return 0;
}
}