...
 
Commits (5)
Function Receive-WebFile {
Param (
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[string[]]$Uri
)
Begin {
$sources=@()
$destinations=@()
}
Process {
ForEach($u in $Uri) {
$resp = Invoke-WebRequest -Method:HEAD -Uri:$u -UseBasicParsing -ErrorAction:Continue
If($null -Ne $resp) {
$filename = Split-Path -Leaf -Path:$u
$cdisp = $resp.Headers["Content-Disposition"]
If(![string]::IsNullOrEmpty($cdisp)) {
$disps = $cdisp -Split ";\s*"
ForEach($d in $disps) {
$pair = $d -Split "\s*=\s*"
If($pair[0] -Eq "filename") {
$filename = $pair[1] -Replace "^`"|`"`$","" # Strip quotes at the beginning and end.
$filename = Split-Path -Leaf -Path:$filename
}
Param (
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[string[]]$Uri
)
Begin {
$sources=@()
$destinations=@()
}
Process {
ForEach($u in $Uri) {
$resp = Invoke-WebRequest -Method:HEAD -Uri:$u -UseBasicParsing -ErrorAction:Continue
If($null -Ne $resp) {
$filename = Split-Path -Leaf -Path:$u
$cdisp = $resp.Headers["Content-Disposition"]
If(![string]::IsNullOrEmpty($cdisp)) {
$disps = $cdisp -Split ";\s*"
ForEach($d in $disps) {
$pair = $d -Split "\s*=\s*"
If($pair[0] -Eq "filename") {
$filename = $pair[1] -Replace "^`"|`"`$","" # Strip quotes at the beginning and end.
$filename = Split-Path -Leaf -Path:$filename
}
}
$sources += $u
$destinations += $filename
}
$sources += $u
$destinations += $filename
}
}
End {
If ($sources.Length -Gt 0) {
Start-BitsTransfer -Source:$sources -Destination:$destinations -TransferType:Download
} Else {
Write-Warning "No files to receive."
}
}
End {
If ($sources.Length -Gt 0) {
Start-BitsTransfer -Source:$sources -Destination:$destinations -TransferType:Download
} Else {
Write-Warning "No files to receive."
}
}
$Env:PATH += [System.IO.Path]::PathSeparator + "$(Join-Path (Split-Path $Profile) Scripts)"
Function _dh_ShortHomePath {
$p = $PWD.ProviderPath
If($p.StartsWith($HOME)) {
$p = "~" + $p.Substring($HOME.Length)
}
$p
}
Function _dh_SetWindowTitle {
$title = ""
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
$title += "(" + $Global:DevEnv.Name + ") ";
}
if ($PWD.Provider.Name -ne "FileSystem") {
$title += $PWD.Provider.Name.ToLower()
if ($PWD.ProviderPath[0] -ne "\") {
$title += ":\"
} else {
$title += ":"
}
}
$title += _dh_ShortHomePath
$Host.UI.RawUI.WindowTitle = $title
}
Import-Module PromptEd -EA Ignore
If ($null -eq (Get-Module PromptEd)) {
Return
}
$script:Hostname = [System.Environment]::MachineName
Function pe_Hostname {
Write-Host "(" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
Write-Host $Script:HOSTNAME.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Hostname)
Write-Host ")" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Function pe_DevEnvHostname {
Write-Host "(" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
Write-Host $Global:DevEnv.Name -NoNewLine -ForegroundColor (Get-PromptColor DevEnv)
Write-Host "\" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Write-Host $Script:HOSTNAME.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Hostname)
Write-Host ")" -NoNewLine -ForegroundColor (Get-PromptColor Brackets)
}
Function pe_Provider {
if ($PWD.Provider.Name -ne "FileSystem") {
Write-Host $PWD.Provider.Name.ToLower() -NoNewLine -ForegroundColor (Get-PromptColor Provider)
if ($PWD.ProviderPath[0] -ne "\") {
Write-Host ":\" -NoNewLine -ForegroundColor (Get-PromptColor Provider)
} else {
Write-Host ":" -NoNewLine -ForegroundColor (Get-PromptColor Provider)
}
}
}
Function pe_ShortHomePath {
Write-Host (_dh_ShortHomePath) -NoNewLine -ForegroundColor (Get-PromptColor Path)
}
Function pe_NestedPromptLevel {
If ($NestedPromptLevel -Gt 0) {
Write-Host "<" -NoNewLine -ForegroundColor DarkYellow
Write-Host "$NestedPromptLevel" -NoNewLine -ForegroundColor Yellow
Write-Host "> " -NoNewLine -ForegroundColor DarkYellow
}
}
Function pe_Sigil {
If($IsWindows) {
if((
New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "#" -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
} else {
Write-Host "%" -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
}
} Else {
Write-Host $(If($Env:UID -Eq 0) { "#" } Else { "%" }) -NoNewLine -ForegroundColor (Get-PromptColor Sigil)
}
}
Function pe_LastCommandRunTime {
If ($global:_LastCommandTimeSpan -And ($global:_LastCommandTimeSpan.TotalSeconds) -Ge 10) {
# Format buffer: Total Minutes, Seconds, Milliseconds
# We could use total seconds in the second position, but it's only ever used as a primary
# unit if it numbers less than 60.
$fmtBuff = ([int]$global:_LastCommandTimeSpan.TotalMinutes, [int]$global:_LastCommandTimeSpan.Seconds, [int]$global:_LastCommandTimeSpan.Milliseconds)
$fmt = "{1}.{2:D3}s"
If (($global:_LastCommandTimeSpan.TotalSeconds) -Ge 60) {
$fmt = "{0}m{1}s"
}
Write-Host "[" -NoNewLine -ForegroundColor (Get-PromptColor CommandTimeBrackets)
Write-Host ($fmt -f $fmtBuff) -NoNewLine -ForegroundColor (Get-PromptColor CommandTime)
Write-Host "] " -NoNewLine -ForegroundColor (Get-PromptColor CommandTimeBrackets)
}
}
Add-PromptTask CalculateCommandLength {
If ($global:_LastCommandLaunchDate) {
$global:_LastCommandTimeSpan = (Get-Date).Subtract($global:_LastCommandLaunchDate)
$global:_LastCommandLaunchDate = $null
Set-PromptColor CommandTimeBrackets DarkYellow
Set-PromptColor CommandTime Yellow
If (($global:_LastCommandTimeSpan.TotalSeconds) -Ge 30) {
Set-PromptColor CommandTimeBrackets DarkRed
Set-PromptColor CommandTime Red
}
}
}
Add-BuiltinPrompt DHowettPrompt @(
$function:pe_LastCommandRunTime,
$function:pe_NoSeparator,
$function:pe_DevEnvHostname,
$function:pe_Provider,
$function:pe_NoSeparator,
$function:pe_ShortHomePath,
$function:pe_NestedPromptLevel,
$function:pe_NoSeparator,
$function:pe_Sigil
)
Add-PromptTask WindowTitlePath { _dh_SetWindowTitle }
Set-PromptColor Provider DarkMagenta
Set-PromptColor Sigil Green
Set-PromptColor Hostname White
Set-PromptColor DevEnv Green
Set-PromptColor CommandTime Black
Set-PromptColor CommandTimeBrackets Black
# These are already defined by PromptEd
Set-PromptColor Path Magenta
Set-PromptColor Brackets Blue
Set-Prompt DHowettPrompt
If ($null -ne (Get-Module PSReadline)) {
Set-PSReadlineKeyHandler "Enter" -ScriptBlock {
$global:_LastCommandLaunchDate = Get-Date
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
}
If($null -ne (Get-Module PromptEd)) {
Return
}
Write-Warning "Module PromptEd not found; falling back to legacy prompt."
Function Prompt {
_dh_SetWindowTitle
Write-Host "(" -nonewline -foregroundcolor Blue
If($null -ne $Global:DevEnv -and $Global:DevEnv.Name) {
Write-Host $Global:DevEnv.Name -nonewline -foregroundcolor Green
Write-Host "\" -nonewline -foregroundcolor Blue
}
Write-Host $env:COMPUTERNAME.ToLower() -nonewline -foregroundcolor White
Write-Host ") " -nonewline -foregroundcolor Blue
# Show providername if you are outside FileSystem
if ($pwd.Provider.Name -ne "FileSystem") {
Write-Host $pwd.Provider.Name.ToLower() -nonewline -foregroundcolor DarkMagenta
if ($pwd.ProviderPath[0] -ne "\") {
Write-Host ":\" -nonewline -foregroundcolor DarkMagenta
} else {
Write-Host ":" -nonewline -foregroundcolor DarkMagenta
}
}
Write-Host (_dh_ShortHomePath) -nonewline -foregroundcolor Magenta
if((
New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host " #" -nonewline -foregroundcolor Green
} else {
Write-Host " %" -nonewline -foregroundcolor Green
}
return " "
}
Function Set-Prompt([string]$prompt) {
}
Import-Module DirColors -EA Ignore
If ($null -eq (Get-Module DirColors)) {
Return
}
Update-DirColors "$(Split-Path $PROFILE)\dir_colors"
If ($null -eq (Get-Module PSReadline)) {
Write-Warning "PSReadline wasn't loaded; line editing may be impacted"
Return
}
Set-PSReadlineOption -EditMode Emacs
Set-PSReadlineKeyHandler "Ctrl+Delete" KillWord
Set-PSReadlineKeyHandler "Ctrl+Backspace" BackwardKillWord
Set-PSReadlineKeyHandler "Ctrl+LeftArrow" BackwardWord
Set-PSReadlineKeyHandler "Ctrl+RightArrow" NextWord
Set-PSReadlineKeyHandler "Tab" MenuComplete
Function Enter-DevelopmentEnvironment {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True)]
[String]
$Name
)
$item = (Get-Item (Split-Path $Profile | Join-Path -ChildPath "Dev\${Name}") -EA SilentlyContinue)
If($null -eq $item) {
$item = (Get-Item (Split-Path $Profile | Join-Path -ChildPath "Dev\${Name}.ps1") -EA SilentlyContinue)
}
If($item -is [System.IO.DirectoryInfo]) {
$Script:path = $item.ToString()
$item = (Get-Item (Join-Path $item -ChildPath dev.ps1) -EA SilentlyContinue)
}
If($null -eq $item) {
Write-Error -Message "Unable to find development environment ${Name}." -Category InvalidArgument
Return
}
$Global:DevEnv = @{
Name = $Name;
Path = $Script:path;
}
$local:newDE = (& $item.ToString())
ForEach($h in $local:newDE.GetEnumerator()) {
$Global:DevEnv[$h.Name] = $h.Value
}
If($Global:DevEnv.Path) {
Set-Location $Global:DevEnv.Path
}
}
New-Alias -Name dev -Value Enter-DevelopmentEnvironment
Import-Module Pscx -arg (Join-Path (Split-Path $Profile) Pscx.UserPreferences.ps1) -EA Ignore
New-Alias -name vs -Value Import-VisualStudioVars
New-Alias -Name Eject -Value Eject-Disk
New-Alias -Name fmt -Value Format-Clang
New-Alias -Name cc -Value Get-ColoredContent
Function Enter-Subshell {
Push-Location -StackName:"SubshellStack" > $Null
$Host.EnterNestedPrompt()
Pop-Location -StackName:"SubshellStack" > $Null
}
New-Alias -Name ss -Value Enter-Subshell
Function Take-TemporaryDirectory {
$TAKE = [System.IO.Path]::Combine($Env:TEMP, "take$(Get-Random)")
New-Item -ItemType:Directory -Path:"$TAKE" -ErrorAction:Stop > $Null
Push-Location -Path:$TAKE -StackName:"TakeStack" > $Null
$Host.EnterNestedPrompt()
Pop-Location -StackName:"TakeStack" > $Null
If($null -Eq (Get-Item (Join-Path -Path:$TAKE -ChildPath:".keep") -ErrorAction:Ignore)) {
Remove-Item -Recurse $TAKE
Write-Warning "Removed temporary directory $TAKE"
} Else {
Get-Item $TAKE
}
}
New-Alias -Name take -Value Take-TemporaryDirectory
This diff is collapsed.