Skip to content

Commit

Permalink
Made jumpstat queryable
Browse files Browse the repository at this point in the history
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 :)
  • Loading branch information
tkellogg committed Aug 20, 2012
1 parent 028f7af commit e18ca6d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
17 changes: 17 additions & 0 deletions Jump.Location/CommandController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions Jump.Location/GetJumpStatusCommand.cs
Original file line number Diff line number Diff line change
@@ -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<IRecord> records)
{
foreach (var record in records)
WriteObject(record);
}
}
}
1 change: 1 addition & 0 deletions Jump.Location/Jump.Location.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Database.cs" />
<Compile Include="DirectoryWaitPeriod.cs" />
<Compile Include="FileStoreProvider.cs" />
<Compile Include="GetJumpStatusCommand.cs" />
<Compile Include="JumpLocationCommand.cs" />
<Compile Include="JumpLocationSnapIn.cs">
<SubType>Component</SubType>
Expand Down
21 changes: 1 addition & 20 deletions Jump.Location/JumpLocationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> GetTabExpansion(string searchTerm)
{
Expand All @@ -37,9 +28,6 @@ public static IEnumerable<string> GetTabExpansion(string searchTerm)
[Parameter(ValueFromRemainingArguments = true)]
public string[] Directory { get; set; }

[Parameter]
public SwitchParameter Status { get; set; }

[Parameter]
public SwitchParameter Initialize { get; set; }

Expand All @@ -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)
{
Expand Down
4 changes: 1 addition & 3 deletions Jump.Location/Load.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit e18ca6d

Please sign in to comment.