Ejemplo n.º 1
0
        private static SearchResultComparer GetComparer(
            SortProperty property,
            PathDisplayStyle pathDisplayStyle,
            bool cultureInvariant)
        {
            switch (property)
            {
            case SortProperty.Name:
                if (pathDisplayStyle == PathDisplayStyle.Match)
                {
                    return((cultureInvariant) ? SearchResultComparer.Match_CultureInvariant : SearchResultComparer.Match);
                }
                else
                {
                    return((cultureInvariant) ? SearchResultComparer.Name_CultureInvariant : SearchResultComparer.Name);
                }

            case SortProperty.CreationTime:
                return(SearchResultComparer.CreationTime);

            case SortProperty.ModifiedTime:
                return(SearchResultComparer.ModifiedTime);

            case SortProperty.Size:
                return(SearchResultComparer.Size);

            default:
                throw new ArgumentException($"Unknown enum value '{property}'.", nameof(property));
            }
        }
Ejemplo n.º 2
0
 private static SearchResultComparer GetComparer(SortProperty property, PathDisplayStyle pathDisplayStyle)
 {
     return(property switch
     {
         SortProperty.Name => (pathDisplayStyle == PathDisplayStyle.Match) ? SearchResultComparer.Match : SearchResultComparer.Name,
         SortProperty.CreationTime => SearchResultComparer.CreationTime,
         SortProperty.ModifiedTime => SearchResultComparer.ModifiedTime,
         SortProperty.Size => SearchResultComparer.Size,
         _ => throw new ArgumentException($"Unknown enum value '{property}'.", nameof(property)),
     });
Ejemplo n.º 3
0
        public bool TryParse(FindCommandOptions options)
        {
            var baseOptions = (CommonFindCommandOptions)options;

            if (!TryParse(baseOptions))
            {
                return(false);
            }

            options = (FindCommandOptions)baseOptions;

            if (!TryParseModifyOptions(Modify, OptionNames.Modify, out ModifyOptions? modifyOptions, out bool aggregateOnly))
            {
                return(false);
            }

            OutputDisplayFormat format = options.Format;
            ContentDisplayStyle contentDisplayStyle = format.ContentDisplayStyle;
            PathDisplayStyle    pathDisplayStyle    = format.PathDisplayStyle;

            if (modifyOptions.HasAnyFunction &&
                contentDisplayStyle == ContentDisplayStyle.ValueDetail)
            {
                contentDisplayStyle = ContentDisplayStyle.Value;
            }

            if (aggregateOnly)
            {
                ConsoleOut.Verbosity = Orang.Verbosity.Minimal;
                pathDisplayStyle     = PathDisplayStyle.Omit;
            }

            options.ModifyOptions = modifyOptions;

            options.Format = new OutputDisplayFormat(
                contentDisplayStyle: contentDisplayStyle,
                pathDisplayStyle: pathDisplayStyle,
                lineOptions: format.LineOptions,
                lineContext: format.LineContext,
                displayParts: format.DisplayParts,
                fileProperties: format.FileProperties,
                indent: format.Indent,
                separator: format.Separator,
                includeBaseDirectory: format.IncludeBaseDirectory);

            return(true);
        }
Ejemplo n.º 4
0
        public static IEnumerable <SearchResult> SortResults(
            List <SearchResult> results,
            SortOptions sortOptions,
            PathDisplayStyle pathDisplayStyle)
        {
            ImmutableArray <SortDescriptor> descriptors = sortOptions.Descriptors;

            if (!descriptors.Any())
            {
                return(results);
            }

            IComparer <SearchResult> comparer = GetComparer(descriptors[0].Property, pathDisplayStyle, sortOptions.CultureInvariant);

            if (descriptors.Length == 1)
            {
                if (descriptors[0].Direction == SortDirection.Ascending)
                {
                    results.Sort(comparer);
                }
                else
                {
                    results.Sort((x, y) => - comparer.Compare(x, y));
                }

                return(results);
            }
            else
            {
                IOrderedEnumerable <SearchResult> sorted = (descriptors[0].Direction == SortDirection.Ascending)
                    ? results.OrderBy(f => f, comparer)
                    : results.OrderByDescending(f => f, comparer);

                for (int i = 1; i < descriptors.Length; i++)
                {
                    comparer = GetComparer(descriptors[i].Property, pathDisplayStyle, sortOptions.CultureInvariant);

                    sorted = (descriptors[i].Direction == SortDirection.Ascending)
                        ? sorted.ThenBy(f => f, comparer)
                        : sorted.ThenByDescending(f => f, comparer);
                }

                return(sorted);
            }
        }
Ejemplo n.º 5
0
 public OutputDisplayFormat(
     ContentDisplayStyle contentDisplayStyle,
     PathDisplayStyle pathDisplayStyle         = PathDisplayStyle.Full,
     LineDisplayOptions lineOptions            = LineDisplayOptions.None,
     DisplayParts displayParts                 = DisplayParts.None,
     IEnumerable <FileProperty> fileProperties = null,
     string indent             = null,
     string separator          = null,
     bool includeBaseDirectory = false)
 {
     ContentDisplayStyle = contentDisplayStyle;
     PathDisplayStyle    = pathDisplayStyle;
     LineOptions         = lineOptions;
     DisplayParts        = displayParts;
     FileProperties      = fileProperties?.ToImmutableArray() ?? ImmutableArray <FileProperty> .Empty;
     Indent               = indent ?? DefaultIndent;
     Separator            = separator ?? Environment.NewLine;
     IncludeBaseDirectory = includeBaseDirectory;
 }
Ejemplo n.º 6
0
 public OutputDisplayFormat(
     ContentDisplayStyle contentDisplayStyle,
     PathDisplayStyle pathDisplayStyle         = PathDisplayStyle.Full,
     LineDisplayOptions lineOptions            = LineDisplayOptions.None,
     LineContext lineContext                   = default,
     DisplayParts displayParts                 = DisplayParts.None,
     IEnumerable <FileProperty>?fileProperties = null,
     string?indent     = null,
     string?separator  = null,
     bool alignColumns = true)
 {
     ContentDisplayStyle = contentDisplayStyle;
     PathDisplayStyle    = pathDisplayStyle;
     LineOptions         = lineOptions;
     LineContext         = lineContext;
     DisplayParts        = displayParts;
     FileProperties      = fileProperties?.ToImmutableArray() ?? ImmutableArray <FileProperty> .Empty;
     Indent       = indent ?? DefaultIndent;
     Separator    = separator;
     AlignColumns = alignColumns;
 }
Ejemplo n.º 7
0
        private void ExecuteResults(SearchContext context)
        {
            IEnumerable <SearchResult> results = context.Results !;
            SortOptions?sortOptions            = Options.SortOptions;

            if (sortOptions?.Descriptors.Any() == true)
            {
                PathDisplayStyle pathDisplayStyle = Options.PathDisplayStyle;

                if (pathDisplayStyle == PathDisplayStyle.Match &&
                    NameFilter == null)
                {
                    pathDisplayStyle = PathDisplayStyle.Full;
                }

                results = SortHelpers.SortResults(context.Results !, sortOptions, pathDisplayStyle);

                if (sortOptions.MaxCount > 0)
                {
                    results = results.Take(sortOptions.MaxCount);
                }
            }

            ImmutableArray <FileProperty> fileProperties = Options.Format.FileProperties;
            ColumnWidths?columnWidths = null;

            if (fileProperties.Any() &&
                Options.Format.AlignColumns)
            {
                List <SearchResult> resultList = results.ToList();

                int maxNameWidth = resultList.Max(f => f.Path.Length);
                int maxSizeWidth = 0;

                if (fileProperties.Contains(FileProperty.Size))
                {
                    long maxSize = 0;

                    foreach (SearchResult result in resultList)
                    {
                        long size = result.GetSize();

                        if (result.IsDirectory)
                        {
                            if (context.DirectorySizeMap == null)
                            {
                                context.DirectorySizeMap = new Dictionary <string, long>();
                            }

                            context.DirectorySizeMap[result.Path] = size;
                        }

                        if (size > maxSize)
                        {
                            maxSize = size;
                        }
                    }

                    maxSizeWidth = maxSize.ToString("n0").Length;
                }

                columnWidths = new ColumnWidths(maxNameWidth, maxSizeWidth);

                results = resultList;
            }

            int i = 0;

            try
            {
                foreach (SearchResult result in results)
                {
                    ExecuteResult(result, context, columnWidths);
                    i++;

                    if (context.TerminationReason == TerminationReason.Canceled)
                    {
                        break;
                    }

                    context.CancellationToken.ThrowIfCancellationRequested();
                }
            }
            catch (OperationCanceledException)
            {
                context.TerminationReason = TerminationReason.Canceled;
            }

            if (context.TerminationReason == TerminationReason.Canceled ||
                context.CancellationToken.IsCancellationRequested)
            {
                OperationCanceled();
            }

            if (Options.Format.FileProperties.Contains(FileProperty.Size) &&
                context.Telemetry.FilesTotalSize == 0)
            {
                foreach (SearchResult result in results.Take(i))
                {
                    context.Telemetry.FilesTotalSize += result.GetSize();
                }
            }
        }
Ejemplo n.º 8
0
        public bool TryParse(FindCommandOptions options)
        {
            if (!TryParseAsEnum(
                    Pipe,
                    OptionNames.Pipe,
                    out PipeMode pipeMode,
                    PipeMode.None,
                    OptionValueProviders.PipeMode))
            {
                return(false);
            }

            if (pipeMode == PipeMode.None)
            {
                if (Console.IsInputRedirected)
                {
                    PipeMode = PipeMode.Text;
                }
            }
            else
            {
                if (!Console.IsInputRedirected)
                {
                    WriteError("Redirected/piped input is required "
                               + $"when option '{OptionNames.GetHelpText(OptionNames.Pipe)}' is specified.");

                    return(false);
                }

                PipeMode = pipeMode;
            }

            var baseOptions = (CommonFindCommandOptions)options;

            if (!TryParse(baseOptions))
            {
                return(false);
            }

            options = (FindCommandOptions)baseOptions;

            if (!TryParseProperties(Ask, Name, options))
            {
                return(false);
            }

            string?input = null;

            if (pipeMode != PipeMode.Paths &&
                Console.IsInputRedirected)
            {
                if (options.ContentFilter == null)
                {
                    WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Content)}' is required "
                               + "when redirected/piped input is used as a text to be searched.");

                    return(false);
                }

                input = ConsoleHelpers.ReadRedirectedInput();
            }

            EnumerableModifier <string>?modifier = null;

