void Fail(Exception exception)
        {
            var logMessage = ColorSettings.Error + "Fail : " + exception?.Message;

            Out.Echoln(logMessage);
            LogError(logMessage);
        }
Пример #2
0
        public CommandResult <List <string> > Alias(
            CommandEvaluationContext context,
            [Parameter(0, "name of the alias", true)] string name,
            [Parameter(1, "text of the alias", true)][OptionRequireParameter("name")] string text,
            [Option("s", "save current aliases to user aliases file")] bool save
            )
        {
            var r = new List <string>();

            if (name == null)
            {
                foreach (var kvp in context.CommandLineProcessor.CommandsAlias.Aliases)
                {
                    Out.Echoln(CommandsAlias.BuildAliasCommand(kvp.Key, kvp.Value));
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(text))
                {
                    context.CommandLineProcessor.CommandsAlias.UnsetAlias(context, name);
                }
                else
                {
                    context.CommandLineProcessor.CommandsAlias.AddOrReplaceAlias(context, name, text);
                }
            }
            if (save)
            {
                context.CommandLineProcessor.CommandsAlias.SaveAliases(context);
            }
            return(new CommandResult <List <string> >(r));
        }
        void Success(string message = null)
        {
            var logMessage = ColorSettings.Success + "Success" + (message == null?"":$" : {message}");

            Out.Echoln(logMessage);
            Log(logMessage);
        }
Пример #4
0
        /// <summary>
        /// ask a question, read input to enter. returns true if input is 'y' or 'Y'
        /// </summary>
        /// <param name="question"></param>
        /// <returns>returns true if input is 'y' or 'Y'</returns>
        public static bool Confirm(string question)
        {
            var r = false;

            void endReadln(IAsyncResult result)
            {
                r = result.AsyncState?.ToString()?.ToLower() == "y";
            }

            var cmdlr = new CommandLineReader(null, question + "? ", null);

            cmdlr.BeginReadln(endReadln, null, true, false);
            Out.Echoln();
            return(r);
        }
Пример #5
0
        public void Log(string s, bool enableForwardLogsToSystemDiagnostics = true)
        {
            if (ForwardLogsToSystemDiagnostics && enableForwardLogsToSystemDiagnostics)
            {
                System.Diagnostics.Debug.WriteLine(s);
            }
            var ls = (s + "").Split(_crlf, StringSplitOptions.None)
                     .Select(x => Colors.Log + x);

            //Out.Echoln(ls);
            foreach (var l in ls)
            {
                Out.Echoln(l);
            }
        }
