Skip to content

Commit

Permalink
add - doc - Pipe command finalized
Browse files Browse the repository at this point in the history
---

The pipe command has been finalized! You can now pipe output to another
command as arguments!

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Feb 8, 2025
1 parent d23a361 commit bffe49e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 22 deletions.
69 changes: 49 additions & 20 deletions public/Nitrocid/Shell/ShellBase/Commands/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,27 +380,9 @@ public static void ExecuteCommandWrapped(string Command)
bool buffered = false;
try
{
// First, initialize the alternative command thread
var AltThreads = ShellManager.ShellStack[^1].AltCommandThreads;
if (AltThreads.Count == 0 || AltThreads[^1].IsAlive)
{
DebugWriter.WriteDebug(DebugLevel.I, "Making alt thread for wrapped command {0}...", vars: [Command]);
var WrappedCommand = new KernelThread($"Wrapped Shell Command Thread", false, (cmdThreadParams) => ExecuteCommand((CommandExecutorParameters?)cmdThreadParams));
ShellManager.ShellStack[^1].AltCommandThreads.Add(WrappedCommand);
}

// Then, initialize the buffered writer and execute the commands
DriverHandler.BeginLocalDriver<IConsoleDriver>("Buffered");
// Buffer the target command output
DebugWriter.WriteDebug(DebugLevel.I, "Buffering...");
ShellManager.GetLine(Command, "", currentType, false, false);
CancellationHandlers.AllowCancel();
buffered = true;

// Extract the buffer and then end the local driver
var wrapBuffer = ((Buffered)DriverHandler.CurrentConsoleDriverLocal).consoleBuffer;
var wrapOutput = wrapBuffer.ToString();
wrapBuffer.Clear();
DriverHandler.EndLocalDriver<IConsoleDriver>();
var wrapOutput = BufferCommand(Command);

// Now, print the output
DebugWriter.WriteDebug(DebugLevel.I, "Printing...");
Expand Down Expand Up @@ -457,6 +439,53 @@ public static string[] GetWrappableCommands(string shellType)
return finalWrappables;
}

internal static string BufferCommand(string command)
{
var currentShell = ShellManager.ShellStack[^1];
var currentType = currentShell.ShellType;
var (satisfied, total) = ArgumentsParser.ParseShellCommandArguments(command, currentType);
string commandName = total[0].Command;

// Check to see if the requested command is wrappable
if (!CommandManager.GetCommand(commandName, currentType).Flags.HasFlag(CommandFlags.Wrappable))
throw new KernelException(KernelExceptionType.ShellOperation, Translate.DoTranslation("Can't buffer a command that is not set as wrappable."));

string bufferOutput = "";
bool buffered = false;
try
{
// First, initialize the alternative command thread
var AltThreads = currentShell.AltCommandThreads;
if (AltThreads.Count == 0 || AltThreads[^1].IsAlive)
{
DebugWriter.WriteDebug(DebugLevel.I, "Making alt thread for buffered command {0}...", vars: [command]);
var BufferedCommand = new KernelThread("Buffered Shell Command Thread", false, (cmdThreadParams) => ExecuteCommand((CommandExecutorParameters?)cmdThreadParams));
currentShell.AltCommandThreads.Add(BufferedCommand);
}

// Then, initialize the buffered writer and execute the commands
DriverHandler.BeginLocalDriver<IConsoleDriver>("Buffered");
DebugWriter.WriteDebug(DebugLevel.I, "Buffering...");
ShellManager.GetLine(command, "", currentType, false, false);
CancellationHandlers.AllowCancel();
buffered = true;

// Extract the buffer and then end the local driver
var buffer = ((Buffered)DriverHandler.CurrentConsoleDriverLocal).consoleBuffer;
bufferOutput = buffer.ToString();
buffer.Clear();
DriverHandler.EndLocalDriver<IConsoleDriver>();
}
catch
{
// There is some error, so propagate the error to the caller once we revert the driver.
if (!buffered)
DriverHandler.EndLocalDriver<IConsoleDriver>();
throw;
}
return bufferOutput;
}

