4040from .key_bindings import cli_bindings
4141from .lexer import LiteCliLexer
4242from .packages import special
43+ from .packages .dot_output import format_dot_output
4344from .packages .filepaths import dir_path_exists
4445from .packages .prompt_utils import confirm , confirm_destructive_query
4546from .packages .special .main import NO_QUERY
@@ -60,6 +61,7 @@ def _load_sqlite3() -> Any:
6061_sqlite3 = _load_sqlite3 ()
6162OperationalError = _sqlite3 .OperationalError
6263sqlite_version = _sqlite3 .sqlite_version
64+ LOCAL_OUTPUT_FORMATS = ("dot" ,)
6365
6466# Query tuples are used for maintaining history
6567Query = namedtuple ("Query" , ["query" , "successful" , "mutating" ])
@@ -89,7 +91,11 @@ def __init__(
8991 self .multi_line = c ["main" ].as_bool ("multi_line" )
9092 self .key_bindings = c ["main" ]["key_bindings" ]
9193 special .set_favorite_queries (self .config )
92- self .formatter = TabularOutputFormatter (format_name = c ["main" ]["table_format" ])
94+ self .local_format_name : str | None = None
95+ config_table_format = c ["main" ]["table_format" ]
96+ self .formatter = TabularOutputFormatter (format_name = "ascii" if config_table_format in LOCAL_OUTPUT_FORMATS else config_table_format )
97+ if config_table_format in LOCAL_OUTPUT_FORMATS :
98+ self .local_format_name = config_table_format
9399 # self.formatter.litecli = self, ty raises unresolved-attribute, hence use dynamic assignment
94100 setattr (self .formatter , "litecli" , self )
95101 self .syntax_style = c ["main" ]["syntax_style" ]
@@ -137,7 +143,7 @@ def __init__(
137143
138144 # Initialize completer.
139145 self .completer = SQLCompleter (
140- supported_formats = self .formatter . supported_formats ,
146+ supported_formats = self .supported_table_formats () ,
141147 keyword_casing = keyword_casing ,
142148 )
143149 self ._completer_lock = threading .Lock ()
@@ -188,13 +194,31 @@ def register_special_commands(self) -> None:
188194 case_sensitive = True ,
189195 )
190196
197+ def supported_table_formats (self ) -> list [str ]:
198+ supported_formats = list (self .formatter .supported_formats )
199+ for format_name in LOCAL_OUTPUT_FORMATS :
200+ if format_name not in supported_formats :
201+ supported_formats .append (format_name )
202+ return supported_formats
203+
204+ def current_table_format (self ) -> str :
205+ return self .local_format_name or self .formatter .format_name
206+
207+ def set_table_format (self , format_name : str ) -> None :
208+ if format_name in LOCAL_OUTPUT_FORMATS :
209+ self .local_format_name = format_name
210+ return
211+
212+ self .formatter .format_name = format_name
213+ self .local_format_name = None
214+
191215 def change_table_format (self , arg : str , ** _ : Any ) -> Generator [tuple [None , None , None , str ], None , None ]:
192216 try :
193- self .formatter . format_name = arg
217+ self .set_table_format ( arg )
194218 yield (None , None , None , "Changed table format to {}" .format (arg ))
195219 except ValueError :
196220 msg = "Table format {} not recognized. Allowed formats:" .format (arg )
197- for table_type in self .formatter . supported_formats :
221+ for table_type in self .supported_table_formats () :
198222 msg += "\n \t {}" .format (table_type )
199223 yield (None , None , None , msg )
200224
@@ -839,7 +863,8 @@ def run_query(self, query: str, new_line: bool = True) -> None:
839863 click .echo (line , nl = new_line )
840864
841865 def format_output (self , title : Any , cur : Any , headers : Any , expanded : bool = False , max_width : int | None = None ) -> Iterable [str ]:
842- expanded = expanded or self .formatter .format_name == "vertical"
866+ format_name = self .current_table_format ()
867+ expanded = expanded or format_name == "vertical"
843868 output_iter : Iterable [str ] = []
844869
845870 output_kwargs = {
@@ -854,6 +879,9 @@ def format_output(self, title: Any, cur: Any, headers: Any, expanded: bool = Fal
854879 output_iter = itertools .chain (output_iter , [title ])
855880
856881 if cur :
882+ if format_name == "dot" :
883+ return itertools .chain (output_iter , format_dot_output (cur , headers or []))
884+
857885 column_types = None
858886 if hasattr (cur , "description" ):
859887 column_types = [str (col ) for col in cur .description ]
@@ -972,9 +1000,9 @@ def cli(
9721000 if execute :
9731001 try :
9741002 if csv :
975- litecli .formatter . format_name = "csv"
1003+ litecli .set_table_format ( "csv" )
9761004 elif not table :
977- litecli .formatter . format_name = "tsv"
1005+ litecli .set_table_format ( "tsv" )
9781006
9791007 litecli .run_query (execute )
9801008 exit (0 )
@@ -999,9 +1027,9 @@ def cli(
9991027 new_line = True
10001028
10011029 if csv :
1002- litecli .formatter . format_name = "csv"
1030+ litecli .set_table_format ( "csv" )
10031031 elif not table :
1004- litecli .formatter . format_name = "tsv"
1032+ litecli .set_table_format ( "tsv" )
10051033
10061034 litecli .run_query (stdin_text , new_line = new_line )
10071035 exit (0 )
0 commit comments