48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using NozCli.Repl;
|
|
using Spectre.Console;
|
|
|
|
namespace NozCli.Commands;
|
|
|
|
public class CdCommand : ICommand
|
|
{
|
|
public string Name => "cd";
|
|
public string Description => "Navigate into a topic or folder";
|
|
public string Usage => "cd <folder> | cd .. | cd ~";
|
|
|
|
public async Task ExecuteAsync(string[] args, ReplContext ctx)
|
|
{
|
|
var target = args.FirstOrDefault();
|
|
|
|
if (target is null or "~")
|
|
{
|
|
ctx.CurrentPath = "~";
|
|
return;
|
|
}
|
|
|
|
if (target == "..")
|
|
{
|
|
var parts = ctx.CurrentPath.TrimEnd('/').Split('/');
|
|
ctx.CurrentPath = parts.Length > 1
|
|
? string.Join("/", parts[..^1])
|
|
: "~";
|
|
return;
|
|
}
|
|
|
|
var candidate = ctx.CurrentPath == "~"
|
|
? $"~/noosphere/{target}"
|
|
: $"{ctx.CurrentPath}/{target}";
|
|
|
|
// Check the path has at least one note under it
|
|
var prefix = candidate.Replace("~/noosphere/", "").Replace("~/noosphere", "");
|
|
var exists = ctx.NoteCache.Any(n => n.Slug.StartsWith(prefix + "/") || n.Slug == prefix);
|
|
|
|
if (!exists)
|
|
{
|
|
AnsiConsole.MarkupLine($"[red]cd: {target}: no such folder[/]");
|
|
return;
|
|
}
|
|
|
|
ctx.CurrentPath = candidate;
|
|
await Task.CompletedTask;
|
|
}
|
|
}
|