如何在按列列出的网格中编写按字典顺序排序的列表? [英] How to write a list sorted lexicographically in a grid listed by column?

查看:17
本文介绍了如何在按列列出的网格中编写按字典顺序排序的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Get-ChildItem 的结果,我想遍历这些结果,并显示它们的名称.默认情况下,如果我只是使用 Write-Host 那么我会像这样沿着行列出它:

PerfLogs Program Files Program Files (x86) Python31 Temp 用户 Windows

但是,假设我知道我希望将其拆分为 x 列,我希望输出如下所示:

PerfLogs Python31 Windows程序文件温度程序文件 (x86) 用户

如您所见,它首先在列中列出,然后再跨列列出.

知道如何获得这样的输出吗?理想情况下,它会使用最多的列数以适应屏幕,名称在每列的左侧对齐.

更新:感谢 Roman,我现在可以使用带有目录颜色的 linux 样式 'ls' 输出.建立他更新的脚本我有:

function color-ls{目录 $args |格式-高-打印{$item = $args$fore = $host.UI.RawUI.ForegroundColor$host.UI.RawUI.ForegroundColor = .{if ($item[1].psIsContainer) {'Blue'}elseif ($item[1].Extension -match '.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)') {'Red'}elseif ($item[1].Extension -match '.(zip|tar|gz|rar)') {'黄色'}elseif ($item[1].Extension -match '.(py|pl|cs|rb|h|cpp)') {'Cyan'}elseif ($item[1].Extension -match '.(txt|cfg|conf|ini|csv|log|xml)') {'Green'}否则 {$fore}}写主机 $args[0] -NoNewLine$host.UI.RawUI.ForegroundColor = $fore}}

输出:

http://dl.dropbox.com/u/2809/lscolor.png

解决方案

这是一个有趣的想法和任务.

更新:更新后的脚本包含一些修复和改进.它还允许以多种方式自定义输出.请参阅脚本注释中的示例.

脚本格式-High.ps1:

<预><代码><#.概要使用最大合适的列号按列格式化输入..描述Format-High 打印指定的属性、表达式或字符串按列填充表的输入对象的表示.它的命名与按行打印的 Format-Wide 形成对比..例子# 只是项目ls c:windows |格式-高# 同样基于 PSIContainer 的颜色ls c:windows |格式-高-打印 {$c = if ($args[1].PSIsContainer) {'yellow'} else {'white'};写主机 $args[0] -ForegroundColor $c -NoNewline}# 只是过程,不好ps |格式-高# 进程名称,好多了ps |格式-高名# 自定义表达式和宽度ps |格式-高 {$_.Name + ':' + $_.WS} 70# 基于工作集的颜色处理名称ps |格式-高名 70 {$c = if ($args[1].WS -gt 10mb) {'red'} else {'green'};写主机 $args[0] -ForegroundColor $c -NoNewline}#>参数([对象]$属性,[int]$Width = $Host.UI.RawUI.WindowSize.Width - 1,[scriptblock]$Print = { Write-Host $args[0] -NoNewline },[对象[]]$输入对象)# 处理输入,获取要格式化的字符串if ($InputObject -eq $null) { $InputObject = @($input) }if ($Property -is [string]) { $strings = $InputObject |选择对象 -ExpandProperty $Property }elseif ($Property -is [scriptblock]) { $strings = $InputObject |ForEach-Object $Property }else { $strings = $InputObject }$strings = @(foreach($_ in $strings) { "$_" })# pass 1: 找到最大的列数$nbest = 1$bestwidths = @($Width)for($ncolumn = 2; ;++$ncolumn) {$nrow = [Math]::Ceiling($strings.Count/$ncolumn)$宽度 = @(for($s = 0; $s -lt $strings.Count; $s += $nrow) {$e = [Math]::Min($strings.Count, $s + $nrow)($strings[$s .. ($e - 1)] | Measure-Object -Maximum Length).Maximum + 1})if (($widths | Measure-Object -Sum).Sum -gt $Width) {休息}$bestwidths = $widths$nbest = $ncolumn如果($nrow -le 1){休息}}# pass 2: 打印字符串$nrow = [Math]::Ceiling($strings.Count/$nbest)for($r = 0; $r -lt $nrow;++$r) {for($c = 0; $c -lt $nbest;++$c) {$i = $c * $nrow + $r如果($i -lt $strings.Count){&$Print ($strings[$i].PadRight($bestwidths[$c])) $InputObject[$i]}}&$打印`r`n"}

I have the result of Get-ChildItem, and I want to iterate over these, and display their names. By default if I simply use Write-Host then I get it listed out along the row like this:

PerfLogs  Program Files  Program Files (x86)  Python31  Temp  Users  Windows

However, say that I know I want it split into x columns, I want the output like this instead:

PerfLogs                  Python31     Windows
Program Files             Temp
Program Files (x86)       Users

As you can see, it lists it down the columns first, and then across.

Any idea how to get output like that? Ideally it would use the most # of columns as can fit on screen with the Name aligned to the left in each column.

UPDATE: thanks to Roman, I can now have my linux style 'ls' output with directory colors. Building off his updated script I have:

function color-ls
{
    dir $args | Format-High -Print {    
        $item = $args
        $fore = $host.UI.RawUI.ForegroundColor        
        $host.UI.RawUI.ForegroundColor = .{     
            if ($item[1].psIsContainer) {'Blue'}
            elseif ($item[1].Extension -match '.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)') {'Red'}
            elseif ($item[1].Extension -match '.(zip|tar|gz|rar)') {'Yellow'}
            elseif ($item[1].Extension -match '.(py|pl|cs|rb|h|cpp)') {'Cyan'}
            elseif ($item[1].Extension -match '.(txt|cfg|conf|ini|csv|log|xml)') {'Green'}
            else {$fore}
        }
        write-host $args[0] -NoNewLine
        $host.UI.RawUI.ForegroundColor = $fore
    }
}

Output:

http://dl.dropbox.com/u/2809/lscolor.png

解决方案

It’s an interesting idea and task.

UPDATE: the updated script contains a few fixes and improvements. It also allows to customize the output in several ways. See examples in the script comments.

Script Format-High.ps1:

<#
.SYNOPSIS
    Formats input by columns using maximum suitable column number.

.DESCRIPTION
    Format-High prints the specified property, expression, or string
    representation of input objects filling the table by columns.

    It is named in contrast to Format-Wide which prints by rows.

.EXAMPLE
    # just items
    ls c:windows | Format-High

    # ditto in colors based on PSIsContainer
    ls c:windows | Format-High -Print {$c = if ($args[1].PSIsContainer) {'yellow'} else {'white'}; Write-Host $args[0] -ForegroundColor $c -NoNewline}

    # just processes, not good
    ps | Format-High

    # process names, much better
    ps | Format-High Name

    # custom expression and width
    ps | Format-High {$_.Name + ':' + $_.WS} 70

    # process names in colors based on working sets
    ps | Format-High Name 70 {$c = if ($args[1].WS -gt 10mb) {'red'} else {'green'}; Write-Host $args[0] -ForegroundColor $c -NoNewline}
#>

param
(
    [object]$Property,
    [int]$Width = $Host.UI.RawUI.WindowSize.Width - 1,
    [scriptblock]$Print = { Write-Host $args[0] -NoNewline },
    [object[]]$InputObject
)

# process the input, get strings to format
if ($InputObject -eq $null) { $InputObject = @($input) }
if ($Property -is [string]) { $strings = $InputObject | Select-Object -ExpandProperty $Property }
elseif ($Property -is [scriptblock]) { $strings = $InputObject | ForEach-Object $Property }
else { $strings = $InputObject }
$strings = @(foreach($_ in $strings) { "$_" })

# pass 1: find the maximum column number
$nbest = 1
$bestwidths = @($Width)
for($ncolumn = 2; ; ++$ncolumn) {
    $nrow = [Math]::Ceiling($strings.Count / $ncolumn)
    $widths = @(
        for($s = 0; $s -lt $strings.Count; $s += $nrow) {
            $e = [Math]::Min($strings.Count, $s + $nrow)
            ($strings[$s .. ($e - 1)] | Measure-Object -Maximum Length).Maximum + 1
        }
    )
    if (($widths | Measure-Object -Sum).Sum -gt $Width) {
        break
    }
    $bestwidths = $widths
    $nbest = $ncolumn
    if ($nrow -le 1) {
        break
    }
}

# pass 2: print strings
$nrow = [Math]::Ceiling($strings.Count / $nbest)
for($r = 0; $r -lt $nrow; ++$r) {
    for($c = 0; $c -lt $nbest; ++$c) {
        $i = $c * $nrow + $r
        if ($i -lt $strings.Count) {
            & $Print ($strings[$i].PadRight($bestwidths[$c])) $InputObject[$i]
        }
    }
    & $Print "`r`n"
}

这篇关于如何在按列列出的网格中编写按字典顺序排序的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