Skip to content

Commit 9f98e70

Browse files
committed
feat: Add ThreadedCompleter support
fix: Update __version__ fix: Improve history completion efficiency fix: Enhance command-line interface options fix: Modify stdout data formatting
1 parent a46648d commit 9f98e70

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

manager.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
# prompt toolkit
1919
from prompt_toolkit import PromptSession
20-
from prompt_toolkit.completion import Completer, Completion
20+
from prompt_toolkit.completion import Completer, Completion, ThreadedCompleter
2121
from prompt_toolkit.document import Document
2222
from prompt_toolkit import PromptSession
2323
from prompt_toolkit.history import FileHistory
2424

25-
__version__ = "0.0.1"
25+
__version__ = "0.0.2"
2626

2727
__author__ = "Smartwa"
2828

@@ -109,7 +109,7 @@ def get_completions(self, document: Document, complete_event):
109109
return
110110
text = document.text
111111
history = self.session.history.get_strings()
112-
for entry in reversed(history):
112+
for entry in reversed(list(set(history))):
113113
if entry.startswith(text):
114114
yield Completion(entry, start_position=-len(text))
115115

@@ -129,19 +129,23 @@ def __init__(
129129
disable_coloring,
130130
disable_suggestions,
131131
new_history_thread,
132+
json,
133+
color,
132134
):
133135
super().__init__()
134136
self.__start_time = time.time()
135137
self.__end_time = time.time()
136138
self.db_manager = Sqlite3Manager(db_path, auto_commit)
137139
self.disable_coloring = disable_coloring
140+
self.json = json
141+
self.color = color
138142
history_file_path = Path.home() / ".sqlite3-cli-manager-history.txt"
139143
if new_history_thread and history_file_path.exists():
140144
os.remove(history_file_path)
141145
history = FileHistory(history_file_path)
142146
self.completer_session = PromptSession(history=history)
143-
self.completer_session.completer = HistoryCompletions(
144-
self.completer_session, disable_suggestions
147+
self.completer_session.completer = ThreadedCompleter(
148+
HistoryCompletions(self.completer_session, disable_suggestions)
145149
)
146150

147151
@property
@@ -283,7 +287,7 @@ def do_sys(self, line):
283287
def do_tables(self, line):
284288
"""Show database tables"""
285289
success, tables = self.db_manager.tables()
286-
Commands.stdout_data(success, tables)
290+
Commands.stdout_data(success, tables, json=self.json, color=self.color)
287291

288292
@cli_error_handler
289293
def do_columns(self, line):
@@ -292,7 +296,7 @@ def do_columns(self, line):
292296
columns <table-name>"""
293297
if line:
294298
success, tables = self.db_manager.table_columns(line)
295-
Commands.stdout_data(success, tables)
299+
Commands.stdout_data(success, tables, json=self.json, color=self.color)
296300
else:
297301
click.secho("Table name is required.", fg="yellow")
298302

@@ -305,7 +309,8 @@ def default(self, line):
305309
else:
306310
self.__start_time = time.time()
307311
success, response = self.db_manager.execute_sql_command(line)
308-
Commands.stdout_data(success, response)
312+
Commands.stdout_data(success, response, json=self.json, color=self.color)
313+
self.__end_time = time.time()
309314

310315
def do_exit(self, line):
311316
"""Quit this program"""
@@ -397,6 +402,13 @@ def execute(database, sql, json, quiet):
397402
@staticmethod
398403
@click.command()
399404
@click.argument("database", type=click.Path(exists=True, dir_okay=False))
405+
@click.option(
406+
"-c",
407+
"--color",
408+
help="Results font color",
409+
default="cyan",
410+
)
411+
@click.option("-j", "--json", help="Stdout results in json format", is_flag=True)
400412
@click.option("-a", "--auto-commit", is_flag=True, help="Enable auto-commit")
401413
@click.option(
402414
"-C",
@@ -414,7 +426,13 @@ def execute(database, sql, json, quiet):
414426
"-N", "--new-history-thread", is_flag=True, help="Start a new history thread"
415427
)
416428
def interactive(
417-
database, auto_commit, disable_coloring, disable_suggestions, new_history_thread
429+
database,
430+
color,
431+
json,
432+
auto_commit,
433+
disable_coloring,
434+
disable_suggestions,
435+
new_history_thread,
418436
):
419437
"""Execute sql statements interactively"""
420438
main = Interactive(
@@ -423,6 +441,8 @@ def interactive(
423441
disable_coloring=disable_coloring,
424442
disable_suggestions=disable_suggestions,
425443
new_history_thread=new_history_thread,
444+
json=json,
445+
color=color,
426446
)
427447
main.cmdloop()
428448

0 commit comments

Comments
 (0)