private static void CommandDelegate(ShellExecuteInfo ShellInstance, BaseCommand CommandBase, CommandParameters parameters, ref string value)
{
try
Expand Down
5 changes: 4 additions & 1 deletion public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public static class ShellManager
{
AutoCompleter = (_) => CommandManager.GetCommandNames(CurrentShellType)
}),
})
}, new[]
{
new SwitchInfo("quoted", /* Localizable */ "Whether to pass the output of the source command as one quoted argument or unquoted argument")

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / Make API Reference

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / Make API Reference

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized

Check warning on line 172 in public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs

View workflow job for this annotation

GitHub Actions / build

This string, "Whether to pass the output of the source command as one quoted argument or unquoted argument", is marked as localizable, but is unlocalized
}, true)
], new PipeUnifiedCommand()),

new CommandInfo("presets", /* Localizable */ "Opens the shell preset library", new PresetsUnifiedCommand()),
Expand Down
49 changes: 48 additions & 1 deletion public/Nitrocid/Shell/ShellBase/Shells/Unified/Pipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@

using Nitrocid.ConsoleBase.Colors;
using Nitrocid.ConsoleBase.Writers;
using Nitrocid.Files;
using Nitrocid.Files.Paths;
using Nitrocid.Kernel.Debugging;
using Nitrocid.Kernel.Exceptions;
using Nitrocid.Kernel.Time;
using Nitrocid.Languages;
using Nitrocid.Shell.ShellBase.Commands;
using Nitrocid.Shell.ShellBase.Switches;
using System;
using System.Text;

namespace Nitrocid.Shell.ShellBase.Shells.Unified
{
Expand All @@ -34,7 +43,45 @@ class PipeUnifiedCommand : BaseCommand, ICommand

public override int Execute(CommandParameters parameters, ref string variableValue)
{
TextWriters.Write("Pipe TBD...", true, KernelColorType.Warning);
string sourceCommand = parameters.ArgumentsList[0];
StringBuilder targetCommandBuilder = new(parameters.ArgumentsList[1] + " ");
bool quoted = SwitchManager.ContainsSwitch(parameters.SwitchesList, "-quoted");

// First, get the source command output
var currentShell = ShellManager.ShellStack[^1];
var currentType = currentShell.ShellType;
bool buildingTarget = true;
DebugWriter.WriteDebug(DebugLevel.I, $"Writing piped output to the buffer for {sourceCommand}...");
try
{
// Execute the source command
DebugWriter.WriteDebug(DebugLevel.I, $"Executing {sourceCommand} to the buffer for {currentType}...");
string contents = CommandExecutor.BufferCommand(sourceCommand);
variableValue = contents;
buildingTarget = false;

// Build the command based on the output and execute the target command
DebugWriter.WriteDebug(DebugLevel.I, $"Executing {targetCommandBuilder} for {currentType} with contents {contents}...");
targetCommandBuilder.Append(quoted ? $"\"{contents}\"" : contents);
ShellManager.GetLine($"{targetCommandBuilder}", "", currentType, true, false);
}
catch (Exception ex)
{
if (buildingTarget)
{
DebugWriter.WriteDebug(DebugLevel.E, $"Execution of {sourceCommand} to the buffer failed.");
TextWriters.Write(Translate.DoTranslation("Source command execution failed."), KernelColorType.Error);
}
else
{
DebugWriter.WriteDebug(DebugLevel.E, $"Execution of {targetCommandBuilder} failed.");
TextWriters.Write(Translate.DoTranslation("Target command execution failed. The contents may not have been populated properly. Command executed was") + $"\n {targetCommandBuilder}", KernelColorType.Error);
}
TextWriters.Write(Translate.DoTranslation("Pipe is broken."), KernelColorType.Error);
DebugWriter.WriteDebug(DebugLevel.E, $"Reason for failure: {ex.Message}.");
DebugWriter.WriteDebugStackTrace(ex);
return KernelExceptionTools.GetErrorCode(KernelExceptionType.ShellOperation);
}
return 0;
}

Expand Down

0 comments on commit bffe49e

Please sign in to comment.