From fe1961947ceb09c051994fd925e2ec0934188c3b Mon Sep 17 00:00:00 2001 From: wagesj45 Date: Wed, 1 Jul 2026 00:58:33 -0500 Subject: [PATCH] Add startup config loading --- src/cs-mic-cli/Options.cs | 2 - src/cs-mic-cli/Program.cs | 82 ++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/cs-mic-cli/Options.cs b/src/cs-mic-cli/Options.cs index d69d661..edbd808 100644 --- a/src/cs-mic-cli/Options.cs +++ b/src/cs-mic-cli/Options.cs @@ -6,8 +6,6 @@ public bool Interactive { get; set; } - public bool Version { get; set; } - public IEnumerable Expression { get; set; } = Array.Empty(); } } diff --git a/src/cs-mic-cli/Program.cs b/src/cs-mic-cli/Program.cs index 512486e..4d35e08 100644 --- a/src/cs-mic-cli/Program.cs +++ b/src/cs-mic-cli/Program.cs @@ -10,7 +10,14 @@ namespace cs_mic_cli static int Main(string[] args) { - var verboseOption = new Option("--verbose", "-v") + if (args.Length == 1 && args[0] == "--version") + { + PrintVersion(Console.Out); + + return 0; + } + + var verboseOption = new Option("--verbose") { Description = "Displays error messages from the parser if a soft error occurs." }; @@ -18,10 +25,6 @@ namespace cs_mic_cli { Description = "Starts an interactive session." }; - var versionOption = new Option("--version", "-v") - { - Description = "Prints the version number of CS-MIC." - }; var expressionArgument = new Argument("expression") { Arity = ArgumentArity.ZeroOrMore, @@ -50,7 +53,6 @@ namespace cs_mic_cli { Verbose = parseResult.GetValue(verboseOption), Interactive = parseResult.GetValue(interactiveOption), - Version = parseResult.GetValue(versionOption), Expression = parseResult.GetValue(expressionArgument) ?? Array.Empty() }; @@ -74,14 +76,7 @@ namespace cs_mic_cli CSMic.Interpreter.Errors.errorStream = TextWriter.Null; } - if(options.Version) - { - Console.WriteLine(Functions.Version.VersionCLI); - Console.WriteLine(Functions.Version.VersionCSMIC); - Console.WriteLine(Functions.Version.VersionStandardLibrary); - - return 0; - } + LoadUserConfig(inputInterpreter, options); if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive) { @@ -120,6 +115,65 @@ namespace cs_mic_cli return 0; } + private static void LoadUserConfig(InputInterpreter inputInterpreter, Options options) + { + var configPath = GetUserConfigPath(); + var configDirectory = Path.GetDirectoryName(configPath); + + try + { + if (!string.IsNullOrWhiteSpace(configDirectory)) + { + Directory.CreateDirectory(configDirectory); + } + + if (!File.Exists(configPath)) + { + File.Create(configPath).Dispose(); + } + + foreach (var line in File.ReadLines(configPath)) + { + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } + + inputInterpreter.Interpret(line); + } + } + catch (Exception ex) + { + Console.Error.WriteLine("Failed to load config: {0}", configPath); + + if (options.Verbose) + { + Console.Error.WriteLine(ex.Message); + } + } + } + + private static string GetUserConfigPath() + { + var configHome = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME"); + + if (string.IsNullOrWhiteSpace(configHome)) + { + configHome = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), + ".config"); + } + + return Path.Combine(configHome, "csmic", "init.csmic"); + } + + private static void PrintVersion(TextWriter output) + { + output.WriteLine(Functions.Version.VersionCLI); + output.WriteLine(Functions.Version.VersionCSMIC); + output.WriteLine(Functions.Version.VersionStandardLibrary); + } + private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression, ExitRequestedState? exitRequestedState = null) { inputInterpreter.Interpret(expression);