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+ }
0 commit comments