Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Fallout.Cli/Commands/Navigation/GetNextDirectoryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Linq;
using Fallout.Common;
using Fallout.Common.IO;

namespace Fallout.Cli.Commands.Navigation;

/// <summary><c>fallout :GetNextDirectory</c>: prints (and consumes) the queued next directory.</summary>
public sealed class GetNextDirectoryCommand : IFalloutCommand
{
public string Name => "GetNextDirectory";

public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
var content = NavigationSession.SessionFile.Existing()?.ReadAllLines();
if (content == null || string.IsNullOrWhiteSpace(content[0]))
{
Console.WriteLine(EnvironmentInfo.WorkingDirectory);
return 1;
}

var nextDirectory = content[0];
content[0] = string.Empty;
NavigationSession.SessionFile.WriteAllLines(content);
Console.WriteLine(nextDirectory);
return 0;
}
}
49 changes: 49 additions & 0 deletions src/Fallout.Cli/Commands/Navigation/NavigationSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Fallout.Common;
using Fallout.Common.IO;
using Fallout.Common.Utilities;
using static Fallout.Common.Constants;

namespace Fallout.Cli.Commands.Navigation;

/// <summary>
/// Shared per-terminal-session state for the directory-navigation commands. The companion shell
/// functions invoke the commands by name (preserved on conversion):
/// <code>
/// function nuke- { nuke :PopDirectory; cd $(nuke :GetNextDirectory) }
/// function nuke/ { nuke :PushWithChosenRootDirectory; cd $(nuke :GetNextDirectory) }
/// function nuke. { nuke :PushWithCurrentRootDirectory; cd $(nuke :GetNextDirectory) }
/// function nuke.. { nuke :PushWithParentRootDirectory; cd $(nuke :GetNextDirectory) }
/// </code>
/// </summary>
internal static class NavigationSession
{
public static string SessionId
=> EnvironmentInfo.Platform switch
{
PlatformFamily.OSX => EnvironmentInfo.GetVariable("TERM_SESSION_ID").NotNull()[7..],
PlatformFamily.Windows => EnvironmentInfo.GetVariable("WT_SESSION").NotNull(),
_ => throw new NotSupportedException($"{EnvironmentInfo.Platform} has no session id selector.")
};

public static AbsolutePath SessionFile => GlobalTemporaryDirectory / $"nuke-{SessionId}.dat";

public static int PushAndSetNext(Func<string> directoryProvider)
{
try
{
var content = SessionFile.Existing()?.ReadAllLines().ToList() ?? new List<string> { null };
content[0] = directoryProvider.Invoke();
content.Insert(index: 1, EnvironmentInfo.WorkingDirectory);
SessionFile.WriteAllLines(content);
return 0;
}
catch (Exception exception)
{
Console.Error.WriteLine(exception.Message);
return 1;
}
}
}
26 changes: 26 additions & 0 deletions src/Fallout.Cli/Commands/Navigation/PopDirectoryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using Fallout.Common.IO;

namespace Fallout.Cli.Commands.Navigation;

/// <summary><c>fallout :PopDirectory</c>: pops the previous directory back to the front of the queue.</summary>
public sealed class PopDirectoryCommand : IFalloutCommand
{
public string Name => "PopDirectory";

public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
var content = NavigationSession.SessionFile.Existing()?.ReadAllLines().ToList();
if (content == null || content.Count <= 1)
{
Console.Error.WriteLine("No previous directory");
return 1;
}

content[0] = content[1];
content.RemoveAt(1);
NavigationSession.SessionFile.WriteAllLines(content);
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Linq;
using Fallout.Cli.Prompts;
using Fallout.Common;
using Fallout.Common.IO;
using static Fallout.Common.Constants;

namespace Fallout.Cli.Commands.Navigation;

/// <summary>
/// <c>fallout :PushWithChosenRootDirectory</c>: prompts for a discovered root directory and queues it.
/// </summary>
public sealed class PushWithChosenRootDirectoryCommand : IFalloutCommand
{
private readonly IConsolePrompts _prompts;

public PushWithChosenRootDirectoryCommand(IConsolePrompts prompts) => _prompts = prompts;

public string Name => "PushWithChosenRootDirectory";

public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
return NavigationSession.PushAndSetNext(() =>
{
var directories = EnvironmentInfo.WorkingDirectory.GlobDirectories($"**/{FalloutDirectoryName}")
.Concat(EnvironmentInfo.WorkingDirectory.GlobFiles($"**/{FalloutDirectoryName}"))
.Where(x => !x.Equals(EnvironmentInfo.WorkingDirectory))
.Select(x => x.Parent)
.Select(x => (x, EnvironmentInfo.WorkingDirectory.GetRelativePathTo(x).ToString()))
.OrderBy(x => x.Item2).ToArray();

return _prompts.PromptForChoice("Where to go next?", directories);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Fallout.Common;
using Fallout.Common.IO;
using Fallout.Common.Utilities;

namespace Fallout.Cli.Commands.Navigation;

/// <summary><c>fallout :PushWithCurrentRootDirectory</c>: queues the current root directory.</summary>
public sealed class PushWithCurrentRootDirectoryCommand : IFalloutCommand
{
public string Name => "PushWithCurrentRootDirectory";

public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
return NavigationSession.PushAndSetNext(() => rootDirectory.NotNull("No root directory"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;
using Fallout.Common;
using Fallout.Common.IO;
using Fallout.Common.Utilities;
using static Fallout.Common.Constants;

namespace Fallout.Cli.Commands.Navigation;

/// <summary><c>fallout :PushWithParentRootDirectory</c>: queues the parent repository's root directory.</summary>
public sealed class PushWithParentRootDirectoryCommand : IFalloutCommand
{
public string Name => "PushWithParentRootDirectory";

public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
return NavigationSession.PushAndSetNext(() => TryGetRootDirectoryFrom(Path.GetDirectoryName(rootDirectory.NotNull("No root directory")))
.NotNull("No parent root directory"));
}
}
101 changes: 0 additions & 101 deletions src/Fallout.Cli/Program.Navigation.cs

This file was deleted.

13 changes: 8 additions & 5 deletions src/Fallout.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ private static void RegisterCommands(IServiceCollection services)

// Legacy handlers still living on Program, adapted until they are extracted into command
// types. Each conversion deletes one line here plus its Program.X.cs partial.
services.AddSingleton<IFalloutCommand>(new DelegateCommand("GetNextDirectory", GetNextDirectory));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("PopDirectory", PopDirectory));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("PushWithCurrentRootDirectory", PushWithCurrentRootDirectory));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("PushWithParentRootDirectory", PushWithParentRootDirectory));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("PushWithChosenRootDirectory", PushWithChosenRootDirectory));
services.AddSingleton<IFalloutCommand, Commands.Navigation.GetNextDirectoryCommand>();
services.AddSingleton<IFalloutCommand, Commands.Navigation.PopDirectoryCommand>();
services.AddSingleton<IFalloutCommand, Commands.Navigation.PushWithCurrentRootDirectoryCommand>();
services.AddSingleton<IFalloutCommand, Commands.Navigation.PushWithParentRootDirectoryCommand>();
services.AddSingleton<IFalloutCommand, Commands.Navigation.PushWithChosenRootDirectoryCommand>();

// Legacy handlers still living on Program, adapted until they are extracted into command
// types. Each conversion deletes one line here plus its Program.X.cs partial.
}

internal static void PrintInfo()
Expand Down