Skip to content

Commit

Permalink
Renamed tab with "Discovery"
Browse files Browse the repository at this point in the history
- changed copy behavior to show only a msg
- improved tdlr heuristic
  • Loading branch information
mkcn committed Dec 11, 2021
1 parent 3b7b3c4 commit 44c52bf
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 91 deletions.
2 changes: 1 addition & 1 deletion fastHistory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def handle_arguments(logger_console, config_reader, path_data_folder, path_code_
elif arg1 == "--add-explicit" and args_len == 3:
input_cmd = str(sys.argv[2]).strip()
handle_add_request(logger_console, input_cmd, path_data_folder, error_feedback=False)
elif arg1 == "-t" or arg1 == "--tldr":
elif arg1 == "-f" or arg1 == "-d" or arg1 == "--discover":
input_cmd = retrieve_parameters_from_bash_hook(arg1=arg1)
handle_search_request(logger_console, input_cmd, path_data_folder, config_reader.get_theme(),
config_reader.get_last_column_size(), is_tldr_search=True)
Expand Down
2 changes: 1 addition & 1 deletion fastHistory/console/consoleHelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
OPTIONS
-a, --add <command> # add command (same as '# <command_to_save> #')
-f, --find, --tldr start directly with a TLDR search
-f, --d, --discover start directly with a Discovery search using TLDR pages
--config change configuration file
--export [<output_name>] export database with all stored commands
-h, --help show this help message
Expand Down
7 changes: 5 additions & 2 deletions fastHistory/console/consoleUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ def paste_into_terminal(data):
return [False, "your terminal does not support auto-paste\ncopy-to-clipboard failed too with the following message:\n\t%s\nplease manually copy and use the following command:\n\t%s" % (res[1], data)]

@staticmethod
def copy_to_clipboard(data):
def copy_to_clipboard(data, show_data_in_msg=True):
try:
import pyperclip
pyperclip.copy(data)
return [True, "copied to clipboard: %s" % data]
if show_data_in_msg:
return [True, "copied! %s" % data]
else:
return [True, "copied!"]
except ImportError:
return [False, "pyperclip module not found (to install it run 'pip3 install pyperclip')"]
except Exception as e:
Expand Down
34 changes: 23 additions & 11 deletions fastHistory/pick/loopSelectFavourites.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from fastHistory import DataManager
from fastHistory import DataManager, ConsoleUtils
from fastHistory.pick.keys import Keys
from fastHistory.pick.loopInfo import LoopInfo
from fastHistory.pick.pageSelectFavourites import PageSelectFavourites
Expand Down Expand Up @@ -32,6 +32,7 @@ def run_loop_select(self):
"""
# get filtered starting options
msg_to_show = None
self.options = self.data_manager.filter(self.search_t.get_text_lower(), self.get_number_options_to_draw())
self.initialize_options_to_draw()

Expand All @@ -42,7 +43,10 @@ def run_loop_select(self):
options=self.get_options(),
search_t=self.search_t,
context_shift=self.context_shift,
last_column_size=self.last_column_size)
last_column_size=self.last_column_size,
msg_to_show=msg_to_show)
if msg_to_show:
msg_to_show = None

# wait for char
c = self.drawer.wait_next_char()
Expand All @@ -60,9 +64,13 @@ def run_loop_select(self):
self.search_t.get_text_lower(),
self.index + self.get_number_options_to_draw())
elif c in Keys.KEYS_ENTER:
return [True, [True, self.get_selected_and_update_db()]]
selected_cmd = self.get_selected_and_update_db()
if selected_cmd:
return [True, [True, selected_cmd]]
elif c == Keys.KEY_CTRL_SPACE:
return [True, [False, self.get_selected_and_update_db()]]
selected_cmd_to_copy = self.get_selected_and_update_db()
if selected_cmd_to_copy:
msg_to_show = ConsoleUtils.copy_to_clipboard(selected_cmd_to_copy, show_data_in_msg=False)[1]
# note: currently not implemented
elif c == Keys.KEY_SELECT and self.multi_select:
self.mark_index()
Expand All @@ -82,8 +90,13 @@ def run_loop_select(self):
if res[0]:
if res[1] == "select":
return [True, [True, self.get_selected_and_update_db()]]
else: # res[1] == "copy":
return [True, [False, self.get_selected_and_update_db()]]
elif res[1] == "copy":
selected_cmd_to_copy = self.get_selected_and_update_db()
if selected_cmd_to_copy:
msg_to_show = ConsoleUtils.copy_to_clipboard(selected_cmd_to_copy, show_data_in_msg=False)[1]
break
else:
pass
else:
if res[1] == "update":
self.loop_select_options_reload()
Expand All @@ -98,12 +111,11 @@ def run_loop_select(self):
else: # res[1] == None
break
# search TLDR
elif c == Keys.KEY_CTRL_T:
elif c == Keys.KEY_CTRL_D or c == Keys.KEY_CTRL_F:
return [False, 2]
elif c == Keys.KEY_CTRL_R:
# TO IMPLEMENT this will open a bash history tab
# TODO IMPLEMENT this will open a bash history tab
elif c == Keys.KEY_CTRL_R:
pass
return [False, 1]
# -> command
elif c == Keys.KEY_RIGHT:
if self.search_t.is_cursor_at_the_end():
Expand Down Expand Up @@ -279,7 +291,7 @@ def get_selected_and_update_db(self):
"""
option_count = len(self.options)
if option_count == 0 or self.index >= option_count or self.index < 0:
return ""
return None
selected_cmd = self.options[self.index][DataManager.OPTION.INDEX_CMD]
# move selected command on top
self.data_manager.update_selected_element_order(selected_cmd)
Expand Down
38 changes: 25 additions & 13 deletions fastHistory/pick/loopSelectTLDR.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from fastHistory import ConsoleUtils
from fastHistory.console import consoleUtils
from fastHistory.parser.InputData import InputData
from fastHistory.parser.inputParser import InputParser
from fastHistory.pick.textManager import ContextShifter
Expand Down Expand Up @@ -45,6 +46,7 @@ def run_loop_tldr(self, cached_in_memory_pages):
tldr_options_waiting = True
tldr_examples_reload_needed = True
tldr_ui_reload = True
msg_to_show = None

page_tldr_search = PageSelectTLDR(self.drawer)

Expand Down Expand Up @@ -92,7 +94,11 @@ def run_loop_tldr(self, cached_in_memory_pages):
example_content_shift=self.example_content_shift,
focus_area=self.focus,
has_url_more_info=self.tldr_examples.has_url_more_info(),
is_waiting=tldr_options_waiting)
is_waiting=tldr_options_waiting,
msg_to_show=msg_to_show)

if msg_to_show:
msg_to_show = None

# wait for char
c = self.drawer.wait_next_char(multi_threading_mode=tldr_parser_thread.is_alive())
Expand All @@ -103,20 +109,23 @@ def run_loop_tldr(self, cached_in_memory_pages):
tldr_ui_reload = False
continue
elif c in Keys.KEYS_ENTER:
res = self.get_selected_example(search_input=input_data)
if res:
return [True, res]
selected_example = self.get_selected_example(search_input=input_data)
if selected_example:
return [True, [True, selected_example]]
elif c == Keys.KEY_CTRL_SPACE:
res = self.get_selected_example(search_input=input_data, copied=True)
if res:
return [True, res]
example_to_copy = self.get_selected_example(search_input=input_data, copied=True)
if example_to_copy:
msg_to_show = ConsoleUtils.copy_to_clipboard(example_to_copy, show_data_in_msg=False)[1]
elif c == Keys.KEY_CTRL_L:
if self.tldr_examples.has_url_more_info():
return [True, [False, self.tldr_examples.get_url_more_info()]]
msg_to_show = ConsoleUtils.copy_to_clipboard(self.tldr_examples.get_url_more_info())[1]
elif c == Keys.KEY_CTRL_E:
res = ConsoleUtils.copy_to_clipboard(self.tldr_examples.get_tldr_github_page())
msg_to_show = res[1]
elif c == Keys.KEY_TAB:
self.flip_focus()
# go back to main page
elif c == Keys.KEY_CTRL_F or c == Keys.KEY_CTRL_T or c == Keys.KEY_ESC:
elif c == Keys.KEY_CTRL_F or c == Keys.KEY_CTRL_D or c == Keys.KEY_ESC:
return [False, 0]
elif c == Keys.KEY_UP:
if self.move_up():
Expand Down Expand Up @@ -159,6 +168,12 @@ def run_loop_tldr(self, cached_in_memory_pages):
logging.error("input not handled: %s" % repr(c))

def get_selected_example(self, search_input: InputData, copied: bool = False):
"""
if copied is false, it adds all the search words as tags (es. selected_command #word1 #word2)
:param search_input:
:param copied:
:return:
"""
res = self.tldr_examples.get_current_selected_example(self.tldr_examples_index)
if res is None:
return None
Expand All @@ -172,10 +187,7 @@ def get_selected_example(self, search_input: InputData, copied: bool = False):
res += ending

if self.focus == PageSelectTLDR.Focus.AREA_EXAMPLES:
if not copied:
return [True, res]
else:
return [False, res]
return res
else:
self.focus = PageSelectTLDR.Focus.AREA_EXAMPLES
self.search_field.move_cursor_to_end()
Expand Down
8 changes: 6 additions & 2 deletions fastHistory/pick/pageGeneric.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class PageGeneric(object):
CHAR_EDIT = 'E'

TITLE_DEFAULT = "fastHistory"
TAB_NAME_TLDR = " TLDR "
TAB_NAME_MY_LIST = " My list "
TAB_NAME_TLDR = " Discover "
TAB_NAME_MY_LIST = " My list "

INDEX_SECTION_VALUE = 0
INDEX_SECTION_IS_MARKED = 1
Expand Down Expand Up @@ -284,3 +284,7 @@ def find_sections_to_mark(string, words_to_mark, case_sensitive=False, recursive
sections.append([string[previous_index:], current_value == marked])
return sections

def draw_msg_to_show(self, msg_to_show):
self.drawer.set_y(self.drawer.get_max_y() - 1)
self.drawer.fill_row(x=0, color=self.drawer.color_columns_title, max_x=self.drawer.get_max_x() - 1)
self.drawer.draw_row(msg_to_show, x=2, color=self.drawer.color_columns_title, allow_last_row=True)
34 changes: 20 additions & 14 deletions fastHistory/pick/pageSelectFavourites.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PageSelectFavourites(PageGeneric):
def __init__(self, drawer):
PageGeneric.__init__(self, drawer)

def draw_page(self, search_filters, options, search_t, context_shift, last_column_size):
def draw_page(self, search_filters, options, search_t, context_shift, last_column_size, msg_to_show=None):
"""
draw page where the user can select the command
Expand Down Expand Up @@ -118,9 +118,11 @@ def draw_page(self, search_filters, options, search_t, context_shift, last_colum
selected=selected,
context_shift=context_shift,
last_column_size=index_tab_column)

# help line in the last line
self._draw_help_line_selector()
# last line
if msg_to_show:
self.draw_msg_to_show(msg_to_show)
else:
self._draw_help_line_selector()

# cursor set position
self.drawer.show_cursor()
Expand All @@ -136,7 +138,9 @@ def draw_no_result(self, search_filters):
:return:
"""
msg_no_result = "no result"
msg_try_tldr = "try TDLR search (ctrl+t)"
msg_try_tldr_1 = "use "
msg_try_tldr_2 = "ctrl+d"
msg_try_tldr_3 = " to Discover more commands"
if search_filters.is_advanced():
shift = 3
else:
Expand All @@ -145,11 +149,13 @@ def draw_no_result(self, search_filters):
for y in range(int(self.drawer.get_max_y()/2 - shift)):
self.drawer.new_line()
msg_no_result_space = int(self.drawer.get_max_x()/2 - len(msg_no_result)/2 - 1)
msg_try_tldr_space = int(self.drawer.get_max_x()/2 - len(msg_try_tldr)/2 - 1)
msg_try_tldr_space = int(self.drawer.get_max_x()/2 - len(msg_try_tldr_1 + msg_try_tldr_2 + msg_try_tldr_3)/2 - 1)

self.drawer.draw_row(msg_no_result, x=msg_no_result_space)
self.drawer.new_line()
self.drawer.draw_row(msg_try_tldr, x=msg_try_tldr_space)
self.drawer.draw_row(msg_try_tldr_1, x=msg_try_tldr_space)
self.drawer.draw_row(msg_try_tldr_2, color=self.drawer.color_columns_title)
self.drawer.draw_row(msg_try_tldr_3)
self.drawer.new_line()
self.drawer.new_line()

Expand All @@ -168,20 +174,20 @@ def draw_no_result(self, search_filters):

def _draw_help_line_selector(self):
self.drawer.set_y(self.drawer.get_max_y() - 1)
self.drawer.draw_row("Enter", x_indent=2, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Enter", x_indent=1, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Select", x_indent=1, allow_last_row=True)

self.drawer.draw_row("Ctrl+space", x_indent=2, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Ctrl+space", x_indent=1, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Copy", x_indent=1, allow_last_row=True)

self.drawer.draw_row("Tab", x_indent=2, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Tab", x_indent=1, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("More", x_indent=1, allow_last_row=True)

self.drawer.draw_row("Ctrl+t", x_indent=2, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("TLDR", x_indent=1, allow_last_row=True)
self.drawer.draw_row("Ctrl+d", x_indent=1, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Discover", x_indent=1, allow_last_row=True)

self.drawer.draw_row("Del", x_indent=2, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Del", x_indent=1, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Delete", x_indent=1, allow_last_row=True)

self.drawer.draw_row("Ctrl+c", x_indent=2, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Ctrl+c", x_indent=1, color=self.drawer.color_columns_title, allow_last_row=True)
self.drawer.draw_row("Exit", x_indent=1, allow_last_row=True)
Loading

0 comments on commit 44c52bf

Please sign in to comment.