Skip to content

Commit

Permalink
refactor(tips): unify show and list
Browse files Browse the repository at this point in the history
  • Loading branch information
himkt committed Jul 8, 2024
1 parent e664f18 commit 7a813ca
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions bin/tips
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ def tips_home() -> pathlib.Path:
return pathlib.Path(os.getenv("TIPS_HOME", default)) / "tips.d"


def list_tips(_: argparse.Namespace) -> list[str]:
for name in tips_home().iterdir():
print(name.name)

def list_tips(args: argparse.Namespace) -> None:
if (name := args.name) is None:
for _tips_dir in tips_home().iterdir():
name = _tips_dir.name
if args.query is not None and args.query not in name:
continue
print(name)
return

def show_tips(args: argparse.Namespace) -> None:
tips_file = tips_home() / args.name / "tips"
tips_file = tips_home() / name / "tips"
if not tips_file.exists():
print(f"No tips available for {args.name}")
return
Expand Down Expand Up @@ -56,7 +59,6 @@ def main(args: argparse.Namespace) -> None:

actions = {
"list": list_tips,
"show": show_tips,
"edit": edit_tips,
"init": init_tips,
}
Expand All @@ -73,17 +75,15 @@ if __name__ == "__main__":
parser.add_argument("--version", "-v", action="version", version="0.0.1")
sub_parsers = parser.add_subparsers(help="sub command help", dest="sub_parser")

show_parser = sub_parsers.add_parser("show")
show_parser.add_argument("name", type=str, default=None)
show_parser.add_argument("--query", "-q")

edit_parser = sub_parsers.add_parser("edit")
edit_parser.add_argument("name", type=str, default=None)

init_parser = sub_parsers.add_parser("init")
init_parser.add_argument("name", type=str)

list_parser = sub_parsers.add_parser("list")
list_parser.add_argument("name", type=str, nargs="?")
list_parser.add_argument("--query", "-q")

args = parser.parse_args()
main(args)

0 comments on commit 7a813ca

Please sign in to comment.