Пример #6
0
        public void Infos()
        {
            lock (Out.Lock)
            {
                Out.Echoln($"OS={Environment.OSVersion} {(Environment.Is64BitOperatingSystem ? "64" : "32")}bits plateform={RuntimeEnvironment.OSType}");
                Out.Echoln($"{White}{Bkf}{Colors.HighlightIdentifier}window:{Rsf} left={Colors.Numeric}{sc.WindowLeft}{Rsf},top={Colors.Numeric}{sc.WindowTop}{Rsf},width={Colors.Numeric}{sc.WindowWidth}{Rsf},height={Colors.Numeric}{sc.WindowHeight}{Rsf},largest width={Colors.Numeric}{sc.LargestWindowWidth}{Rsf},largest height={Colors.Numeric}{sc.LargestWindowHeight}{Rsf}");
                Out.Echoln($"{Colors.HighlightIdentifier}buffer:{Rsf} width={Colors.Numeric}{sc.BufferWidth}{Rsf},height={Colors.Numeric}{sc.BufferHeight}{Rsf} | input encoding={Colors.Numeric}{sc.InputEncoding.EncodingName}{Rsf} | output encoding={Colors.Numeric}{sc.OutputEncoding.EncodingName}{Rsf}");
                Out.Echoln($"{White}default background color={Bkf}{Colors.KeyWord}{DefaultBackground}{Rsf} | default foreground color={Colors.KeyWord}{DefaultForeground}{Rsf}");
                if (RuntimeEnvironment.OSType == OSPlatform.Windows)
                {
#pragma warning disable CA1416 // Valider la compatibilité de la plateforme
                    Out.Echoln($"number lock={Colors.Numeric}{sc.NumberLock}{Rsf} | capslock={Colors.Numeric}{sc.CapsLock}{Rsf}");
#pragma warning restore CA1416 // Valider la compatibilité de la plateforme
#pragma warning disable CA1416 // Valider la compatibilité de la plateforme
                    Out.Echoln($"cursor visible={Colors.Numeric}{sc.CursorVisible}{Rsf} | cursor size={Colors.Numeric}{sc.CursorSize}");
#pragma warning restore CA1416 // Valider la compatibilité de la plateforme
                }
            };
        }
        void InitializeCommandProcessor(string[] args, bool printInfo = true, CommandEvaluationContext commandEvaluationContext = null)
        {
            SetArgs(args);

            cons.ForegroundColor = DefaultForeground;
            cons.BackgroundColor = DefaultBackground;

            commandEvaluationContext = commandEvaluationContext ??
                                       new CommandEvaluationContext(
                this,
                Out,
                cons.In,
                Err,
                null
                );
            CommandEvaluationContext = commandEvaluationContext;

            if (printInfo)
            {
                PrintInfo(CommandEvaluationContext);
            }

            // assume the application folder ($env.APPDATA/OrbitalShell) exists and is initialized

            var lbr = false;

            // creates user app data folders
            if (!Directory.Exists(AppDataFolderPath))
            {
                LogAppendAllLinesErrorIsEnabled = false;
                Info(ColorSettings.Log + $"creating user shell folder: '{AppDataFolderPath}' ... ", false);
                try
                {
                    Directory.CreateDirectory(AppDataFolderPath);
                    Success();
                } catch (Exception createAppDataFolderPathException)
                {
                    Fail(createAppDataFolderPathException);
                }
                lbr = true;
            }

            // initialize log file
            if (!File.Exists(LogFilePath))
            {
                Info(ColorSettings.Log + $"creating log file: '{LogFilePath}' ... ", false);
                try
                {
                    var logError = Log($"file created on {System.DateTime.Now}");
                    if (logError == null)
                    {
                        Success();
                    }
                    else
                    {
                        throw logError;
                    }
                }
                catch (Exception createLogFileException)
                {
                    LogAppendAllLinesErrorIsEnabled = false;
                    Fail(createLogFileException);
                }
                lbr = true;
            }

            // initialize user profile
            if (!File.Exists(UserProfileFilePath))
            {
                Info(ColorSettings.Log + $"creating user profile file: '{UserProfileFilePath}' ... ", false);
                try
                {
                    var defaultProfileFilePath = Path.Combine(DefaultsFolderPath, UserProfileFileName);
                    File.Copy(defaultProfileFilePath, UserProfileFilePath);
                    Success();
                }
                catch (Exception createUserProfileFileException)
                {
                    Fail(createUserProfileFileException);
                }
                lbr = true;
            }

            // create/restore commands history
            CmdsHistory = new CommandsHistory();
            var createNewHistoryFile = !File.Exists(HistoryFilePath);

            if (createNewHistoryFile)
            {
                Info(ColorSettings.Log + $"creating user commands history file: '{HistoryFilePath}' ... ", false);
            }
            try
            {
                if (createNewHistoryFile)
#pragma warning disable CS0642 // Possibilité d'instruction vide erronée
                {
                    using (var fs = File.Create(HistoryFilePath));
                }
#pragma warning restore CS0642 // Possibilité d'instruction vide erronée
                CmdsHistory.Init(AppDataFolderPath, HistoryFileName);
                if (createNewHistoryFile)
                {
                    Success();
                }
            }
            catch (Exception createUserProfileFileException)
            {
                Fail(createUserProfileFileException);
            }
            lbr |= createNewHistoryFile;

            // create/restore user aliases
            CommandsAlias = new CommandsAlias();
            var createNewCommandsAliasFile = !File.Exists(CommandsAliasFilePath);
            if (createNewCommandsAliasFile)
            {
                Info(ColorSettings.Log + $"creating user commands aliases file: '{CommandsAliasFilePath}' ... ", false);
            }
            try
            {
                if (createNewCommandsAliasFile)
                {
                    var defaultAliasFilePath = Path.Combine(DefaultsFolderPath, CommandsAliasFileName);
                    File.Copy(defaultAliasFilePath, CommandsAliasFilePath);
                }
                if (createNewCommandsAliasFile)
                {
                    Success();
                }
            }
            catch (Exception createUserProfileFileException)
            {
                Fail(createUserProfileFileException);
            }
            lbr |= createNewHistoryFile;

            // end inits
            if (lbr)
            {
                Out.Echoln();
            }

            // load kernel commands
            RegisterCommandsAssembly(CommandEvaluationContext, Assembly.GetExecutingAssembly());
#if enable_test_commands
            RegisterCommandsClass <TestCommands>();
#endif
        }