dotnet-shell/Shell

Would it be possible to use an external prompt, such as startship?

MostHated opened this issue · 8 comments

Hey again,
I am not sure if you are familiar with the prompt starship, but it's written in rust, cross-platform, and says it should be compatible with most any shell. I am not sure if it would be possible to just pop it in there, though, in a manner similar to other shells? I made a post in their discussions area as well, as I wasn't sure exactly who would be the best to ask. starship/starship#2905

If you have any thoughts on this, I definitely would appreciate it.
Thanks,
-MH

-- Edit
I was looking more through their FAQ and came across this bit:

Basic Implementation

The way Starship is built, it should be possible to add support for virtually any shell. The starship binary is stateless and shell agnostic, so as long as your shell supports prompt customization and shell expansion, Starship can be used.

Here's a small example getting Starship working with bash:

# Get the status code from the last command executed
STATUS=$?

# Get the number of jobs running.
NUM_JOBS=$(jobs -p | wc -l)

# Set the prompt to the output of `starship prompt`
PS1="$(starship prompt --status=$STATUS --jobs=$NUM_JOBS)"

Done a quick test and replacing the existing prompt handler with this seems to work fine.

Shell.Prompt = () =>
    var prompt = `starship prompt --status=$Shell.LastExitCode$ --jobs=$Shell.BackgroundProcesses.Count()$`;
    return ColorString.FromRawANSI(prompt);
};

I appreciate you taking the time to look into this. I tried swapping out the default for your example, but for some reason, I keep getting the following:

zsh > dotnet-shell
An error occured loading core.nsh
StartIndex cannot be less than zero. (Parameter 'startIndex')
# >

I tried to add a few slight checks, but I still end up getting the same result:

core.nsh snippet
// bool? powerline = null;
#region c#
Shell.Prompt = () =>
{
    var exitCode = `Shell.LastExitCode`;
    if (exitCode == null || exitCode < 0)
    {
        exitCode = 0;
    }

    var bgJobs = `Shell.BackgroundProcesses.Count()`;
    if (bgJobs == null || bgJobs < 0)
    {
        bgJobs = 0;
    }

    var prompt = `starship prompt --status=$exitCode$ --jobs=$bgJobs$`;
    return ColorString.FromRawANSI(prompt);
};

// Shell.Prompt = () =>
#endregion
// {
//       if (powerline == null && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
//       {
//           var powerlineHelpOut=`which powerline`;
//           powerline = !string.IsNullOrWhiteSpace(powerlineHelpOut);
//       }

//       if (powerline == true)
//       {
//           string powerLinePrompt=`powerline-render shell left --last-exit-code=$Shell.LastExitCode$`;
//           return new ColorString("!", Color.Green, Color.LightBlue) + ColorString.FromRawANSI( powerLinePrompt );
//       }
//       else
//       {
//           return new ColorString("!" + Environment.UserName + "@" + Environment.MachineName, Color.Green) + new ColorString(" " + Shell.WorkingDirectory + ">", Color.Blue) + " ";
//       }
// };

I just edited core.nsh directly. It seemed like that would be the proper way to go about it, am I mistaken on that?

Thanks,
-MH

There are problems:
var exitCode = `Shell.LastExitCode`; should actually be var exitCode = Shell.LastExitCode;
Putting text in backticks means execute this shell command, take the stdout and put in a variable. But Shell.LastExitCode is already a piece of C# -- see LastExitCode. That means we can just assign it.

Same for the bgJobs variable assigned as well - drop those backticks.

bgJobs and exitCode will also never be null or < 0 so you can drop that check as well.

When executing the starship prompt helper you are correctly using $VARIABLE$ notation so that dotnet-shell knows to replace that text with a variable held in C#.

Make those changes and your code should work.

My guess is that you getting that error as some bash error message is being evaluated when starship is run. Personally I would stick with my code, it works and its a bit more concise.

Ah yes, that definitely makes sense. Unfortunately, though, the original code you had shared is what was giving the An error occured loading core.nsh. StartIndex cannot be less than zero. (Parameter 'startIndex') message. That was the reason I tried to check to make sure one of the values weren't coming back as less than 0. The error message didn't change, though between the original code you shared and the modification I had tried.

This is my core.nsh that i've just tested on Ubuntu and works. Could I ask that you give this a go. If there is still a bug there is a much more exciting issue.

I have reproduced the issue. The dotnet-shell preprocess isn't taking out all the comments correctly. Removing the large comment blog (as in my paste) will fix your issue.

I was just in process of typing that out, you beat me to it. 👍

Latest release will fix the issue