Skip to content

Commit

Permalink
Fixes and improvemens for #191
Browse files Browse the repository at this point in the history
  • Loading branch information
gornostal committed May 19, 2019
1 parent 36fc0ef commit e6af066
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 65 deletions.
111 changes: 60 additions & 51 deletions data/preferences/src/components/pages/EditShortcut.vue
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
<template>
<div class="page" v-if="prefsLoaded">
<b-media>
<div :class="{'icon-container': true, 'no-icon': !localIcon}" slot="aside" @click="selectIcon">
<div
:class="{'icon-container': true, 'no-icon': !localIcon}"
slot="aside"
@click="selectIcon"
>
<img v-if="localIcon" :src="expandUserPath(localIcon)">
</div>

<b-form-fieldset
label="Name"
:state="nameState"
>
<b-form-fieldset label="Name" :state="nameState">
<b-form-input class="name" v-model="localName"></b-form-input>
</b-form-fieldset>

<b-form-fieldset
label="Keyword"
:state="keywordState"
>
<b-form-fieldset label="Keyword" :state="keywordState">
<b-form-input class="keyword" v-model="localKeyword"></b-form-input>
</b-form-fieldset>

<b-form-fieldset
label="Query or Script"
:state="cmdState"
>
<b-form-fieldset label="Query or Script" :state="cmdState">
<b-form-input class="cmd" textarea :rows="3" v-model="localCmd"></b-form-input>
<small class="form-text text-muted">
<p>
Use <code>%s</code> as a placeholder for a query in URL or write a script in a language of your choice.
<a @click.prevent="cmdDescriptionExpanded = !cmdDescriptionExpanded"
href="">(toggle example)</a>
Use
<code>%s</code> as a placeholder for a query in URL or write a script in a language of your choice.
<a
@click.prevent="cmdDescriptionExpanded = !cmdDescriptionExpanded"
href
>(toggle example)</a>
</p>
<div v-if="cmdDescriptionExpanded">
<pre class="selectable"><code>#!/usr/bin/env node
Expand All @@ -39,23 +37,24 @@ console.log("Query is:", process.argv[1]);</code></pre>
</b-form-fieldset>

<b-form-fieldset>
<b-form-checkbox v-model="localIsDefaultSearch">
Default search (suggest this shortcut when no results found)
</b-form-checkbox>
<b-form-checkbox v-model="localIsDefaultSearch">Default search</b-form-checkbox>
<small class="form-text text-muted">
<p>Suggest this shortcut when no results found</p>
</small>
</b-form-fieldset>

<b-form-fieldset>
<b-form-checkbox v-model="localRunWithoutArgument">
Run without arguments
</b-form-checkbox>
<b-form-checkbox v-model="localRunWithoutArgument">Run without arguments</b-form-checkbox>
<small class="form-text text-muted">
<p>Allows you to type in a keyword and press Enter to run a shortcut</p>
</small>
</b-form-fieldset>

<b-button-toolbar>
<b-button class="save" variant="primary" href="" @click="save">Save</b-button>
<b-button class="cancel" variant="secondary" href="" @click="hide">Cancel</b-button>
<b-button class="save" variant="primary" href @click="save">Save</b-button>
<b-button class="cancel" variant="secondary" href @click="hide">Cancel</b-button>
</b-button-toolbar>
</b-media>

</div>
</template>

Expand All @@ -70,13 +69,13 @@ const shortcutIconEventName = 'shortcut-icon-event'
export default {
name: 'edit-shortcut',
props: ['id', 'icon', 'name', 'keyword', 'cmd', 'is_default_search', 'run_without_argument'],
created () {
created() {
bus.$on(shortcutIconEventName, this.onIconSelected)
},
beforeDestroy () {
beforeDestroy() {
bus.$off(shortcutIconEventName, this.onIconSelected)
},
data () {
data() {
return {
localIcon: this.icon,
localName: this.name,
Expand All @@ -91,28 +90,29 @@ export default {
computed: {
...mapState(['prefs']),
...mapGetters(['prefsLoaded']),
nameState () {
nameState() {
return this.validate && !this.localName ? 'danger' : ''
},
keywordState () {
keywordState() {
return this.validate && !this.localKeyword ? 'danger' : ''
},
cmdState () {
cmdState() {
return this.validate && !this.localCmd ? 'danger' : ''
}
},
methods: {
expandUserPath (path) {
expandUserPath(path) {
return path.indexOf('~') === 0 ? path.replace('~', this.prefs.env.user_home, 1) : path
},
selectIcon () {
jsonp('prefs://show/file-browser', {type: 'image', name: shortcutIconEventName})
.then(null, (err) => bus.$emit('error', err))
selectIcon() {
jsonp('prefs://show/file-browser', { type: 'image', name: shortcutIconEventName }).then(null, err =>
bus.$emit('error', err)
)
},
onIconSelected (data) {
onIconSelected(data) {
this.localIcon = data.value
},
save () {
save() {
this.validate = true
if (!this.localName || !this.localKeyword || !this.localCmd) {
return
Expand All @@ -128,24 +128,34 @@ export default {
run_without_argument: this.localRunWithoutArgument
}
let method = shortcut.id ? 'update' : 'add'
jsonp('prefs://shortcut/' + method, shortcut)
.then(this.hide, (err) => bus.$emit('error', err))
jsonp('prefs://shortcut/' + method, shortcut).then(this.hide, err => bus.$emit('error', err))
},
hide () {
this.$router.push({path: '/shortcuts'})
hide() {
this.$router.push({ path: '/shortcuts' })
}
}
}
</script>

<style lang="scss" scoped>
.page { padding: 15px }
.row {display: block;}
.name, .keyword { width: 400px }
.save, .cancel {
.page {
padding: 15px;
}
.row {
display: block;
}
.name,
.keyword {
width: 400px;
}
.save,
.cancel {
margin-right: 20px;
margin-top: 20px;
cursor: pointer
margin-top: 10px;
cursor: pointer;
}
fieldset p {
margin-bottom: 4px;
}
.icon-container {
cursor: pointer;
Expand All @@ -165,7 +175,7 @@ export default {
&:hover:before,
&.no-icon:before {
z-index: 1;
content: "\f093";
content: '\f093';
font: 64px FontAwesome;
display: block;
position: absolute;
Expand All @@ -179,7 +189,7 @@ export default {
line-height: 100px;
}
&.no-icon:after {
content: "Click to select icon";
content: 'Click to select icon';
font-size: 0.7em;
display: block;
position: absolute;
Expand All @@ -192,9 +202,8 @@ export default {
color: #d9534f;
}
&.no-icon.validate:after {
content: "Please select an icon";
content: 'Please select an icon';
font-weight: bold;
}
}
</style>
2 changes: 1 addition & 1 deletion data/preferences/src/components/pages/Shortcuts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ button { cursor: pointer }
.actions {
position: absolute;
display: none;
top: 0;
top: -4px;
right: 0;
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/search/shortcuts/test_ShortcutResultItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def test_on_enter__default_search(self, item, ActionList, OpenUrlAction, SetUser
def test_on_enter__run_without_arguments(self, item, ActionList, OpenUrlAction, SetUserQueryAction):
item.run_without_argument = True
assert item.on_enter(Query('kw')) is ActionList.return_value
OpenUrlAction.assert_called_once_with('http://site/?q=')
# it doesn't replace %s if run_without_argument = True
OpenUrlAction.assert_called_once_with('http://site/?q=%s')
assert not SetUserQueryAction.called

def test_on_enter__misspelled_kw(self, item, ActionList, OpenUrlAction, SetUserQueryAction):
Expand Down
7 changes: 7 additions & 0 deletions tests/search/shortcuts/test_ShortcutSearchMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def test_is_enabled__query_doesnt_start_with_query__returns_false(self, mode, sh

assert not mode.is_enabled(query)

def test_is_enabled__query_run_without_argument__returns_true(self, mode, shortcuts_db):
query = 'wk'
shortcut = {'keyword': 'wk', 'run_without_argument': True}
shortcuts_db.get_shortcuts.return_value = [shortcut]

assert mode.is_enabled(query)

def test_handle_query__return_value__is_RenderAction_object(self, mode, shortcuts_db, RenderAction):
query = 'kw something'
shortcut = {'keyword': 'kw'}
Expand Down
2 changes: 1 addition & 1 deletion ulauncher/api/shared/action/RunScriptAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RunScriptAction(BaseAction):
:param list args: arguments
"""

def __init__(self, script, args):
def __init__(self, script, args=None):
self.script = script
self.args = args

Expand Down
25 changes: 14 additions & 11 deletions ulauncher/search/shortcuts/ShortcutResultItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def get_description(self, query):

if query.get_keyword() == self.keyword and query.get_argument():
return description.replace('%s', query.get_argument())
if query.get_keyword() == self.keyword and not query.get_argument() and self.run_without_argument:
return 'Type in your query or press Enter...'
if query.get_keyword() == self.keyword and self.run_without_argument:
return 'Press Enter to run the shortcut'
if query.get_keyword() == self.keyword and not query.get_argument():
return 'Type in your query and press Enter...'

Expand All @@ -66,21 +66,24 @@ def on_enter(self, query):
argument = query
else:
argument = None
if argument:
if re.match(r'^http(s)?://', self.cmd.strip()):
command = self.cmd.strip().replace('%s', argument)
action = OpenUrlAction(command)

if self.run_without_argument:
if self._is_url():
action = OpenUrlAction(self.cmd.strip())
else:
action = RunScriptAction(self.cmd, argument)
action = RunScriptAction(self.cmd)
action_list.append(action)
elif self.run_without_argument:
if re.match(r'^http(s)?://', self.cmd.strip()):
command = self.cmd.strip().replace('%s', "")
elif argument:
if self._is_url():
command = self.cmd.strip().replace('%s', argument)
action = OpenUrlAction(command)
else:
action = RunScriptAction(self.cmd, "")
action = RunScriptAction(self.cmd, argument)
action_list.append(action)
else:
action_list.append(SetUserQueryAction('%s ' % self.keyword))

return action_list

def _is_url(self) -> bool:
return bool(re.match(r'^http(s)?://', self.cmd.strip()))
2 changes: 2 additions & 0 deletions ulauncher/search/shortcuts/ShortcutSearchMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def is_enabled(self, query):

def _get_active_shortcut(self, query):
for s in self.shortcutsDb.get_shortcuts():
if query == s.get('keyword') and s.get('run_without_argument'):
return s
if query.startswith('%s ' % s.get('keyword')):
return s

Expand Down
1 change: 1 addition & 0 deletions ulauncher/ui/windows/PreferencesUlauncherDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ def prefs_shortcut_update(self, url_params):
req_data['cmd'],
req_data.get('icon') or None,
str_to_bool(req_data['is_default_search']),
str_to_bool(req_data['run_without_argument']),
req_data.get('id'))
shortcuts.commit()
return {'id': id}
Expand Down

0 comments on commit e6af066

Please sign in to comment.