예제 #1
0
        public static void Print(
            ConsoleTextWriterWrapper @out,
            CancellationTokenSource cancellationTokenSource,
            Table table,
            bool noBorders     = false,
            bool padLastColumn = true)
        {
            @out.EnableFillLineFromCursor = false;
            @out.HideCur();
            var colLengths = new int[table.Columns.Count];

            foreach (var rw in table.Rows)
            {
                var cols = ((DataRow)rw).ItemArray;
                for (int i = 0; i < cols.Length; i++)
                {
                    var s = @out.GetPrint(table.GetFormatedValue(table.Columns[i].ColumnName, cols[i]?.ToString())) ?? "";
                    colLengths[i] = Math.Max(s.Length, colLengths[i]);
                    var s2 = @out.GetPrint(table.GetFormatedHeader(table.Columns[i].ColumnName)) ?? "";
                    colLengths[i] = Math.Max(s2.Length, colLengths[i]);
                }
            }
            var colsep       = noBorders ? " " : (ColorSettings.TableBorder + " | " + ColorSettings.Default);
            var colseplength = noBorders ? 0 : 3;
            var tablewidth   = noBorders ? 0 : 3;

            for (int i = 0; i < table.Columns.Count; i++)
            {
                tablewidth += table.Columns[i].ColumnName.PadRight(colLengths[i], ' ').Length + colseplength;
            }
            var line = noBorders ? "" : (ColorSettings.TableBorder + "".PadRight(tablewidth, '-'));

            if (!noBorders)
            {
                @out.Echoln(line);
            }
            for (int i = 0; i < table.Columns.Count; i++)
            {
                if (i == 0)
                {
                    @out.Echo(colsep);
                }
                var col     = table.Columns[i];
                var colName = (i == table.Columns.Count - 1 && !padLastColumn) ?
                              table.GetFormatedHeader(col.ColumnName)
                    : table.GetFormatedHeader(col.ColumnName).PadRight(colLengths[i], ' ');
                @out.Echo(ColorSettings.TableColumnName + colName + colsep);
            }
            @out.Echoln();
            if (!noBorders)
            {
                @out.Echoln(line);
            }

            foreach (var rw in table.Rows)
            {
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    @out.EnableFillLineFromCursor = true;
                    @out.ShowCur();
                    @out.Echoln(ColorSettings.Default + "");
                    return;
                }
                var row = (DataRow)rw;
                var arr = row.ItemArray;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (i == 0)
                    {
                        Out.Echo(colsep);
                    }
                    var txt    = (arr[i] == null) ? "" : arr[i].ToString();
                    var fvalue = table.GetFormatedValue(table.Columns[i].ColumnName, txt);
                    var l      = Out.GetPrint(fvalue).Length;
                    var spc    = (i == arr.Length - 1 && !padLastColumn) ? "" : ("".PadRight(Math.Max(0, colLengths[i] - l), ' '));
                    @out.Echo(ColorSettings.Default + fvalue + spc + colsep);
                }
                @out.Echoln();
            }
            @out.Echoln(line + ColorSettings.Default.ToString());
            @out.ShowCur();
            @out.EnableFillLineFromCursor = true;
        }
예제 #2
0
        public void ProcessCommandLine(
            string commandLine,
            ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine    = false,
            bool enableHistory          = false,
            bool enablePrePostComOutput = true)
        {
            if (commandLine != null)
            {
                if (outputStartNextLine)
                {
                    Out.LineBreak();
                }
                ExpressionEvaluationResult expressionEvaluationResult = null;

                try
                {
                    sc.CancelKeyPress += CancelKeyPress;
                    CommandLineProcessor.CancellationTokenSource = new CancellationTokenSource();
                    Out.IsModified = false;
                    Err.IsModified = false;

                    var task = Task.Run <ExpressionEvaluationResult>(
                        () => evalCommandDelegate(
                            CommandLineProcessor.CommandEvaluationContext,
                            commandLine,
                            _prompt == null ? 0 : Out.GetPrint(_prompt).Length,            // TODO has no sens with multi line prompt !!!
                            (enablePrePostComOutput && CommandLineProcessor != null) ?
                            CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPreAnalysisOutput) : ""),
                        CommandLineProcessor.CancellationTokenSource.Token
                        );

                    try
                    {
                        try
                        {
                            task.Wait(CommandLineProcessor.CancellationTokenSource.Token);
                        }
                        catch (ThreadInterruptedException)
                        {
                            // get interrupted after send input
                        }
                        expressionEvaluationResult = task.Result;
                    }
                    catch (OperationCanceledException)
                    {
                        //var res = task.Result;
                        expressionEvaluationResult = task.Result;
                        Out.Warningln($"command canceled: {commandLine}");
                    }
                    finally { }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
                finally
                {
                    if (enablePrePostComOutput && CommandLineProcessor != null)
                    {
                        if (Out.IsModified || Err.IsModified)
                        {
                            if (!(Out.CursorLeft == 0 && Out.CursorTop == 0))
                            {
                                Out.Echo(CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutModifiedOutput));
                            }
                        }
                        Out.Echo(CommandLineProcessor.CommandEvaluationContext.ShellEnv.GetValue <string>(ShellEnvironmentVar.settings_clr_comPostExecOutput));
                    }

                    CommandLineProcessor.CancellationTokenSource.Dispose();
                    CommandLineProcessor.CancellationTokenSource = null;
                    sc.CancelKeyPress -= CancelKeyPress;
                }
            }

            if (enableHistory && !string.IsNullOrWhiteSpace(commandLine))
            {
                CommandLineProcessor.CmdsHistory.HistoryAppend(commandLine);
            }
        }
        public void ProcessCommandLine(
            string commandLine,
            ExpressionEvaluationCommandDelegate evalCommandDelegate,
            bool outputStartNextLine = false,
            bool enableHistory       = false)
        {
            if (commandLine != null)
            {
                if (outputStartNextLine)
                {
                    Out.LineBreak();
                }

                ExpressionEvaluationResult expressionEvaluationResult = null;

                try
                {
                    sc.CancelKeyPress += CancelKeyPress;
                    CommandLineProcessor.CancellationTokenSource = new CancellationTokenSource();
                    var task = Task.Run <ExpressionEvaluationResult>(
                        () => evalCommandDelegate(CommandLineProcessor.CommandEvaluationContext, commandLine, _prompt == null ? 0 : Out.GetPrint(_prompt).Length),
                        CommandLineProcessor.CancellationTokenSource.Token
                        );

                    try
                    {
                        try
                        {
                            task.Wait(CommandLineProcessor.CancellationTokenSource.Token);
                        } catch (ThreadInterruptedException) {
                            // get interrupted after send input
                        }
                        expressionEvaluationResult = task.Result;
                    }
                    catch (OperationCanceledException)
                    {
                        var res = task.Result;
                        Warningln($"command canceled: {commandLine}");
                    }
                    finally
                    {
                    }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
                finally
                {
                    CommandLineProcessor.CancellationTokenSource.Dispose();
                    CommandLineProcessor.CancellationTokenSource = null;
                    sc.CancelKeyPress -= CancelKeyPress;
                    lock (ConsoleLock)
                    {
                        /*if (!WorkArea.rect.IsEmpty && (WorkArea.rect.Y != CursorTop || WorkArea.rect.X != CursorLeft))
                         *  LineBreak();*/// case of auto line break (spacing)

                        //Out.RestoreDefaultColors();   // FGZ removed
                    }
                }
            }
            if (enableHistory && !string.IsNullOrWhiteSpace(commandLine))
            {
                CommandLineProcessor.CmdsHistory.HistoryAppend(commandLine);
            }
        }