Skip to content

Commit 037c91d

Browse files
authored
Merge pull request #31 from onihilist/feat/config-file
[FEAT] - config file
2 parents ad23530 + eb3db38 commit 037c91d

8 files changed

Lines changed: 68 additions & 14 deletions

File tree

Shell/Commands/CommandLoader.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static class CommandLoader
1616
public static void RefreshCommands()
1717
{
1818
var paths = Environment.GetEnvironmentVariable("PATH")?.Split(':') ?? Array.Empty<string>();
19-
2019
var newCommands = new HashSet<string>();
2120

2221
foreach (var path in paths)
@@ -46,9 +45,7 @@ public static void RefreshCommands()
4645
AnsiConsole.MarkupLine($"[[[red]-[/]]] - Error accessing directory {path}: [bold yellow]{ex.Message}[/]");
4746
}
4847
}
49-
5048
HashSet<string> availableCommands = newCommands;
5149
AnsiConsole.MarkupLine($"[[[green]+[/]]] - Refreshed command list. Found {availableCommands.Count} new commands.");
5250
}
53-
5451
}

Shell/Config/ShellConfig.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
using System.Text.Json.Nodes;
3+
4+
namespace NShell.Shell.Config;
5+
6+
public class ShellConfig
7+
{
8+
public int HistoryExpirationTime { get; set; }
9+
public int HistoryMaxStorage { get; set; }
10+
public required string SelectedTheme { get; set; }
11+
12+
public static ShellConfig? LoadConfig()
13+
{
14+
var configFile = Directory.GetFiles($"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/.nshell", "nshell.conf.json");
15+
16+
foreach (var filePath in configFile)
17+
{
18+
string json = File.ReadAllText(filePath);
19+
JsonNode? data = JsonNode.Parse(json);
20+
JsonNode? historyNode = data?["configuration"]?["nshell"]?["history"]?[0];
21+
JsonNode? themeNode = data?["configuration"]?["nshell"]?["theme"]?[0];
22+
23+
if (historyNode != null && themeNode != null)
24+
{
25+
string expiration = historyNode["expiration_time"]?.ToString() ?? "0d";
26+
int days = ParseExpirationTime(expiration);
27+
28+
return new ShellConfig
29+
{
30+
HistoryExpirationTime = days,
31+
HistoryMaxStorage = historyNode["max_storage"]?.GetValue<int>() ?? 0,
32+
SelectedTheme = themeNode["selected_theme"]?.ToString() ?? "default"
33+
};
34+
}
35+
}
36+
37+
return null;
38+
}
39+
40+
private static int ParseExpirationTime(string expiration)
41+
{
42+
if (expiration.EndsWith("w") && int.TryParse(expiration[..^1], out int weeks))
43+
{
44+
return weeks*168;
45+
} if (expiration.EndsWith("d") && int.TryParse(expiration[..^1], out int days))
46+
{
47+
return days*24;
48+
} if (expiration.EndsWith("h") && int.TryParse(expiration[..^1], out int hours))
49+
{
50+
return hours;
51+
}
52+
return 0;
53+
}
54+
}

Shell/History/HistoryManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class HistoryManager
1818

1919
public HistoryManager(string? path = null)
2020
{
21-
_historyPath = path ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nshell/.history");
21+
_historyPath = path ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nshell/.nhistory");
2222
Load();
2323
}
2424

Shell/Keyboard/KeyboardHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public static void Handler(HistoryManager history, ShellContext context, Command
2828

2929
if (key.Key == ConsoleKey.Enter)
3030
{
31+
if (inputBuffer.Trim() != "")
32+
{
33+
history.Add(inputBuffer);
34+
history.Save();
35+
}
3136
Console.WriteLine();
3237
break;
3338
}

Shell/Plugin/PluginLoader.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,5 @@ public void LoadPlugins()
5151
Directory.CreateDirectory(PluginFolderPath);
5252
}
5353
}
54-
5554
}
56-
5755
}

Shell/Themes/ThemeLoader.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11

2-
using System;
3-
using System.Collections.Generic;
4-
using System.IO;
5-
using System.Linq;
6-
using System.Text.Json;
72
using System.Text.Json.Nodes;
83

94
namespace NShell.Shell.Themes

install.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ N_SHELL_DIR="$USER_PROFILE/.nshell"
3131
HISTORY_FILE="$N_SHELL_DIR/.nhistory"
3232
THEMES_DIR="$N_SHELL_DIR/themes"
3333
PLUGINS_DIR="$N_SHELL_DIR/plugins"
34+
CONFIG_FILE="$N_SHELL_DIR/nshell.conf.json"
3435

3536
echo "[*] - Creating directories/files if they don't exist..."
3637

3738
mkdir -p "$THEMES_DIR"
3839
mkdir -p "$PLUGINS_DIR"
3940
touch "$HISTORY_FILE"
41+
curl -L "https://gist.githubusercontent.com/onihilist/8bf7548dc7478f1b6af2db4bdc0c668d/raw/28f46aa5ebcb27b4f0edb302e2ff4347f49a2f83/nshell.conf.json" -o $CONFIG_FILE
4042

4143
echo "[+] - Directories/Files created or already exist: "
4244
echo " - $THEMES_DIR"
4345
echo " - $PLUGINS_DIR"
4446
echo " - $HISTORY_FILE"
47+
echo " - $CONFIG_FILE"
4548

4649
echo "[*] - Compiling NShell..."
4750
dotnet publish NShell.csproj \

uninstall.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ chsh -s /bin/bash || {
1010
exit 1
1111
}
1212

13+
USER_PROFILE=$(eval echo ~$USER)
14+
N_SHELL_DIR="$USER_PROFILE/.nshell"
1315
BIN_PATH="/usr/local/bin/nshell"
1416

1517
if [ -f "$BIN_PATH" ]; then
16-
echo "[*] - Removing $BIN_PATH..."
17-
sudo rm "$BIN_PATH"
18+
echo "[*] - Removing $BIN_PATH & related folders/files..."
19+
sudo rm -rf "$BIN_PATH" "$N_SHELL_DIR"
1820
else
19-
echo "[-] - No executable found at $BIN_PATH"
21+
echo "[-] - No executable found at $BIN_PATH"discord
2022
fi
2123

2224
if grep -Fxq "$BIN_PATH" /etc/shells; then

0 commit comments

Comments
 (0)