From e18ca6d58b3b7fa8fbb20375a69cd77af038b95a Mon Sep 17 00:00:00 2001 From: tkellogg Date: Sun, 19 Aug 2012 23:50:04 -0600 Subject: [PATCH] Made jumpstat queryable I broke jumpstat into it's own Get-JumpStatus cmdlet to start maintaining separation of concerns. Once this was done, it was obvious to implement querying via Get-JumpStatus since it's already returning objects instead of changing directories. This is a great step towards scripting this a lot better than autojump was done. For one, it'll be easier to manipulate the DB (once we expose -Save). But moreso, this is a great step toward using jumpstat as input into other commands. I'd love to be able to do PS> explorer $(jumpstat ju) And have it jump right to my favorite project :) --- Jump.Location/CommandController.cs | 17 ++++++++++++++++ Jump.Location/GetJumpStatusCommand.cs | 29 +++++++++++++++++++++++++++ Jump.Location/Jump.Location.csproj | 1 + Jump.Location/JumpLocationCommand.cs | 21 +------------------ Jump.Location/Load.ps1 | 4 +--- 5 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 Jump.Location/GetJumpStatusCommand.cs diff --git a/Jump.Location/CommandController.cs b/Jump.Location/CommandController.cs index e515294..f61f073 100644 --- a/Jump.Location/CommandController.cs +++ b/Jump.Location/CommandController.cs @@ -14,6 +14,7 @@ class CommandController private bool needsToSave; private DirectoryWaitPeriod waitPeriod; private DateTime lastSaveDate = DateTime.Now; + private static CommandController defaultInstance; internal CommandController(IDatabase database, IFileStoreProvider fileStore) { @@ -23,6 +24,22 @@ internal CommandController(IDatabase database, IFileStoreProvider fileStore) thread.Start(); } + public static CommandController DefaultInstance + { + get + { + if (defaultInstance == null) + { + var home = Environment.GetEnvironmentVariable("USERPROFILE"); + // TODO: I think there's potential here for a bug + home = home ?? @"C:\"; + var dbLocation = Path.Combine(home, "jump-location.txt"); + defaultInstance = Create(dbLocation); + } + return defaultInstance; + } + } + public static CommandController Create(string path) { var fileStore = new FileStoreProvider(path); diff --git a/Jump.Location/GetJumpStatusCommand.cs b/Jump.Location/GetJumpStatusCommand.cs new file mode 100644 index 0000000..b77f2cf --- /dev/null +++ b/Jump.Location/GetJumpStatusCommand.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Management.Automation; + +namespace Jump.Location +{ + [Cmdlet("Get", "JumpStatus")] + public class GetJumpStatusCommand : PSCmdlet + { + private static readonly CommandController Controller = CommandController.DefaultInstance; + + [Parameter(ValueFromRemainingArguments = true)] + public string[] Directory { get; set; } + + protected override void ProcessRecord() + { + if (Directory == null || Directory.Length == 0) + ProcessSearch(Controller.GetOrderedRecords()); + else + ProcessSearch(Controller.GetMatchesForSearchTerm(Directory)); + } + + private void ProcessSearch(IEnumerable records) + { + foreach (var record in records) + WriteObject(record); + } + } +} diff --git a/Jump.Location/Jump.Location.csproj b/Jump.Location/Jump.Location.csproj index 9e47397..177fa35 100644 --- a/Jump.Location/Jump.Location.csproj +++ b/Jump.Location/Jump.Location.csproj @@ -62,6 +62,7 @@ + Component diff --git a/Jump.Location/JumpLocationCommand.cs b/Jump.Location/JumpLocationCommand.cs index 5f0b8ce..f810d59 100644 --- a/Jump.Location/JumpLocationCommand.cs +++ b/Jump.Location/JumpLocationCommand.cs @@ -10,16 +10,7 @@ namespace Jump.Location public class JumpLocationCommand : PSCmdlet { private static bool _hasRegisteredDirectoryHook; - private static readonly CommandController Controller; - - static JumpLocationCommand() - { - var home = Environment.GetEnvironmentVariable("USERPROFILE"); - // TODO: I think there's potential here for a bug - home = home ?? @"C:\"; - var dbLocation = Path.Combine(home, "jump-location.txt"); - Controller = CommandController.Create(dbLocation); - } + private static readonly CommandController Controller = CommandController.DefaultInstance; public static IEnumerable GetTabExpansion(string searchTerm) { @@ -37,9 +28,6 @@ public static IEnumerable GetTabExpansion(string searchTerm) [Parameter(ValueFromRemainingArguments = true)] public string[] Directory { get; set; } - [Parameter] - public SwitchParameter Status { get; set; } - [Parameter] public SwitchParameter Initialize { get; set; } @@ -66,13 +54,6 @@ protected override void BeginProcessing() protected override void ProcessRecord() { - if (Status) - { - foreach(var record in Controller.GetOrderedRecords()) - WriteObject(record); - return; - } - // This lets us do just `Jump-Location` to initialize everything in the profile script if (Initialize) { diff --git a/Jump.Location/Load.ps1 b/Jump.Location/Load.ps1 index dab52b9..988d884 100644 --- a/Jump.Location/Load.ps1 +++ b/Jump.Location/Load.ps1 @@ -14,9 +14,7 @@ if (-Not (Get-Command Jump-Location -ErrorAction SilentlyContinue)) { Import-Module $dllpath -Global -DisableNameChecking New-Alias -Name j -Value Jump-Location -Scope Global - function global:Jumpstat() { - Jump-Location -Status - } + New-Alias -Name jumpstat -Value Get-JumpStatus -Scope Global function global:PushJ { Param (