Last active
April 29, 2022 09:31
-
-
Save roh85/39dffdc2870fc2f5ffd66b853d1eedb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compatibility with PS major versions <= 2 | |
if(!$PSScriptRoot) { | |
$PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path | |
} | |
# Add Cmder modules directory to the autoload path. | |
$CmderModulePath = Join-path $PSScriptRoot "psmodules/" | |
if( -not $env:PSModulePath.Contains($CmderModulePath) ){ | |
$env:PSModulePath = $env:PSModulePath.Insert(0, "$CmderModulePath;") | |
} | |
try { | |
# Check if git is on PATH, i.e. Git already installed on system | |
Get-command -Name "git" -ErrorAction Stop >$null | |
} catch { | |
$env:Path += ";$env:CMDER_ROOT\vendor\git-for-windows\bin" | |
} | |
try { | |
Import-Module -Name "posh-git" -ErrorAction Stop >$null | |
$gitStatus = $true | |
} catch { | |
Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart cmder." | |
$gitStatus = $false | |
} | |
function checkGit($Path) { | |
if (Test-Path -Path (Join-Path $Path '.git/') ) { | |
Write-VcsStatus | |
return | |
} | |
$SplitPath = split-path $path | |
if ($SplitPath) { | |
checkGit($SplitPath) | |
} | |
} | |
# Set up a Cmder prompt, adding the git prompt parts inside git repos | |
function global:prompt { | |
$realLASTEXITCODE = $LASTEXITCODE | |
$Host.UI.RawUI.ForegroundColor = "White" | |
Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green | |
if($gitStatus){ | |
checkGit($pwd.ProviderPath) | |
} | |
$global:LASTEXITCODE = $realLASTEXITCODE | |
Write-Host "`nλ" -NoNewLine -ForegroundColor "DarkGray" | |
return " " | |
} | |
# Load special features come from posh-git | |
if ($gitStatus) { | |
Start-SshAgent -Quiet | |
} | |
# Move to the wanted location | |
if (Test-Path Env:\CMDER_START) { | |
Set-Location -Path $Env:CMDER_START | |
} elseif ($Env:CMDER_ROOT -and $Env:CMDER_ROOT.StartsWith($pwd)) { | |
Set-Location -Path $Env:USERPROFILE | |
} | |
# Enhance Path | |
$env:Path = "$Env:CMDER_ROOT\bin;$env:Path;$Env:CMDER_ROOT" | |
function Internal-Change-Directory($cmd, $ShowCount){ | |
if(!$ShowCount) { | |
$ShowCount = 10; | |
} | |
if(!$global:CD_COMMAND_HISTORY_LIST) { | |
$global:CD_COMMAND_HISTORY_LIST = New-Object 'System.Collections.Generic.List[string]' | |
} | |
function Set-CDLocation($newLocation) { | |
$newLocation = get-item $newLocation; | |
$global:CD_COMMAND_HISTORY_LIST.Add($newLocation) | |
Push-Location $newLocation | |
} | |
function Get-CommandList() { | |
$global:CD_COMMAND_HISTORY_LIST | sort -Unique | |
} | |
function Print-Extended-CD-Menu() { | |
$index = 1; | |
foreach($location in Get-CommandList){ | |
Write-Host ("{0,6}) {1}" -f $index, $location) | |
$index++ | |
if($index -gt $ShowCount){ | |
break; | |
} | |
} | |
} | |
switch($cmd) { | |
"" { Print-Extended-CD-Menu } | |
"?" { Print-Extended-CD-Menu } | |
"--help" { Print-Extended-CD-Menu } | |
"-" { | |
if($global:CD_COMMAND_HISTORY_LIST.Count -ge 2) { | |
Set-CDLocation ($global:CD_COMMAND_HISTORY_LIST[$global:CD_COMMAND_HISTORY_LIST.Count-2]) | |
} | |
} | |
default { | |
$newLocation = $cmd; | |
# Turn "cd ...." into "cd ../../../" | |
if ($newLocation -match "^\.*$" -and $newLocation.length -gt 2) { | |
$numberToChange = $newLocation.length - 1 | |
$newLocation = "" | |
for($i = 0; $i -lt $numberToChange; $i++) { | |
$newLocation += "../" | |
} | |
} | |
# check to see if we're using a number command and get the correct directory. | |
[int]$cdIndex = 0; | |
if([system.int32]::TryParse($cmd, [ref]$cdIndex)) { | |
# Don't pull from the history if the index value is the same as a folder name | |
if( !(test-path $cdIndex) ) { | |
$results = (Get-CommandList); | |
if( ($results | measure).Count -eq 1 ){ | |
$newLocation = $results | |
} | |
else { | |
$newLocation = $results[$cdIndex-1] | |
} | |
} | |
} | |
#If we are actually changing the dir. | |
if($pwd.Path -ne $newLocation){ | |
# if the path exists | |
if( test-path $newLocation ){ | |
# if it's a file - get the file's directory. | |
if( !(Get-Item $newLocation -Force).PSIsContainer ) { | |
$newLocation = (split-path $newLocation) | |
} | |
Set-CDLocation $newLocation | |
} | |
else { | |
if($force) { | |
$prompt = 'y' | |
} | |
else { | |
$prompt = Read-Host "Folder not found. Create it? [y/n]" | |
} | |
if($prompt -eq 'y' -or $prompt -eq 'yes') { | |
mkdir $newLocation | |
Set-CDLocation $newLocation | |
} | |
} | |
} | |
} | |
} | |
} | |
function Get-ChildItem-Color { | |
if ($Args[0] -eq $true) { | |
$ifwide = $true | |
if ($Args.Length -gt 1) { | |
$Args = $Args[1..($Args.length - 1)] | |
} else { | |
$Args = @() | |
} | |
} else { | |
$ifwide = $false | |
} | |
if (($Args[0] -eq "-a") -or ($Args[0] -eq "--all")) { | |
$Args[0] = "-Force" | |
} | |
$width = $host.UI.RawUI.WindowSize.Width | |
$items = Invoke-Expression "Get-ChildItem `"$Args`""; | |
$lnStr = $items | select-object Name | sort-object { "$_".length } -descending | select-object -first 1 | |
$len = $lnStr.name.length | |
$cols = If ($len) {($width+1)/($len+2)} Else {1}; | |
$cols = [math]::floor($cols); | |
if(!$cols){ $cols=1;} | |
$color_fore = $Host.UI.RawUI.ForegroundColor | |
$compressed_list = @(".7z", ".gz", ".rar", ".tar", ".zip") | |
$executable_list = @(".exe", ".bat", ".cmd", ".py", ".pl", ".ps1", | |
".psm1", ".vbs", ".rb", ".reg", ".fsx") | |
$dll_pdb_list = @(".dll", ".pdb") | |
$text_files_list = @(".csv", ".lg", "markdown", ".rst", ".txt") | |
$configs_list = @(".cfg", ".config", ".conf", ".ini") | |
$color_table = @{} | |
foreach ($Extension in $compressed_list) { | |
$color_table[$Extension] = "Yellow" | |
} | |
foreach ($Extension in $executable_list) { | |
$color_table[$Extension] = "Blue" | |
} | |
foreach ($Extension in $text_files_list) { | |
$color_table[$Extension] = "Cyan" | |
} | |
foreach ($Extension in $dll_pdb_list) { | |
$color_table[$Extension] = "Darkgreen" | |
} | |
foreach ($Extension in $configs_list) { | |
$color_table[$Extension] = "DarkYellow" | |
} | |
$i = 0 | |
$pad = [math]::ceiling(($width+2) / $cols) - 3 | |
$nnl = $false | |
$items | | |
%{ | |
if ($_.GetType().Name -eq 'DirectoryInfo') { | |
$c = 'Red' | |
$length = "" | |
} else { | |
$c = $color_table[$_.Extension] | |
if ($c -eq $none) { | |
$c = $color_fore | |
} | |
$length = $_.length | |
} | |
# get the directory name | |
if ($_.GetType().Name -eq "FileInfo") { | |
$DirectoryName = $_.DirectoryName | |
} elseif ($_.GetType().Name -eq "DirectoryInfo") { | |
$DirectoryName = $_.Parent.FullName | |
} | |
if ($ifwide) { # Wide (ls) | |
if ($LastDirectoryName -ne $DirectoryName) { # change this to `$LastDirectoryName -ne $DirectoryName` to show DirectoryName | |
if($i -ne 0 -AND $host.ui.rawui.CursorPosition.X -ne 0){ # conditionally add an empty line | |
write-host "" | |
} | |
Write-Host -Fore $color_fore ("`n Directory: $DirectoryName`n") | |
} | |
$nnl = ++$i % $cols -ne 0 | |
# truncate the item name | |
$towrite = $_.Name | |
if ($towrite.length -gt $pad) { | |
$towrite = $towrite.Substring(0, $pad - 3) + "..." | |
} | |
Write-Host ("{0,-$pad}" -f $towrite) -Fore $c -NoNewLine:$nnl | |
if($nnl){ | |
write-host " " -NoNewLine | |
} | |
} else { | |
If ($LastDirectoryName -ne $DirectoryName) { # first item - print out the header | |
Write-Host "`n Directory: $DirectoryName`n" | |
Write-Host "Mode LastWriteTime Length Name" | |
Write-Host "---- ------------- ------ ----" | |
} | |
$Host.UI.RawUI.ForegroundColor = $c | |
Write-Host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode, | |
([String]::Format("{0,10} {1,8}", | |
$_.LastWriteTime.ToString("d"), | |
$_.LastWriteTime.ToString("t"))), | |
$length, $_.name) | |
$Host.UI.RawUI.ForegroundColor = $color_fore | |
++$i # increase the counter | |
} | |
$LastDirectoryName = $DirectoryName | |
} | |
if ($nnl) { # conditionally add an empty line | |
Write-Host "" | |
} | |
} | |
function Get-ChildItem-Format-Wide { | |
$New_Args = @($true) | |
$New_Args += "$Args" | |
Invoke-Expression "Get-ChildItem-Color $New_Args" | |
} | |
function NuGetClearAll { | |
Invoke-Expression "nuget locals all -clear" | |
} | |
function RestoreAndBuild { | |
[CmdletBinding()] | |
Param($arg) | |
Invoke-Expression "Nuget.exe restore $arg" | |
Invoke-Expression "MSBuild.exe $arg" | |
} | |
function GitClean { | |
Invoke-Expression "git clean -d -f -x" | |
} | |
function DockerStopAll{ | |
Invoke-Expression "docker stop $(docker ps -aq)" | |
} | |
function DockerRemoveAllContainers{ | |
Invoke-Expression "docker rm $(docker ps -aq)" | |
} | |
function DockerRemoveAllImages{ | |
Invoke-Expression "docker rmi $(docker images -q)" | |
} | |
function DockerStopAndRemoveAllContainers{ | |
Invoke-Expression "docker stop $(docker ps -aq)" | |
Invoke-Expression "docker rm $(docker ps -aq)" | |
} | |
function DockerCleanAll{ | |
Invoke-Expression "docker stop $(docker ps -aq)" | |
Invoke-Expression "docker rm $(docker ps -aq)" | |
Invoke-Expression "docker rmi $(docker images -q)" | |
} | |
Set-Alias l Get-ChildItem-Color -option AllScope | |
Set-Alias ls Get-ChildItem-Format-Wide -option AllScope | |
Set-Alias nugetclear NuGetClearAll | |
Set-Alias open start | |
Set-Alias rb RestoreAndBuild | |
Set-Alias clean GitClean | |
Set-Alias docker-stop DockerStopAll | |
Set-Alias docker-rm DockerRemoveAllContainers | |
Set-Alias docker-rmi DockerRemoveAllImages | |
Set-Alias docker-srm DockerStopAndRemoveAllContainers | |
Set-Alias docker-srmi DockerCleanAll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment