Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSuggest: offer default suggestion #4

Merged
merged 2 commits into from Aug 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions WebSuggest/websuggest.ini
Expand Up @@ -37,6 +37,12 @@
# * Default: yes
#enable_predefined_items = yes

# Time that the plugin will wait before sending the request.
# * Time in seconds (can be used with float type)
# * The minimum value is 0.25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specify a hard-coded maximum value to prevent mistyping. 3 seconds should do it I think

# * Default: 0.25
#waiting_time = 0.25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think idle_time is more appropriate than waiting_time



[predefined_item/Amazon]
provider = amazon
Expand Down
46 changes: 34 additions & 12 deletions WebSuggest/websuggest.py
Expand Up @@ -238,13 +238,15 @@ class WebSuggest(kp.Plugin):

DEFAULT_ENABLE_PREDEFINED_PROVIDERS = True
DEFAULT_ENABLE_PREDEFINED_ITEMS = True
DEFAULT_WAITING_TIME = 0.25
DEFAULT_ACTION = ACTION_BROWSE

actions_names = []
default_icon = None
icons = {}
providers = {}
profiles = {}
waiting_time = DEFAULT_WAITING_TIME

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -307,11 +309,18 @@ def on_suggest(self, user_input, items_chain):
self.warn('Item definition not found in current config: "{}"'.format(profile_name))
return

default_item = current_item.clone()
default_item.set_args(user_input)
if not user_input:
default_item.set_short_desc("Open the search engine home page")

suggestions = [default_item]

# avoid doing unnecessary network requests in case user is still typing
if len(user_input) < 2 or self.should_terminate(0.25):
if len(user_input) < 2 or self.should_terminate(self.waiting_time):
self.set_suggestions(suggestions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_suggestions() call is useless here since suggestions will be discarded by KP anyways

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the case of entering one character. But I agree that this is unnecessary. Nobody is looking for 1 character. :)

return

suggestions = []
provider_suggestions = []

try:
Expand All @@ -332,19 +341,33 @@ def on_suggest(self, user_input, items_chain):
#item.set_data_bag(user_input)
suggestions.append(item)

if suggestions:
self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
if not provider_suggestions: # change default item
suggestions[0].set_short_desc("No suggestions found (default action: {})".format(
profile['default_action']))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent to align with above parenthesis. profile[... should be aligned with "No suggestions ... (I know it may not be the case everywhere in the repo)


self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)

def on_execute(self, item, action):
target_props = kpu.kwargs_decode(item.target())
profile_name = target_props['profile']
args = item.raw_args()

try:
profile = self.profiles[profile_name]
except KeyError:
self.warn('Item definition not found in current config: "{}"'.format(profile_name))
return

if not args: # open the search engine home page
base = profile['provider'].browse_base
try:
parts = urllib.parse.urlsplit(base)
url = '{}://{}'.format(parts.scheme, parts.netloc)
except ValueError:
url = base
kpu.web_browser_command(url=url, execute=True)
return

# choose action
action_name = action.name() if action else None
if not action_name:
Expand All @@ -360,24 +383,20 @@ def on_execute(self, item, action):
# browse or copy url
if action_name in (self.ACTION_BROWSE, self.ACTION_BROWSE_PRIVATE,
self.ACTION_COPY_URL):
url = profile['provider'].build_browse_url(item.raw_args())
url = profile['provider'].build_browse_url(args)

# copy url
if action_name == self.ACTION_COPY_URL:
kpu.set_clipboard(url)

# launch browser
else:
if action_name == self.ACTION_BROWSE_PRIVATE:
private_mode = True
else:
private_mode = None
kpu.web_browser_command(private_mode=private_mode, url=url,
execute=True)
private_mode = True if action_name == self.ACTION_BROWSE_PRIVATE else None
kpu.web_browser_command(private_mode=private_mode, url=url, execute=True)

# default action: copy result (ACTION_COPY_RESULT)
else:
kpu.set_clipboard(item.raw_args())
kpu.set_clipboard(args)

def on_events(self, flags):
if flags & (kp.Events.APPCONFIG | kp.Events.PACKCONFIG |
Expand Down Expand Up @@ -405,6 +424,9 @@ def _read_config(self):
enable_predefined_items = settings.get_bool(
"enable_predefined_items", self.CONFIG_SECTION_MAIN,
fallback=self.DEFAULT_ENABLE_PREDEFINED_ITEMS)
self.waiting_time = settings.get_float(
"waiting_time", self.CONFIG_SECTION_MAIN,
fallback=self.DEFAULT_WAITING_TIME, min=0.25)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(max value)


# [predefined_provider/*] and [provider/*] sections
for section in settings.sections():
Expand Down