antirez / antirez/linenoise

Bug: Cursor moves far to the right if prompt has colors

未關閉
#158 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
C
星號
4.4k
分支
741
PR 合併指標
30 天內沒有已合併 PR

描述

If the prompt contains a color code, the cursor moves far to the right.
This is probably due to the fact that `plen` would include each char in the color code even though they have no bearing on cursor position (since they aren't printed to the screen like a char).

This is reproduce-able with this modified code:
`#ifndef NO_COLOR`

` #define RED "\x1B[31m"`

` #define WHITE "\x1B[37m"`

` #define RESET "\x1B[0m"`

`#else`

` #define RED ""`

` #define WHITE ""`

` #define RESET ""`

`#endif`

`char prompt[255] = {'\0'};`

` snprintf(prompt, 255, "%sexample%s@%sexample%s$ ", WHITE, RED, WHITE, RESET);`

If you add that code to a program and call `linenoise(prompt)` the cursor moves over 20pts to the right but displays text correctly.

If you compile the same program with the `-DNO_COLOR` flag then the issue does not happen

I traced the cursor move to this line:
https://github.com/antirez/linenoise/blob/4a961c0108720741e2683868eb10495f015ee422/linenoise.c#L537

The only work-around I found is to place two prompts (one above the real one) so at least part of it is in color.

Here is a picture comparing the two, note the white cursor on the left

![2018-08-06-042855_1366x768_scrot](https://user-images.githubusercontent.com/4129165/43705727-62e1fb9c-9931-11e8-8404-0db76df51ead.png)

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。