Ejemplo n.º 1
0
        public string ToColorizedString(ColorSettings colors, bool grammarSymbolsVisible = true)
        {
            var f = GetCmd(EchoDirectives.f + "", DefaultForeground.ToString().ToLower());
            var r = $"{colors.ParameterName}{ParameterName}{f}";

            if (IsOption)
            {
                var    optVal = (HasValue) ? $" {colors.ParameterValueType}{ParameterInfo.ParameterType.UnmangledName()}" : "";
                string sepcar = grammarSymbolsVisible ? $"{colors.SyntaxSymbol}|" : ", ";
                //string longopt = OptionLongName != null ? $"{sepcar}{colors.OptionPrefix}{CommandLineSyntax.OptionLongPrefix}{colors.OptionName}{OptionLongName}" : "";

                string longopt = (OptionName != null && OptionLongName != null ? $"{sepcar}" : "")
                                 + (OptionLongName != null ? $"{colors.OptionPrefix}{CommandLineSyntax.OptionLongPrefix}{colors.OptionName}{OptionLongName}" : "");
                r = "";
                if (OptionName != null)
                {
                    r += $"{colors.OptionPrefix}{CommandLineSyntax.OptionPrefix}{colors.OptionName}{OptionName}";
                }
                r += $"{longopt}{optVal}{f}";
            }
            if (IsOptional && grammarSymbolsVisible)
            {
                r = $"{colors.SyntaxSymbol}[{r}{colors.SyntaxSymbol}]{f}";
            }
#if printDefaultValueInSyntax
            if (HasDefaultValue && grammarSymbolsVisible)
            {
                r += $"{Cyan}{{={($"{colors.Value}{DumpAsText(DefaultValue)}{Cyan}}}{f}")}";
            }
#endif
            return(r);
        }
        public void Print(bool printAttributes = false, bool shortPath = false, string prefix = "", string postfix = "", int paddingRight = -1, string linePrefix = "")
        {
            var bg    = GetCmd(EchoDirectives.b + "", DefaultBackground.ToString().ToLower());
            var fg    = GetCmd(EchoDirectives.f + "", DefaultForeground.ToString().ToLower());
            var color = (IsDirectory) ? NormalDirectoryColorization : FileColorization;

            if (!IsSystem && IsDirectory && !IsReadOnly)
            {
                color += WritableDirectoryColorization;
            }
            if (IsSystem && !IsDirectory)
            {
                color += SystemColorization + bg;
            }
            if (IsSystem && IsDirectory && !IsReadOnly)
            {
                color += SystemWritableDirectoryColorization;
            }
            if (IsFile && IsReadOnly)
            {
                color += ReadOnlyFileColorization;
            }
            var    endcolor = bg + fg;
            var    r        = "";
            var    attr     = "";
            string hidden   = "";

            if (printAttributes)
            {
                var dir = IsDirectory ? "d" : "-";
                var ro  = IsReadOnly ? "r-" : "rw";
                var sys = IsSystem ? "s" : "-";
                var h   = IsHidden ? "h" : "-";
                //var c = IsCompressed ? "c" : "-";
                var a      = IsArchive ? "a" : "-";
                var size   = (IsDirectory) ? "" : HumanFormatOfSize(((FileInfo)FileSystemInfo).Length, 2);
                var moddat = FileSystemInfo.LastWriteTime;
                hidden = IsHidden ? "*" : "";
                var dat     = (moddat.Year != System.DateTime.Now.Year) ? moddat.Year + "" : "";
                var smoddat = $"{dat,4} {moddat.ToString("MMM", CultureInfo.InvariantCulture),-3} {moddat.Day,-2} {moddat.Hour.ToString().PadLeft(2,'0')}:{moddat.Minute.ToString().PadLeft(2,'0')}";
                attr = $" {dir}{ro}{sys}{h}{a} {size,10} {smoddat}  ";
            }
            var name  = shortPath ? FileSystemInfo.Name : FileSystemInfo.FullName;
            var quote = name.Contains(' ') ? "\"" : "";
            var pdr   = paddingRight - name.Length;

            if (!string.IsNullOrWhiteSpace(quote))
            {
                pdr -= 2;
            }
            var rightspace = (paddingRight > -1) ? endcolor + "".PadRight(pdr > 0?pdr:1, ' ') : "";

            r += $"{linePrefix}{attr}{color}{prefix}{quote}{name}{quote}{hidden}{rightspace}{postfix}";
            Out.Echo(r + ColorSettings.Default);
            if (HasError)
            {
                Out.Echo($" {ErrorColorization}{GetError()}");
            }
        }
        public string ToColorizedString(bool grammarSymbolsVisible = true)
        {
            var f = GetCmd(EchoDirectives.f + "", DefaultForeground.ToString().ToLower());
            var r = $"{ColorSettings.ParameterName}{ParameterName}{f}";

            if (IsOption)
            {
                var optVal = (HasValue) ? $" {ColorSettings.ParameterValueType}{ParameterValueTypeName}" : "";
                r = $"{ColorSettings.OptionPrefix}{ParameterSyntax.OptionPrefix}{ColorSettings.OptionName}{OptionName}{optVal}{f}";
            }
            if (IsOptional && grammarSymbolsVisible)
            {
                r = $"{ColorSettings.SyntaxSymbol}[{r}{ColorSettings.SyntaxSymbol}]{f}";
            }
#if printDefaultValueInSyntax
            if (HasDefaultValue && grammarSymbolsVisible)
            {
                r += $"{Cyan}{{={($"{Darkyellow}{DumpAsText(DefaultValue)}{Cyan}}}{f}")}";
            }
#endif
            return(r);
        }
Ejemplo n.º 4
0
        void PrintCommandHelp(
            CommandEvaluationContext context,
            CommandSpecification com,
            bool shortView       = false,
            bool list            = false,
            int maxcnamelength   = -1,
            int maxcmdtypelength = -1,
            int maxmodlength     = -1,
            bool singleout       = false)
        {
#pragma warning disable IDE0071                  // Simplifier l’interpolation
#pragma warning disable IDE0071WithoutSuggestion // Simplifier l’interpolation
            if (maxcnamelength == -1)
            {
                maxcnamelength = com.Name.Length + 1;
            }
            if (maxcmdtypelength == -1)
            {
                maxcmdtypelength = com.DeclaringTypeShortName.Length + 1;
            }
            var col = singleout? "": "".PadRight(maxcnamelength, ' ');
            var f   = GetCmd(EchoDirectives.f + "", DefaultForeground.ToString().ToLower());
            if (list)
            {
                if (!shortView)
                {
                    context.Out.Echoln($"{Darkcyan}{com.ModuleName.PadRight(maxmodlength, ' ')}   {com.DeclaringTypeShortName.PadRight(maxcmdtypelength, ' ')}{Tab}{ColorSettings.Highlight}{com.Name.PadRight(maxcnamelength, ' ')}{Tab}{f}{com.Description}{ColorSettings.Default}");
                }
                else
                {
                    context.Out.Echoln($"{ColorSettings.Highlight}{com.Name.PadRight(maxcnamelength, ' ')}{f}{Tab}{com.Description}{ColorSettings.Default}");
                }
            }
            else
            {
                if (singleout)
                {
                    context.Out.Echoln(com.Description);
                    if (com.ParametersCount > 0)
                    {
                        context.Out.Echo($"{Br}{col}{ColorSettings.Label}syntax: {f}{com.ToColorizedString()}{(!shortView ? Br : "")}");
                    }
                    context.Out.Echoln(GetPrintableDocText(com.LongDescription, list, shortView, 0));
                }
                else
                {
                    context.Out.Echoln($"{com.Name.PadRight(maxcnamelength, ' ')}{com.Description}");
                    if (com.ParametersCount > 0)
                    {
                        context.Out.Echo($"{Br}{col}{ColorSettings.Label}syntax: {f}{com.ToColorizedString()}{(!shortView ? Br : "")}");
                    }
                    context.Out.Echo(GetPrintableDocText(com.LongDescription, list, shortView, maxcnamelength));
                }
            }

            if (!list)
            {
                if (com.ParametersCount > 0)
                {
                    if (!shortView)
                    {
                        var mpl = com.ParametersSpecifications.Values.Select(x => x.Dump(false).Length).Max() + TabLength;
                        foreach (var p in com.ParametersSpecifications.Values)
                        {
                            var ptype  = (!p.IsOption && p.HasValue) ? $"of type: {Darkyellow}{p.ParameterInfo.ParameterType.Name}{f}" : "";
                            var pdef   = (p.HasValue && p.IsOptional && p.HasDefaultValue && p.DefaultValue != null && (!p.IsOption || p.ParameterValueTypeName != typeof(bool).Name)) ? ((ptype != ""?". ":"") + $"default value: {Darkyellow}{DumpAsText(p.DefaultValue)}{f}") : "";
                            var supdef = $"{ptype}{pdef}";
                            context.Out.Echoln($"{col}{Tab}{p.ToColorizedString(false)}{"".PadRight(mpl - p.Dump(false).Length, ' ')}{p.Description}");
                            if (!string.IsNullOrWhiteSpace(supdef))
                            {
                                context.Out.Echoln($"{col}{Tab}{" ".PadRight(mpl)}{supdef}");
                            }
                        }

                        if (string.IsNullOrWhiteSpace(com.Documentation))
                        {
                            context.Out.Echoln();
                        }
                        context.Out.Echo(GetPrintableDocText(com.Documentation, list, shortView, singleout ? 0 : maxcnamelength));
                    }
                    else
                    {
                        context.Out.Echoln(GetPrintableDocText(com.Documentation, list, shortView, singleout ? 0 : maxcnamelength));
                    }
                }
                if (!shortView)
                {
                    context.Out.Echoln($"{col}{ColorSettings.Label}type  : {ColorSettings.DarkLabel}{com.DeclaringTypeShortName}");
                    context.Out.Echoln($"{col}{ColorSettings.Label}module: {ColorSettings.DarkLabel}{com.ModuleName}{ColorSettings.Default}");
                }
            }
#pragma warning restore IDE0071WithoutSuggestion // Simplifier l’interpolation
#pragma warning restore IDE0071                  // Simplifier l’interpolation
        }
Ejemplo n.º 5
0
        public void Echo(EchoEvaluationContext ctx)
        {
            var(@out, context, opts) = ctx;
            if (context.EchoMap.MappedCall(this, ctx))
            {
                return;
            }

            var options = opts as FileSystemPathFormattingOptions;

            options ??= (FileSystemPathFormattingOptions)
            context.ShellEnv.GetValue <FileSystemPathFormattingOptions>(ShellEnvironmentVar.display_fileSystemPathFormattingOptions)
            .InitFrom(opts);

            if (options.IsRawModeEnabled)
            {
                var rs = options.ShortPath ? FileSystemInfo.Name : UnescapePathSeparators(FileSystemInfo.FullName);
                var q  = rs.Contains(' ') ? "\"" : "";
                rs = q + rs + q;
                @out.Echo(rs, options.LineBreak, options.IsRawModeEnabled);
                return;
            }

            var bg    = GetCmd(EchoDirectives.b + "", DefaultBackground.ToString().ToLower());
            var fg    = GetCmd(EchoDirectives.f + "", DefaultForeground.ToString().ToLower());
            var color = (IsDirectory) ? NormalDirectoryColorization : FileColorization;

            if (!IsSystem && IsDirectory && !IsReadOnly)
            {
                color += WritableDirectoryColorization;
            }
            if (IsSystem && !IsDirectory)
            {
                color += SystemColorization + bg;
            }
            if (IsSystem && IsDirectory && !IsReadOnly)
            {
                color += SystemWritableDirectoryColorization;
            }
            if (IsFile && IsReadOnly)
            {
                color += ReadOnlyFileColorization;
            }
            var    endcolor = bg + fg + ANSI.RSTXTA;
            var    r        = "";
            var    attr     = "";
            string hidden   = "";

            if (options.PrintAttributes)
            {
                var dir = IsDirectory ? "d" : "-";
                var ro  = IsReadOnly ? "r-" : "rw";
                var sys = IsSystem ? "s" : "-";
                var h   = IsHidden ? "h" : "-";
                //var c = IsCompressed ? "c" : "-";
                var a    = IsArchive ? "a" : "-";
                var size = (IsDirectory || FileSystemInfo == null) ? "" : HumanFormatOfSize(((FileInfo)FileSystemInfo).Length, 2);
                hidden = IsHidden ? "*" : "";
                string smoddat = "";
                if (FileSystemInfo != null)
                {
                    var moddat = FileSystemInfo.LastWriteTime;
                    var dat    = (moddat.Year != System.DateTime.Now.Year) ? moddat.Year + "" : "";
                    smoddat = $"{dat,4} {moddat.ToString("MMM", CultureInfo.InvariantCulture),-3} {moddat.Day,-2} {moddat.Hour.ToString().PadLeft(2, '0')}:{moddat.Minute.ToString().PadLeft(2, '0')}";
                }
                attr = $" {dir}{ro}{sys}{h}{a} {size,10} {smoddat}  ";
            }
            var name  = options.ShortPath ? FileSystemInfo.Name : UnescapePathSeparators(FileSystemInfo.FullName);
            var quote = name.Contains(' ') ? "\"" : "";
            var pdr   = options.PaddingRight - name.Length;

            if (!string.IsNullOrWhiteSpace(quote))
            {
                pdr -= 2;
            }
            var rightspace = (options.PaddingRight > -1) ? endcolor + "".PadRight(pdr > 0 ? pdr : 1, ' ') : "";

            r += $"(rsf){options.LinePrefix}{attr}{color}{options.Prefix}{quote}{name}{quote}{hidden}{rightspace}{options.Postfix}";
            @out.Echo(r + context.ShellEnv.Colors.Default);
            if (HasError)
            {
                @out.Echo($" {ErrorColorization}{GetError()}");
            }
            @out.Echo(ANSI.RSTXTA); // @TODO: convention - si modif des couleurs uniquement ?? ou est-ce un hack pour la fin de ligne ?? a pour contrat de resetter f et b + unset text decoration
        }