Replace vendor powershell.tmBundle by SublimeText powershell. Improve samples for PS.

This commit is contained in:
Sergei Vorobev
2015-01-27 18:31:17 -08:00
parent cf0bc3914f
commit 160c0b4ac0
9 changed files with 277 additions and 12 deletions

6
.gitmodules vendored
View File

@@ -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

View File

@@ -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

View File

@@ -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 = ''
}

View File

@@ -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

View File

@@ -1,2 +0,0 @@
# Hello world in powershell
Write-Host 'Hello World'

View File

@@ -1,5 +0,0 @@
# Hello World powershell module
function hello() {
Write-Host 'Hello World'
}

View File

@@ -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
vendor/grammars/powershell vendored Submodule