From 7be6b280116d1bf2693b3c415a6b20839b92e1c7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 10 Aug 2022 20:10:17 +0530 Subject: [PATCH] Preserve blanks lines and per line formatting in the ask kitten for the message with choices --- kittens/ask/main.py | 12 ++++++++++-- kitty/boss.py | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/kittens/ask/main.py b/kittens/ask/main.py index 3c28a388e..c9e45eab4 100644 --- a/kittens/ask/main.py +++ b/kittens/ask/main.py @@ -2,6 +2,7 @@ # License: GPL v3 Copyright: 2018, Kovid Goyal import os +import re import sys from contextlib import suppress from typing import ( @@ -16,11 +17,12 @@ from kitty.typing import BossType, KeyEventType, TypedDict from kitty.utils import ScreenSize from ..tui.handler import Handler, result_handler -from ..tui.loop import Loop, MouseEvent +from ..tui.loop import Loop, MouseEvent, debug from ..tui.operations import MouseTracking, alternate_screen, styled if TYPE_CHECKING: import readline + debug else: readline = None @@ -198,6 +200,7 @@ class Choose(Handler): mouse_tracking = MouseTracking.buttons_only def __init__(self, cli_opts: AskCLIOptions) -> None: + self.prefix_style_pat = re.compile(r'(?:\x1b\[[^m]*?m)+') self.cli_opts = cli_opts self.choices: Dict[str, Choice] = {} self.clickable_ranges: Dict[str, List[Range]] = {} @@ -228,12 +231,17 @@ class Choose(Handler): self.cmd.set_cursor_visible(True) def draw_long_text(self, text: str) -> int: + if not text: + self.print('') + return 1 y = 0 width = self.screen_size.cols - 2 + m = self.prefix_style_pat.match(text) + prefix = m.group() if m else '' while text: t, text = truncate_at_space(text, width) t = t.strip() - self.print(' ' * extra_for(wcswidth(t), width), styled(t, bold=True), sep='') + self.print(' ' * extra_for(wcswidth(t), width), styled(prefix + t, bold=True), sep='') y += 1 return y diff --git a/kitty/boss.py b/kitty/boss.py index 7fa1c0f64..6a13ce15d 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -485,12 +485,12 @@ class Boss: wid = 0 if window is None else window.id overlay_window = self.choose( _('A program wishes to control kitty.\n' - 'Action: {1}\n' 'Password: {0}\n' '{2}' + 'Action: {1}\n' 'Password: {0}\n\n' '{2}' ).format( styled(pcmd['password'], fg='yellow'), styled(pcmd['cmd'], fg='magenta'), '\x1b[m' + styled(_( 'Note that allowing the password will allow all future actions using the same password, in this kitty instance.' - ), dim=True)), + ), dim=True, italic=True)), partial(self.remote_cmd_permission_received, pcmd, wid, peer_id), 'a;green:Allow request', 'p;yellow:Allow password', 'r;magenta:Deny request', 'd;red:Deny password', window=window, default='a'