junegunn / junegunn/fzf

DISABLE_NEWLINE_AUTO_RETURN is not rest in some cases

Open
#3,334 3 comments 0 reactions 0 assignees View on GitHub
help-needed windows
Dominant language
Go
Stars
83k
Forks
2.9k
Avg merge
17h 8m
Merged PRs (30d)
10

Description

- [x] I have read through the manual page (`man fzf`)
- [x] I have the latest version of fzf (0.41.1, latest available in chocolatey)
- [x] I have searched through the existing issues

## Info

- OS
- [x] Windows
- Shell
- [x] pwsh

## Problem / Steps to reproduce
Occasionally after an `fzf` call (that involves showing preview) the console (I repro'd this on Windows Terminal) enters some strange mode when it starts to interpret literally the carriage return `CR` chars and makes them distinct from `LF` chars:

![image](https://github.com/junegunn/fzf/assets/711295/203d5d88-a5fb-4723-bc9d-364068bc558c)

After checking with Windows Terminal / Powershell / Console team it turned out that a wrong ConsoleMode is responsible for that kind of behavior. When I test it on a new tab the mode value is 7, but on a broken one it is 15:

```ps1
$Kernel32 = Add-Type -Name 'Kernel32' -Namespace 'Win32' -PassThru -MemberDefinition '[DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetStdHandle(int nStdHandle); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint lpMode);'
[uint] $mode = 0
$Kernel32::GetConsoleMode($Kernel32::GetStdHandle(-11), [ref]$mode) | Out-Null; $mode
```

Setting the ConsoleMode back to 7 fixes the console:

```ps1
$Kernel32::SetConsoleMode($Kernel32::GetStdHandle(-11), 0x7) | Out-Null
```

The difference in flags corresponds to [DISABLE_NEWLINE_AUTO_RETURN](https://learn.microsoft.com/en-us/windows/console/getconsolemode?ranMID=24542&ranEAID=mP6UMnc5Ozo&ranSiteID=mP6UMnc5Ozo-vPOdyuBO4o_aFRa2bzi4aA&epi=mP6UMnc5Ozo-vPOdyuBO4o_aFRa2bzi4aA&irgwc=1&OCID=AID2200057_aff_7593_1243925&tduid=(ir__9uoj6w3ewkkfbwge9grfpbvh9f2xewcwxurdc0uk00)(7593)(1243925)(mP6UMnc5Ozo-vPOdyuBO4o_aFRa2bzi4aA)()&irclickid=_9uoj6w3ewkkfbwge9grfpbvh9f2xewcwxurdc0uk00) flag that fzf [initializes here](https://github.com/search?q=repo%3Ajunegunn%2Ffzf%20DISABLE_NEWLINE_AUTO_RETURN&type=code) and [sets here](https://github.com/search?q=repo%3Ajunegunn%2Ffzf+consoleFlagsOutput&type=code).

My guess is that there are some conditions when `fzf` sets the DISABLE_NEWLINE_AUTO_RETURN flag and then forgets to cleanup. And this makes the console almost unusable. Plus it is very hard to pin point what is wrong here (I had this problem for months and then accidentally learned the contacts of the Windows Terminal / Powershell / Console team who knew what can cause it).

Windows Terminal has a [long-term work item](https://github.com/microsoft/terminal/issues/4954) to keep the ConsoleMode internally by itself, but this would not be implemented anytime soon.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the fzf code locations linked in the issue's searches for DISABLE_NEWLINE_AUTO_RETURN and consoleFlagsOutput. Reproduce the preview case in PowerShell on Windows Terminal and compare console mode values before and after the call. Done means every relevant exit path restores the console mode so the flag does not remain set.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, powershell
Domain
cli, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.