Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ is only place where he\'ll ever have use it?
## Features

- Save a new command with a brief description
- Save notes or command sets with `keep new notes` and `keep new set`
- Search the saved commands using powerful patterns
- Save the commands as a secret GitHub gist
- Use `keep push` and `keep pull` to sync the commands between GitHub
Expand Down
65 changes: 52 additions & 13 deletions keep/commands/cmd_new.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
import click
from keep import cli, utils
from keep import cli as kcli, utils

@click.command("new", short_help="Saves a new command.")

@click.group("new", short_help="Create a new entry.", invoke_without_command=True)
@click.option("--cmd", help="The command to save")
@click.option("--desc", help="The description of the command")
@click.option("--alias", default="", help="The alias of the command")
@cli.pass_context
def cli(ctx, cmd, desc, alias):
"""Saves a new command"""
if not cmd:
cmd = click.prompt("Command")
if not desc:
desc = click.prompt("Description")
if not alias:
alias = click.prompt("Alias (optional)", default="")
utils.save_command(cmd, desc, alias)
@kcli.pass_context
def cli(kctx, cmd, desc, alias):
"""Saves a new command, note or command set."""
ctx = click.get_current_context()
if ctx.invoked_subcommand is None:
if not cmd:
cmd = click.prompt("Command")
if not desc:
desc = click.prompt("Description")
if not alias:
alias = click.prompt("Alias (optional)", default="")
utils.save_command(cmd, desc, alias)
utils.log(kctx, f"Saved the new command - {cmd} - with the description - {desc}.")


@cli.command("notes", short_help="Saves a new note.")
@click.option("--name", help="Name of the note")
@click.option("--text", help="Text of the note")
@kcli.pass_context
def notes(kctx, name, text):
if not name:
name = click.prompt("Name")
if not text:
template = "# Write your note below\n"
edited = click.edit(template)
if not edited:
click.echo("No note provided.")
return
text = "\n".join([line for line in edited.splitlines() if not line.startswith("#")])
utils.save_note(name, text)
utils.log(kctx, f"Saved the note - {name}.")


@cli.command("set", short_help="Saves a new set of commands.")
@click.option("--name", help="Name of the set")
@click.option("--commands", multiple=True, help="Commands in the set")
@kcli.pass_context
def set_(kctx, name, commands):
if not name:
name = click.prompt("Name")
if not commands:
template = "# Enter commands one per line\n"
edited = click.edit(template)
if not edited:
click.echo("No commands provided.")
return
commands = [line for line in edited.splitlines() if not line.startswith("#") and line.strip()]
utils.save_command_set(name, list(commands))
utils.log(kctx, f"Saved command set - {name}.")

utils.log(ctx, "Saved the new command - {} - with the description - {}.".format(cmd, desc))
22 changes: 22 additions & 0 deletions keep/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ def save_command(cmd, desc, alias=""):
f.write(json.dumps(commands))


def save_note(name, note):
"""Save a note text with a given name."""
json_path = os.path.join(dir_path, 'notes.json')
notes = {}
if os.path.exists(json_path):
notes = json.loads(open(json_path, 'r').read())
notes[name] = note
with open(json_path, 'w') as f:
f.write(json.dumps(notes))


def save_command_set(name, commands):
"""Save a set of commands under a name."""
json_path = os.path.join(dir_path, 'sets.json')
sets = {}
if os.path.exists(json_path):
sets = json.loads(open(json_path, 'r').read())
sets[name] = commands
with open(json_path, 'w') as f:
f.write(json.dumps(sets))


def read_commands():
json_path = os.path.join(dir_path, 'commands.json')
if not os.path.exists(json_path):
Expand Down