Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newline added to fish prompt #4032

Closed
OmegaLambda1998 opened this issue Sep 15, 2021 · 19 comments
Closed

Newline added to fish prompt #4032

OmegaLambda1998 opened this issue Sep 15, 2021 · 19 comments
Labels

Comments

@OmegaLambda1998
Copy link

Describe the bug
When using fish with anything other than the default prompt, a newline is added between the prompt and the cursor.
Initially submitted as an issue with fish: fish-shell/fish-shell#8297 (where a lot more information can be found), who believe kitty is adding this newline

To Reproduce
Steps to reproduce the behavior:

  1. Install fish
  2. Change fish_prompt
  3. Cursor is now offset from prompt

Screenshots
If applicable, add screenshots to help explain your problem.
1631703062_grim

The expected behaviour is:

/home/patricka
>newline

Environment details

kitty 0.23.1 (5916bbd5b3) created by Kovid Goyal
Linux Archbox 5.14.3-arch1-1 #1 SMP PREEMPT Sun, 12 Sep 2021 20:51:34 +0000 x86_64
Arch Linux \r (\l)
LSB_VERSION=1.4
DISTRIB_ID=Arch
DISTRIB_RELEASE=rolling
DISTRIB_DESCRIPTION="Arch Linux"
Running under:Wayland
Frozen: False
Paths:
  kitty: /usr/bin/kitty
  base dir: /usr/lib/kitty
  extensions dir: /usr/lib/kitty/kitty
  system shell: /usr/bin/fish
Loaded config files:
  /home/patricka/.config/kitty/kitty.conf

Config options different from defaults:
background_opacity      0
bold_font               FiraCode Nerd Font Mono Bold
clipboard_control       ('write-clipboard', 'write-primary')
cursor_blink_interval   0.0
cursor_text_color       None
enable_audio_bell       False
font_family             FiraCode Nerd Font Mono
update_check_interval   24.0
Changed mouse actions:
	 b1 -2 ungrabbed KeyAction('mouse_click_url_or_select')
	 shift+b1 -2 ungrabbed KeyAction('mouse_click_url_or_select')
	 shift+b1 -2 grabbed KeyAction('mouse_click_url_or_select')
	 shift+ctrl+b1 repeat ungrabbed KeyAction('mouse_click_url')
	 shift+ctrl+b1 repeat grabbed KeyAction('mouse_click_url')
Added shortcuts:
	shift+super+w KeyAction('close_os_window')
Colors:
	active_tab_background   #f8f8f2   
	active_tab_foreground   #282a36   
	background              #282a36   
	color0                  #21222c   
	color1                  #ff5555   
	color10                 #69ff94   
	color11                 #ffffa5   
	color12                 #d6acff   
	color13                 #ff92df   
	color14                 #a4ffff   
	color2                  #50fa7b   
	color3                  #f1fa8c   
	color4                  #bd93f9   
	color5                  #ff79c6   
	color6                  #8be9fd   
	color7                  #f8f8f2   
	color8                  #6272a4   
	color9                  #ff6e6e   
	cursor                  #f8f8f2   
	foreground              #f8f8f2   
	inactive_tab_background #6272a4   
	inactive_tab_foreground #282a36   
	mark1_background        #ff5555   
	mark1_foreground        #282a36   
	selection_background    #44475a   
	selection_foreground    #ffffff   
	url_color               #8be9fd   

Additional context
Cursor still offset with kitty --config NONE
1631703363_grim

@kovidgoyal
Copy link
Owner

kitty doesnt draw prompts. The shell does. From your screen shot you are running

echo ">"

what you should be running is

echo -n "> "

or really

printf "$PWD\n> "

@OmegaLambda1998
Copy link
Author

Sorry to open this again, however this doesn't solve my issue.
If you look at the original issue: fish-shell/fish-shell#8297
You'll see that this issue occurs for every prompt (including well established ones like pure and tide) not just my mwe one. The people over at fish referred me to kitty being the cause of the issue.

@kovidgoyal
Copy link
Owner

As I said, kitty does not draw prompts, the shell does. There is nothing kitty can do about what the shell chooses to draw. And running

kitty --config NONE -o shell=fish

with the fish_prompt set to printf "$PWD\n> " works fine for me.

@kovidgoyal
Copy link
Owner

And looking at your issue this is with regards to shell integration? which is not released yet?

@OmegaLambda1998
Copy link
Author

I'm not sure, I just installed kitty and fish today and am just trying to get them to play nice together.

So what would the next step be? If it is shell integration, how do I remove that from my kitty install?

@kovidgoyal
Copy link
Owner

shell integration in kitty is not released. So where did you get kitty from? In any case if you want to disable it, use shell_integration disabled in kitty.conf.

@OmegaLambda1998
Copy link
Author

I installed kitty-git from the arch user repository (https://aur.archlinux.org/packages/kitty-git/). And yes shell_integration disabled fixed the issue. Thanks for your patience!

@kovidgoyal
Copy link
Owner

And this will fix it even with shell integration: a073936

for future reference, please mention the version of kitty you are using.

@faho
Copy link

faho commented Sep 15, 2021

Let me explain the problem, @kovidgoyal:

Fish executes fish_prompt, and then trims a trailing newline from its output. This is so simple prompts like

function fish_prompt
    echo $PWD '>'
end

work without us having to constantly explain to use echo -n.

However, because kitty adds more output after the actual prompt output, the newline now isn't trailing anymore, so fish won't remove it. Ideally, you'd be trimming the trailing newlines yourself, or we'd find some other way to add your prompt marker.

And this will fix it even with shell integration: a073936

It won't. It'll break every multiline prompt, because fish splits command substitutions on newlines.

The simplest solution I can come up with requires printing twice (string join could almost be used directly, but always prints a trailing newline):

    set --local op (_ksi_original_fish_prompt)  # trim trailing newlines to mimic fish behavior
    string join \n -- $op[1..-2] # notice no quotes, so the lines are passed separately
    printf '%s' $op[-1] # print the last component without a newline

or, with just printf you'll have to check if there is something to print because printf '%s\n' will print a newline even without a string to print (same as in bash):

    set --local op (_ksi_original_fish_prompt)  # trim trailing newlines to mimic fish behavior
    if set -q op[2]
        printf '%s\n' $op[1..-2] # notice no quotes, so the lines are passed separately
    end
    printf '%s' $op[-1] # print the last component without a newline

I think that should cover all bases, but it's possible I've overlooked something. Splitting on newlines makes it super-easy to deal with line-based output, but also makes it easy to drop or accidentally add a newline somewhere, so this is probably more finicky than it needs to be. Sorry!

@OmegaLambda1998
Copy link
Author

My apologies, is kitty 0.23.1 (5916bbd5b3) created by Kovid Goyal from the output of ctrl+shift+f6 not the kitty version?

@kovidgoyal
Copy link
Owner

kovidgoyal commented Sep 15, 2021 via email

@kovidgoyal
Copy link
Owner

And @faho since you are around, any interest in integrating shell integration natively in fish. It will work with more than just kitty, at least iterm2 and wezterm also support it.

@faho
Copy link

faho commented Sep 15, 2021

Or are
there any other footguns I'm missing?

string collect will trim any number of trailing newlines, so if you want to have a prompt that leaves you on a newline, e.g.

function fish_prompt
   echo $PWD '>'
   echo
end

it won't work (this should possibly be changed, but at the moment that's the way it is).

Really, I think writing the last line out separately is the best choice here.

And @faho since you are around, any interest in integrating shell integration natively in fish. It will work with more than just kitty, at least iterm2 and wezterm also support it.

I'm okay with adding a lot of stuff, by default. However, I've explained my conditions before:

  • Either it has to not break everywhere (including awful things like emacs' ansi-term), or
  • where it works has to be trivially detectable (e.g. TERM is xterm-kitty, ITERM_PROFILE is set, whatever wezterm does)
  • The sequence should ideally have the same form everywhere, no ; in that term and : in that other one

If there's gonna be a bunch of these sequences, there could even just be a $NUTERM variable that y'all could set, and it would guarantee to fish that these things would be safe to send.

I've really lost my appetite for complicated terminal detection and bug reports for poorly behaving terminals, so if we're gonna add something I want it to not cause more problems.

If that's given, sure, send the docs or (better) a patch.

@faho
Copy link

faho commented Sep 15, 2021

In case you're only reading email: I've edited in how to write it out in my comment before.

@kovidgoyal
Copy link
Owner

My apologies, is kitty 0.23.1 (5916bbd5b3) created by Kovid Goyal from the output of ctrl+shift+f6 not the kitty version?

It is but I havent memorized commit hashes, so when you are running from master its best to mention that fact or I wont notice.

@kovidgoyal
Copy link
Owner

kovidgoyal commented Sep 15, 2021 via email

@kovidgoyal
Copy link
Owner

kovidgoyal commented Sep 15, 2021

And using

    function fish_prompt
        echo one
        echo -e "\e[33mXXXX"
        echo
        echo
    end

gives a prompt of:

one
XXXX

<cursor>

No inserted spaces on the last line. So to summarise, with two trailing newlines, one gets inserted spaces on the last line, with three or more one does not.

EDIT: I am guessing this is because ith three the penultimate line has no printable characters. So fish's prompt algorithm seems to be:

  1. Remove one trailing newline
  2. If there is still a trailing newline, replace it with a cursor down command?

@faho
Copy link

faho commented Sep 15, 2021

Ah, okay. I can reproduce that, even without an escape. It seems we're mistakenly starting at the last line's width instead of the beginning of the line in that case.

It's a fish bug that I don't think you need to concern yourself with.

kovidgoyal added a commit that referenced this issue Sep 15, 2021
@kovidgoyal
Copy link
Owner

OK then thanks for your help I have committed the pure printf based
solution: f277cbf

faho added a commit to fish-shell/fish-shell that referenced this issue Sep 15, 2021
This makes us start drawing the commandline at the beginning of the
line again.

See kovidgoyal/kitty#4032 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants