Skip to content

Instantly share code, notes, and snippets.

@matchy233
Last active March 22, 2024 23:51
Show Gist options
  • Save matchy233/0c8baaad8af9aa12838ded428d6f8e31 to your computer and use it in GitHub Desktop.
Save matchy233/0c8baaad8af9aa12838ded428d6f8e31 to your computer and use it in GitHub Desktop.
Bash-like ls for PowerShell
function ls_custom {
param([switch]$a, [switch]$l, [switch]$la, [switch]$al, [switch]$h, [switch]$lah)
if ($la -or $al) {
$a = $true
$l = $true
}
if ($lah) {
$a = $true
$l = $true
$h = $true
}
$regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)
$hidden = New-Object System.Text.RegularExpressions.Regex('^\.', $regex_opts)
$compressed = New-Object System.Text.RegularExpressions.Regex('\.(zip|tar|gz|rar)$', $regex_opts)
$executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib|py|sh)$', $regex_opts)
$width = $Host.UI.RawUI.WindowSize.Width
$cols = 5
if ($width -le 82) {
$cols = 3
}
if ($l) {
write-output "`n Directory: $((Get-Location).Path)`n"
$format_title = "{0} {1, 29} {2,15} {3}"
write-output ($format_title -f "Mode", "LastWriteTime", "Length", "Name") -ForegroundColor Green
write-output ($format_title -f "----", "-------------", "------", "----") -ForegroundColor Green
$getHumanReadableSize = {
param($size)
if ($size -lt 1kb) {
return $size.tostring()
}
elseif ($size -lt 1mb) {
if ($size / 1kb -lt 10) {
return [string]::Format("{0:0.0}K", [math]::Round($size / 1kb, 2))
} else {
return [string]::Format("{0:0}K", [math]::Round($size / 1kb, 2))
}
}
elseif ($size -lt 1gb) {
if ($size / 1mb -lt 10) {
return [string]::Format("{0:0.0}M", [math]::Round($size / 1mb, 2))
} else {
return [string]::Format("{0:0}M", [math]::Round($size / 1mb, 2))
}
}
elseif ($size -lt 1tb) {
if ($size / 1gb -lt 10) {
return [string]::Format("{0:0.0}G", [math]::Round($size / 1gb, 2))
} else {
return [string]::Format("{0:0}G", [math]::Round($size / 1gb, 2))
}
}
else {
if ($size / 1tb -lt 10) {
return [string]::Format("{0:0.0}T", [math]::Round($size / 1tb, 2))
} else {
return [string]::Format("{0:0}T", [math]::Round($size / 1tb, 2))
}
}
}
$WriteFileInfo = {
param($mode, $lwt, $len)
write-output ("{0} {1, 28:dd-MMM-yy hh:mm} {2,15}" -f $mode, $lwt, $len) -NoNewline
}
get-childitem $args | foreach-object {
$out = $_.name
$isHidden = $hidden.IsMatch($_.name)
if ($h) {
$len = $getHumanReadableSize.Invoke($_.length)[0]
}
else {
$len = $_.length
}
if ($_.GetType().Name -eq 'DirectoryInfo') {
if ($isHidden -AND $a) {
&$WriteFileInfo $_.mode $_.lastwritetime " "
write-output (" {0}" -f $_.name) -ForegroundColor DarkBlue
}
elseif ($isHidden) {
# do nothing
}
else {
&$WriteFileInfo $_.mode $_.lastwritetime " "
write-output (" {0}" -f $_.name) -ForegroundColor Blue
}
}
elseif ($isHidden) {
if ($a) {
&$WriteFileInfo $_.mode $_.lastwritetime $len
write-output (" {0}" -f $_.name) -ForegroundColor DarkGray
}
}
elseif ($compressed.IsMatch($_.name)) {
&$WriteFileInfo $_.mode $_.lastwritetime $len
write-output (" {0}" -f $_.name) -ForegroundColor Red
}
elseif ($executable.IsMatch($out)) {
&$WriteFileInfo $_.mode $_.lastwritetime $len
write-output (" {0}" -f $_.name) -ForegroundColor Green
}
else {
&$WriteFileInfo $_.mode $_.lastwritetime $len
write-output (" {0}" -f $_.name)
}
}
}
else {
invoke-expression ("Get-ChildItem $args") |
foreach-object {
$i = 0
$colwid = [int]($width / $cols)
$pad = [int]($width / $cols) - 1
} `
{
$out = $_.Name
$isHidden = $hidden.IsMatch($out)
if ($isHidden -AND ( -NOT $a )) {
$nnl = $i % $cols -ne 0
}
else {
$nnl = ++$i % $cols -ne 0
}
if ($out.Length -ge $colwid - 1) {
$out = "$($out.substring(0, $colwid - 5))..."
}
if ($_.GetType().Name -eq 'DirectoryInfo') {
if ($isHidden -AND $a) {
write-output ("{0,-$pad}" -f $out) -Fore DarkBlue -NoNewLine:$nnl
}
elseif ($isHidden) {
# do nothing
}
else {
write-output ("{0,-$pad}" -f $out) -Fore Blue -NoNewLine:$nnl
}
}
elseif ($isHidden) {
if ($a) {
write-output ("{0,-$pad}" -f $out) -Fore DarkGray -NoNewLine:$nnl
}
}
elseif ($compressed.IsMatch($out)) {
write-output ("{0,-$pad}" -f $out) -Fore Red -NoNewLine:$nnl
}
elseif ($executable.IsMatch($out)) {
write-output ("{0,-$pad}" -f $out) -Fore Green -NoNewLine:$nnl
}
else {
write-output ("{0,-$pad}" -f $out) -NoNewLine:$nnl
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment