-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeLoader.cs
More file actions
183 lines (167 loc) · 8.54 KB
/
ThemeLoader.cs
File metadata and controls
183 lines (167 loc) · 8.54 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
using System.Text.Json.Nodes;
namespace NShell.Shell.Themes
{
/// <summary>
/// Enum representing the available default themes.
/// </summary>
public enum DefaultThemesEnum
{
Default,
Light
}
/// <summary>
/// Manages loading and retrieving custom or predefined themes.
/// </summary>
public static class ThemeLoader
{
private static readonly Dictionary<string, DefaultThemesEnum> _stringToEnum = new()
{
{ "default", DefaultThemesEnum.Default },
{ "light", DefaultThemesEnum.Light }
};
/// <summary>
/// Tries to get a default theme from a name.
/// </summary>
/// <param name="name">The theme name (case-insensitive).</param>
/// <param name="theme">The resulting theme if found.</param>
/// <returns>True if the theme was found, false otherwise.</returns>
public static bool TryGetTheme(string name, out DefaultThemesEnum theme)
{
return _stringToEnum.TryGetValue(name.ToLower(), out theme);
}
/// <summary>
/// Converts a predefined theme to a formatted string for the prompt.
/// </summary>
/// <param name="theme">The theme to apply.</param>
/// <param name="currentDirectory">The current directory to display in the prompt.</param>
/// <returns>Prompt string and associated LS_COLORS.</returns>
public static string[] ToStringValue(DefaultThemesEnum theme, string currentDirectory)
{
return theme switch
{
DefaultThemesEnum.Default =>
new string[] {
$"[white]\u250c[/][bold green][[{Environment.UserName}@{Environment.MachineName}]][/]\n[white]\u2514[/][blue][[{currentDirectory}]][/] >> ",
"di=34:fi=37:ln=36:pi=33:so=35:ex=32"
},
DefaultThemesEnum.Light =>
new string[] {
$"[white]\u250c[[[/][silver]{Environment.UserName}[/][red]@[/][silver]{Environment.MachineName}[/][white]]]\n\u2514[[[/]{ColorizePathLight("red", "silver", currentDirectory)}[white]]][/] >> ",
"di=37:fi=30:ln=36:pi=33:so=35:ex=32"
},
_ => new string[] { "[[[yellow]*[/]]] - Unknown theme" }
};
}
/// <summary>
/// Colors each character in the path for the Light theme:
/// '/' is colored red, other characters are colored silver.
/// </summary>
private static string ColorizePathLight(string slashColor, string wordsColor,string path)
{
var sb = new System.Text.StringBuilder();
foreach (var c in path)
{
if (c == '/')
sb.Append($"[{slashColor}]/[/]");
else
sb.Append($"[{wordsColor}]{c}[/]");
}
return sb.ToString();
}
/// <summary>
/// Loads a custom theme from the <c>~/.nshell/themes/</c> directory,
/// based on the name found in the theme's JSON file.
/// If the directory doesn't exist, it will be created.
/// </summary>
/// <param name="themeName">The name of the theme (as defined by the "name" field in the JSON file).</param>
/// <param name="currentDirectory">The current directory to display in the prompt.</param>
/// <returns>Prompt string and LS_COLORS if theme found, error message otherwise.</returns>
public static string[] LoadCustomTheme(string themeName, string currentDirectory)
{
string themesDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nshell", "themes");
if (!Directory.Exists(themesDirectory))
{
try
{
Directory.CreateDirectory(themesDirectory);
}
catch (Exception ex)
{
return new[] { $"[[[red]-[/]]] - Could not create themes directory: {ex.Message}" };
}
}
var themeFiles = Directory.GetFiles(themesDirectory, "*.json");
foreach (var filePath in themeFiles)
{
string json = File.ReadAllText(filePath);
JsonNode? data = JsonNode.Parse(json);
if (data?["name"]?.ToString().Equals(themeName, StringComparison.OrdinalIgnoreCase) == true)
{
string? format = data["format"]?.ToString();
string? formatTop = data["format_top"]?.ToString();
string? formatBottom = data["format_bottom"]?.ToString();
string? cornerTop = data["corner_top"]?.ToString();
string? cornerBottom = data["corner_bottom"]?.ToString();
string? pathSlashColor = data["path_slash_color"]?.ToString();
string? pathWordsColor = data["path_words_color"]?.ToString();
string? colors = data["ls_colors"]?.ToString() ?? "di=34:fi=37:ln=36:pi=33:so=35:ex=32";
if (cornerTop != null && cornerBottom != null && formatTop != null && formatBottom != null)
{
if (pathSlashColor != null && pathWordsColor != null)
{
string prompt = $"{cornerTop}{formatTop.Replace("{user}", Environment.UserName).Replace("{host}", Environment.MachineName)}\n" +
$"{cornerBottom}{formatBottom.Replace("{cwd}", ColorizePathLight(pathSlashColor, pathWordsColor, currentDirectory))} >> ";
return new[] { prompt, colors };
}
else
{
string prompt = $"{cornerTop}{formatTop.Replace("{user}", Environment.UserName).Replace("{host}", Environment.MachineName)}\n" +
$"{cornerBottom}{formatBottom.Replace("{cwd}", currentDirectory)} >> ";
return new[] { prompt, colors };
}
}
if (format != null)
{
if (pathSlashColor != null && pathWordsColor != null)
{
string prompt = format
.Replace("{user}", Environment.UserName)
.Replace("{host}", Environment.MachineName)
.Replace("{cwd}", ColorizePathLight(pathSlashColor, pathWordsColor, currentDirectory));
return new[] { prompt, colors };
}
else
{
string prompt = format
.Replace("{user}", Environment.UserName)
.Replace("{host}", Environment.MachineName)
.Replace("{cwd}", currentDirectory);
return new[] { prompt, colors };
}
}
}
}
return new[] { "[[[red]-[/]]] - Theme not found." };
}
/// <summary>
/// Returns all available default theme names.
/// </summary>
/// <returns>An array of strings representing the theme names.</returns>
public static string[] GetAllThemes()
{
return _stringToEnum.Keys.ToArray();
}
/// <summary>
/// Tries to load a custom JSON theme and returns true if successful.
/// </summary>
/// <param name="themeName">The theme file name without the .json extension.</param>
/// <param name="result">Array containing the prompt and LS_COLORS if successful.</param>
/// <returns>True if the theme was loaded successfully, false otherwise.</returns>
public static bool TryLoadJsonTheme(string themeName, out string[] result)
{
result = LoadCustomTheme(themeName, Directory.GetCurrentDirectory());
return !result[0].Contains("invalid", StringComparison.OrdinalIgnoreCase) &&
!result[0].Contains("not found", StringComparison.OrdinalIgnoreCase);
}
}
}