4848)
4949from prompt_toolkit .history import FileHistory
5050from prompt_toolkit .auto_suggest import AutoSuggestFromHistory
51+ from prompt_toolkit .cursor_shapes import ModalCursorShapeConfig
5152from pygments .lexers .sql import PostgresLexer
5253
5354from pgspecial .main import PGSpecial , NO_QUERY , PAGER_OFF , PAGER_LONG_OUTPUT
@@ -208,6 +209,7 @@ def __init__(
208209 self .output_file = None
209210 self .pgspecial = PGSpecial ()
210211
212+ self .hide_named_query_text = "hide_named_query_text" in c ["main" ] and c ["main" ].as_bool ("hide_named_query_text" )
211213 self .explain_mode = False
212214 self .multi_line = c ["main" ].as_bool ("multi_line" )
213215 self .multiline_mode = c ["main" ].get ("multi_line_mode" , "psql" )
@@ -307,7 +309,28 @@ def __init__(
307309 def quit (self ):
308310 raise PgCliQuitError
309311
312+ def toggle_named_query_quiet (self ):
313+ """Toggle hiding of named query text"""
314+ self .hide_named_query_text = not self .hide_named_query_text
315+ status = "ON" if self .hide_named_query_text else "OFF"
316+ message = f"Named query quiet mode: { status } "
317+ return [(None , None , None , message )]
318+
319+ def _is_named_query_execution (self , text ):
320+ """Check if the command is a named query execution (\n <name>)."""
321+ text = text .strip ()
322+ return text .startswith ("\\ n " ) and not text .startswith ("\\ ns " ) and not text .startswith ("\\ nd " )
323+
310324 def register_special_commands (self ):
325+ self .pgspecial .register (
326+ self .toggle_named_query_quiet ,
327+ "\\ nq" ,
328+ "\\ nq" ,
329+ "Toggle named query quiet mode (hide query text)" ,
330+ arg_type = NO_QUERY ,
331+ case_sensitive = True ,
332+ )
333+
311334 self .pgspecial .register (
312335 self .change_db ,
313336 "\\ c" ,
@@ -828,7 +851,14 @@ def execute_command(self, text, handle_closed_connection=True):
828851 if self .output_file and not text .startswith (("\\ o " , "\\ log-file" , "\\ ? " , "\\ echo " )):
829852 try :
830853 with open (self .output_file , "a" , encoding = "utf-8" ) as f :
831- click .echo (text , file = f )
854+ should_hide = (
855+ self .hide_named_query_text
856+ and query .is_special
857+ and query .successful
858+ and self ._is_named_query_execution (text )
859+ )
860+ if not should_hide :
861+ click .echo (text , file = f )
832862 click .echo ("\n " .join (output ), file = f )
833863 click .echo ("" , file = f ) # extra newline
834864 except OSError as e :
@@ -842,7 +872,14 @@ def execute_command(self, text, handle_closed_connection=True):
842872 try :
843873 with open (self .log_file , "a" , encoding = "utf-8" ) as f :
844874 click .echo (dt .datetime .now ().isoformat (), file = f ) # timestamp log
845- click .echo (text , file = f )
875+ should_hide = (
876+ self .hide_named_query_text
877+ and query .is_special
878+ and query .successful
879+ and self ._is_named_query_execution (text )
880+ )
881+ if not should_hide :
882+ click .echo (text , file = f )
846883 click .echo ("\n " .join (output ), file = f )
847884 click .echo ("" , file = f ) # extra newline
848885 except OSError as e :
@@ -1070,6 +1107,7 @@ def get_continuation(width, line_number, is_soft_wrap):
10701107 enable_suspend = True ,
10711108 editing_mode = EditingMode .VI if self .vi_mode else EditingMode .EMACS ,
10721109 search_ignore_case = True ,
1110+ cursor = ModalCursorShapeConfig (),
10731111 )
10741112
10751113 return prompt_app
@@ -1155,6 +1193,18 @@ def _evaluate_command(self, text):
11551193 style_output = self .style_output ,
11561194 max_field_width = self .max_field_width ,
11571195 )
1196+
1197+ # Hide query text for named queries in quiet mode
1198+ if (
1199+ self .hide_named_query_text
1200+ and is_special
1201+ and success
1202+ and self ._is_named_query_execution (text )
1203+ and title
1204+ and title .startswith ("> " )
1205+ ):
1206+ title = None
1207+
11581208 execution = time () - start
11591209 formatted = format_output (title , cur , headers , status , settings , self .explain_mode )
11601210
@@ -1278,6 +1328,7 @@ def get_prompt(self, string):
12781328 string = string .replace ("\\ i" , str (self .pgexecute .pid ) or "(none)" )
12791329 string = string .replace ("\\ #" , "#" if self .pgexecute .superuser else ">" )
12801330 string = string .replace ("\\ n" , "\n " )
1331+ string = string .replace ("\\ T" , self .pgexecute .transaction_indicator )
12811332 return string
12821333
12831334 def get_last_query (self ):
0 commit comments