sgtpep / pmenu

A dynamic terminal-based menu inspired by dmenu written in Python without dependencies with an optional MRU ordering which could also be used as an application launcher and CtrlP alternative.
GNU General Public License v3.0
122 stars 6 forks source link

option to set initial query #7

Open thinlines opened 3 years ago

thinlines commented 3 years ago

Hi! Great script, thanks for writing it. I wanted to be able to set the initial query string to make pmenu more useful in bash scripting, so I went ahead and made a few changes. It seems to work okay, though I'm barely a novice in python. Here's a patch:

diff --unified --recursive a/pmenu b/pmenu
--- a/pmenu 2021-07-06 01:33:36.554971843 +0800
+++ b/pmenu 2021-07-06 01:00:54.000000000 +0800
@@ -21,6 +21,7 @@
 def get_args():
     parser = argparse.ArgumentParser(usage="pipe newline-separated menu items to stdin and/or pass them as positional arguments")
     parser.add_argument('item', nargs='*', help="the menu item text")
+    parser.add_argument('-q', '--query', help="start with the given query")
     parser.add_argument('-c', '--command', help="the shell command which output will populate the menu items on every keystroke ({} will be replaced by the current input text)")
     parser.add_argument('-n', '--name', help="the cache file name with the most recently used items")
     parser.add_argument('-p', '--prompt', help="the prompt text")
@@ -34,6 +35,14 @@

     return args

+def get_query_text():
+    if not args.query:
+        query_text = ''
+        return query_text
+
+    query_text = args.query
+    return query_text
+
 def get_mru_path():
     if not args.name:
         return
@@ -268,7 +277,7 @@

 if __name__ == '__main__':
     args = get_args()
-    query_text = ''
+    query_text = get_query_text()
     input_items = get_input_items()
     mru_path = get_mru_path()
     mru_items = get_mru_items(mru_path, input_items)

Just thought I'd throw this on here in case anyone's interested. If you want me to submit a pull request, I'd be glad to. Thanks again!

sgtpep commented 3 years ago

Hi, @thinlines! I'm happy to hear that you find the script useful. Thanks for your suggestion, it totally makes sense to make it customizable. Will be happy to accept a PR. How about simplifying to a bit to something like this:

diff --unified --recursive a/pmenu b/pmenu
--- a/pmenu 2021-07-06 01:33:36.554971843 +0800
+++ b/pmenu 2021-07-06 01:00:54.000000000 +0800
@@ -21,6 +21,7 @@
 def get_args():
     parser = argparse.ArgumentParser(usage="pipe newline-separated menu items to stdin and/or pass them as positional arguments")
     parser.add_argument('item', nargs='*', help="the menu item text")
+    parser.add_argument('-q', '--query', default='', help="start with the given query")
     parser.add_argument('-c', '--command', help="the shell command which output will populate the menu items on every keystroke ({} will be replaced by the current input text)")
     parser.add_argument('-n', '--name', help="the cache file name with the most recently used items")
     parser.add_argument('-p', '--prompt', help="the prompt text")
@@ -34,6 +35,14 @@

     return args

 def get_mru_path():
     if not args.name:
         return
@@ -268,7 +277,7 @@

 if __name__ == '__main__':
     args = get_args()
-    query_text = ''
+    query_text = args.query
     input_items = get_input_items()
     mru_path = get_mru_path()
     mru_items = get_mru_items(mru_path, input_items)