1
0
Fork 0

bang: allow a file with a list of queries

master
Adrien Abraham 2 years ago
parent b7c069c8c4
commit 366a14d2f6

@ -35,8 +35,9 @@ import webbrowser
def load_config(config_path=None): def load_config(config_path=None):
if config_path is None: if config_path is None:
config_path = os.environ.get("BANGS_CONFIG_PATH") or os.path.expanduser( config_path = (
"~/.config/bangs.json" os.environ.get("BANGS_CONFIG_PATH")
or os.path.expanduser("~/.config/bangs.json")
) )
try: try:
with open(config_path, "rt") as bangs_file: with open(config_path, "rt") as bangs_file:
@ -49,7 +50,7 @@ def list_bangs(config):
for item in config["bangs"]: for item in config["bangs"]:
name = item["name"] name = item["name"]
handle = item["handle"] handle = item["handle"]
print(f"- {handle} {name}") print(f"- {handle}: {name}")
def run_rofi(config, input_text="", title="bang"): def run_rofi(config, input_text="", title="bang"):
@ -67,10 +68,9 @@ def run_rofi(config, input_text="", title="bang"):
def open_bang(config, handle, query): def open_bang(config, handle, query):
for bang in config["bangs"]: try:
if handle == bang["handle"]: bang = next(bang for bang in config["bangs"] if handle == bang["handle"])
break except StopIteration:
else:
print("Unknown handle.") print("Unknown handle.")
return return
query = query.strip() query = query.strip()
@ -85,6 +85,7 @@ def main():
ap.add_argument("-c", "--config", help="path to JSON config file") ap.add_argument("-c", "--config", help="path to JSON config file")
ap.add_argument("-l", "--list", action="store_true", help="show available bangs") ap.add_argument("-l", "--list", action="store_true", help="show available bangs")
ap.add_argument("-b", "--bang", nargs="+", help="launch with this bang already set") ap.add_argument("-b", "--bang", nargs="+", help="launch with this bang already set")
ap.add_argument("-f", "--queries-file", help="file with one bang argument per line")
args = ap.parse_args() args = ap.parse_args()
config = load_config() config = load_config()
@ -95,24 +96,36 @@ def main():
list_bangs(config) list_bangs(config)
return return
queries = []
if listfile := args.queries_file:
try:
with open(listfile, "rt") as file:
queries = [line.rstrip() for line in file.readlines()]
except OSError:
exit("Can't load queries file.")
# If a bang is specified on the command line, use it, optionally with its args.
if bang_args := args.bang: if bang_args := args.bang:
handle = bang_args[0] handle = bang_args[0]
if bang_args[1:]: if bang_args[1:]:
query = " ".join(bang_args[1:]) queries.append(" ".join(bang_args[1:]))
else: # Else show a Rofi with the list of available bangs.
query = run_rofi(config, title=handle)
else: else:
process_input = "\n".join(i["handle"] for i in config["bangs"]) + "\n" process_input = "\n".join(i["handle"] for i in config["bangs"]) + "\n"
output = run_rofi(config, input_text=process_input) output = run_rofi(config, input_text=process_input)
parts = output.split(maxsplit=1) parts = output.split(maxsplit=1)
if len(parts) < 1: if len(parts) < 1:
exit("Bad Rofi output.") exit("Bad Rofi output.")
if len(parts) == 1: handle = parts[0]
handle = parts[0] if len(parts) > 1:
query = run_rofi(config, title=handle) queries.append(parts[1])
else:
handle, query = parts # If no queries were obtained during options parsing, show Rofi now to get a single query.
open_bang(config, handle, query) if not queries:
queries.append(run_rofi(config, title=handle))
for query in queries:
open_bang(config, handle, query)
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save