rmyorston / busybox-w32

WIN32 native port of BusyBox.
https://frippery.org/busybox
Other
670 stars 124 forks source link

Settings empty title #401

Closed ale5000-git closed 4 months ago

ale5000-git commented 4 months ago

Hi, setting an empty title using printf '\033]0;\007\r' works on cmd.exe but not on Windows Terminal.

rmyorston commented 4 months ago

It looks as though the issue is with the Terminal: it doesn't work in PowerShell either:

Write-Output "$([char]27)]0;hello$([char]7)"

works, but

Write-Output "$([char]27)]0;$([char]7)"

doesn't.

ale5000-git commented 4 months ago

If the problem isn't in busybox, then as even better alternative is it possible to support this?

printf '\033[22;0t' # Save current title on stack
printf '\033[23;0t' # Restore title from stack
rmyorston commented 4 months ago

How would that work? Isn't the stack of titles a property of the terminal?

ale5000-git commented 4 months ago

This is my suggestion:

A real way would be to find the hWnd of the window where busybox is hosted and get the title directly but I think it would be too much complicated and unneded since Windows Terminal set automatically the title as Command Prompt - command (example: Command Prompt - busybox ash).

So the simplest way is to keep track of it in 2 variables.

The first variable is the last_title:

The second variable is the title_saved:

The rest goes without saying.

rmyorston commented 4 months ago

Emulating a stack of title in BusyBox isn't going to work without support from the underlying terminal.

Perhaps it would help if you could explain what you're trying to do. You've spoken of wanting to set the title to an empty string and of fetching/saving titles to a stack, but it isn't clear how these are related.

ale5000-git commented 4 months ago

The empty title was only a workaround (I just reported the problem in case was an easy fix).

Mainly I want:

So I just need to restore the titles that are set during the lifetime of the busybox executable (I don't care about the titles before).

rmyorston commented 4 months ago

It seems that fetching the title of a terminal isn't a very well supported operation.

As a bit of a hack I've added a title built-in to the shell in busybox-w32.

This is completely non-portable. If it works for you, good. If not, I refer you to the 'NO WARRANTY' clause of the licence ;-)

There's a new prerelease, of course.

ale5000-git commented 4 months ago

It works perfectly, thanks.

To have enough portability I have used these:

set_title()
{
  if test "${CI:-false}" != 'false'; then return 1; fi

  if command 1> /dev/null -v title; then
    PREVIOUS_TITLE="$(title)" # Save current title
    title "${1:?}"            # Set new title
  elif test -t 1; then
    printf '\033[22;0t\r' && printf '       \r'                         # Save current title on stack
    printf '\033]0;%s\007\r' "${1:?}" && printf '    %*s \r' "${#1}" '' # Set new title
  elif test -t 2; then
    printf 1>&2 '\033[22;0t\r' && printf 1>&2 '       \r'                         # Save current title on stack
    printf 1>&2 '\033]0;%s\007\r' "${1:?}" && printf 1>&2 '    %*s \r' "${#1}" '' # Set new title
  fi
}

restore_title()
{
  if test "${CI:-false}" != 'false'; then return 1; fi

  if command 1> /dev/null -v title; then
    title "${PREVIOUS_TITLE?}" # Restore saved title
    PREVIOUS_TITLE=''
  elif test -t 1; then
    printf '\033]0;\007\r' && printf '     \r'  # Set empty title (fallback in case saving/restoring title doesn't work)
    printf '\033[23;0t\r' && printf '       \r' # Restore title from stack
  elif test -t 2; then
    printf 1>&2 '\033]0;\007\r' && printf 1>&2 '     \r'  # Set empty title (fallback in case saving/restoring title doesn't work)
    printf 1>&2 '\033[23;0t\r' && printf 1>&2 '       \r' # Restore title from stack
  fi
}