Skip to content

Commit

Permalink
Merge pull request #2047 from vors/powershell
Browse files Browse the repository at this point in the history
Replace vendor powershell.tmBundle by SublimeText powershell
  • Loading branch information
arfon committed Jan 28, 2015
2 parents cf0bc39 + 160c0b4 commit d6fdbaf
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
[submodule "vendor/grammars/Handlebars"]
path = vendor/grammars/Handlebars
url = https://github.com/daaain/Handlebars
[submodule "vendor/grammars/powershell.tmbundle"]
path = vendor/grammars/powershell.tmbundle
url = https://github.com/davidpeckham/powershell.tmbundle
[submodule "vendor/grammars/powershell"]
path = vendor/grammars/powershell
url = https://github.com/SublimeText/PowerShell
[submodule "vendor/grammars/jade-tmbundle"]
path = vendor/grammars/jade-tmbundle
url = https://github.com/davidrios/jade-tmbundle
Expand Down
2 changes: 1 addition & 1 deletion grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ vendor/grammars/pike-textmate:
- source.pike
vendor/grammars/postscript.tmbundle:
- source.postscript
vendor/grammars/powershell.tmbundle:
vendor/grammars/powershell:
- source.powershell
vendor/grammars/processing.tmbundle:
- source.processing
Expand Down
116 changes: 116 additions & 0 deletions samples/PowerShell/ZLocation.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#
# Module manifest for module 'ZLocation'
#
# Generated by: sevoroby
#
# Generated on: 12/10/2014
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'ZLocation.psm1'

# Version number of this module.
ModuleVersion = '0.1'

# ID used to uniquely identify this module
GUID = '18e8ca17-7f67-4f1c-85ff-159373bf66f5'

# Author of this module
Author = 'Sergei Vorobev'

# Company or vendor of this module
CompanyName = 'Microsoft'

# Copyright statement for this module
Copyright = '(c) 2014 Sergei Vorobev. All rights reserved.'

# Description of the functionality provided by this module
# Description = ''

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @("ZLocation.Storage.psm1", "ZLocation.Search.psm1")

# Functions to export from this module
FunctionsToExport = '*'

# Cmdlets to export from this module
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module
AliasesToExport = '*'

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}
91 changes: 91 additions & 0 deletions samples/PowerShell/ZLocation.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#
# Weight function.
#
function Update-ZLocation([string]$path)
{
$now = [datetime]::Now
if (Test-Path variable:global:__zlocation_current)
{
$prev = $global:__zlocation_current
$weight = $now.Subtract($prev.Time).TotalSeconds
Add-ZWeight ($prev.Location) $weight
}

$global:__zlocation_current = @{
Location = $path
Time = [datetime]::Now
}

# populate folder immidiatly after the first cd
Add-ZWeight $path 0
}

# this approach hurts `cd` performance (0.0008 sec vs 0.025 sec).
# Consider replace it with OnIdle Event.
(Get-Variable pwd).attributes.Add((new-object ValidateScript { Update-ZLocation $_.Path; return $true }))
#
# End of weight function.
#


#
# Tab complention.
#
if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion PreZTabExpansion
}

function Get-EscapedPath
{
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[string]$path
)

process {
if ($path.Contains(' '))
{
return '"' + $path + '"'
}
return $path
}
}

function global:TabExpansion($line, $lastWord) {
switch -regex ($line) {
"^(Set-ZLocation|z) .*" {
$arguments = $line -split ' ' | Where { $_.length -gt 0 } | select -Skip 1
Find-Matches (Get-ZLocation) $arguments | Get-EscapedPath
}
default {
if (Test-Path Function:\PreZTabExpansion) {
PreZTabExpansion $line $lastWord
}
}
}
}
#
# End of tab completion.
#

function Set-ZLocation()
{
if (-not $args) {
$args = @()
}
$matches = Find-Matches (Get-ZLocation) $args
if ($matches) {
Push-Location ($matches | Select-Object -First 1)
} else {
Write-Warning "Cannot find matching location"
}
}


Set-Alias -Name z -Value Set-ZLocation
Export-ModuleMember -Function Set-ZLocation, Get-ZLocation -Alias z
2 changes: 0 additions & 2 deletions samples/PowerShell/hello.ps1

This file was deleted.

5 changes: 0 additions & 5 deletions samples/PowerShell/hello.psm1

This file was deleted.

65 changes: 65 additions & 0 deletions samples/PowerShell/history.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function Save-HistoryAll() {
$history = Get-History -Count $MaximumHistoryCount
[array]::Reverse($history)
$history = $history | Group CommandLine | Foreach {$_.Group[0]}
[array]::Reverse($history)
$history | Export-Csv $historyPath
}

function Save-HistoryIncremental() {
# Get-History -Count $MaximumHistoryCount | Group CommandLine | Foreach {$_.Group[0]} | Export-Csv $historyPath
Get-History -Count 1 | Export-Csv -Append $historyPath
}

# hook powershell's exiting event & hide the registration with -supportevent.
#Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { Save-History }

$oldPrompt = Get-Content function:\prompt

if( $oldPrompt -notlike '*Save-HistoryIncremental*' )
{
$newPrompt = @'
Save-HistoryIncremental
'@
$newPrompt += $oldPrompt
$function:prompt = [ScriptBlock]::Create($newPrompt)
}

# load previous history, if it exists
if ((Test-Path $historyPath)) {
$loadTime =
(
Measure-Command {
Import-Csv $historyPath | Add-History
Save-HistoryAll
Clear-History
Import-Csv $historyPath | ? {$count++;$true} | Add-History
}
).totalseconds
Write-Host -Fore Green "`nLoaded $count history item(s) in $loadTime seconds.`n"
}


function Search-History()
{
<#
.SYNOPSIS
Retrive and filter history based on query
.DESCRIPTION
.PARAMETER Name
.EXAMPLE
.LINK
#>

param(
[string[]] $query
)

$history = Get-History -Count $MaximumHistoryCount
foreach ($item in $query){
$item = $item.ToLower()
$history = $history | where {$_.CommandLine.ToLower().Contains($item)}
}
$history
}
1 change: 1 addition & 0 deletions vendor/grammars/powershell
Submodule powershell added at 84fd97
1 change: 0 additions & 1 deletion vendor/grammars/powershell.tmbundle
Submodule powershell.tmbundle deleted from f8716b

0 comments on commit d6fdbaf

Please sign in to comment.