11from __future__ import annotations
22
3- import shutil
43import sys
5- from typing import Callable , NoReturn , cast
6-
7- import click
8- import inquirer
4+ from typing import NoReturn
95
106from codeflash .cli_cmds .console import console , logger
117
@@ -18,80 +14,3 @@ def apologize_and_exit() -> NoReturn:
1814 console .rule ()
1915 logger .info ("👋 Exiting..." )
2016 sys .exit (1 )
21-
22-
23- def inquirer_wrapper (func : Callable [..., str | bool ], * args : str | bool , ** kwargs : str | bool ) -> str | bool :
24- new_args = []
25- new_kwargs = {}
26-
27- if len (args ) == 1 :
28- message = str (args [0 ])
29- else :
30- message = str (kwargs ["message" ])
31- new_kwargs = kwargs .copy ()
32- split_messages = split_string_to_cli_width (message , is_confirm = func == inquirer .confirm )
33- for split_message in split_messages [:- 1 ]:
34- click .echo (split_message )
35-
36- last_message = split_messages [- 1 ]
37-
38- if len (args ) == 1 :
39- new_args .append (last_message )
40- else :
41- new_kwargs ["message" ] = last_message
42-
43- return func (* new_args , ** new_kwargs )
44-
45-
46- def split_string_to_cli_width (string : str , is_confirm : bool = False ) -> list [str ]:
47- cli_width , _ = shutil .get_terminal_size ()
48- # split string to lines that accommodate "[?] " prefix
49- cli_width -= len ("[?] " )
50- lines = split_string_to_fit_width (string , cli_width )
51-
52- # split last line to additionally accommodate ": " or " (y/N): " suffix
53- cli_width -= len (" (y/N):" ) if is_confirm else len (": " )
54- last_lines = split_string_to_fit_width (lines [- 1 ], cli_width )
55-
56- lines = lines [:- 1 ] + last_lines
57-
58- if len (lines ) > 1 :
59- for i in range (len (lines [:- 1 ])):
60- # Add yellow color to question mark in "[?] " prefix
61- lines [i ] = "[\033 [33m?\033 [0m] " + lines [i ]
62- return lines
63-
64-
65- def inquirer_wrapper_path (* args : str , ** kwargs : str ) -> dict [str , str ] | None :
66- new_args = []
67- message = kwargs ["message" ]
68- new_kwargs = kwargs .copy ()
69- split_messages = split_string_to_cli_width (message )
70- for split_message in split_messages [:- 1 ]:
71- click .echo (split_message )
72-
73- last_message = split_messages [- 1 ]
74- new_kwargs ["message" ] = last_message
75- new_args .append (args [0 ])
76-
77- return cast ("dict[str, str]" , inquirer .prompt ([inquirer .Path (* new_args , ** new_kwargs )]))
78-
79-
80- def split_string_to_fit_width (string : str , width : int ) -> list [str ]:
81- words = string .split ()
82- lines = []
83- current_line = [words [0 ]]
84- current_length = len (words [0 ])
85-
86- for word in words [1 :]:
87- word_length = len (word )
88- if current_length + word_length + 1 <= width :
89- current_line .append (word )
90- current_length += word_length + 1
91- else :
92- lines .append (" " .join (current_line ))
93- current_line = [word ]
94- current_length = word_length
95-
96- lines .append (" " .join (current_line ))
97- return lines
0 commit comments