#if DEBUG // --modifier
            if (Modifier.Any() &&
                !TryParseModifier(Modifier, OptionNames.Modifier, out modifier))
            {
                return(false);
            }
#endif
            if (!TryParseModifyOptions(
                    Modify,
                    OptionNames.Modify,
                    modifier,
                    out ModifyOptions? modifyOptions,
                    out bool aggregateOnly))
            {
                return(false);
            }

            OutputDisplayFormat format = options.Format;
            ContentDisplayStyle contentDisplayStyle = format.ContentDisplayStyle;
            PathDisplayStyle    pathDisplayStyle    = format.PathDisplayStyle;

            if (modifyOptions.HasAnyFunction &&
                contentDisplayStyle == ContentDisplayStyle.ValueDetail)
            {
                contentDisplayStyle = ContentDisplayStyle.Value;
            }

            options.Input         = input;
            options.ModifyOptions = modifyOptions;
            options.AggregateOnly = aggregateOnly;
            options.Split         = Split;

            options.Format = new OutputDisplayFormat(
                contentDisplayStyle: contentDisplayStyle,
                pathDisplayStyle: pathDisplayStyle,
                lineOptions: format.LineOptions,
                lineContext: format.LineContext,
                displayParts: format.DisplayParts,
                fileProperties: format.FileProperties,
                indent: format.Indent,
                separator: format.Separator,
                alignColumns: format.AlignColumns,
                includeBaseDirectory: format.IncludeBaseDirectory);

            return(true);
        }