feat: add confirmation prompt

This commit is contained in:
Daylin Morgan 2022-09-14 16:52:49 -05:00
parent ea85b2378e
commit 253832af89
6 changed files with 65 additions and 15 deletions

View file

@ -17,6 +17,7 @@ Current Features:
- ANSI escape code support (including NO_COLOR)
- formatted help output
- custom print function
- confirmation prompt
Depends on `GNU Make`, obviously and `Python >=3.7`.

View file

@ -5,7 +5,7 @@ from pathlib import Path
import jinja2
py_script_names = ["help", "ansi", "info", "print-ansi", "vars"]
py_script_names = ["help", "ansi", "info", "print-ansi", "vars","confirm"]
def get_jinja_env():

View file

@ -1,6 +1,5 @@
# ---- [buitlin recipes] ---- #
## h, help | show this help
.PHONY: help h
help h:
@ -29,8 +28,10 @@ _print-ansi:
tprint = $(call py,info_py,$(1))
tprint-sh = $(call pysh,info_py,$(1))
tconfirm = $(call py,confirm_py,$(1))
_update-task.mk:
$(call tprint,Updating task.mk)
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/main/task.mk -o .task.mk
export MAKEFILE_LIST
export MAKEFILE_LIST

23
src/confirm.py Normal file
View file

@ -0,0 +1,23 @@
#% extends "py-script.mk" %#
#% block name %#confirm#% endblock %#
#% block script %#
import sys
##- '$(ansi_py)' -##
def confirm():
"""
Ask user to enter Y or N (case-insensitive).
:return: True if the answer is Y.
:rtype: bool
"""
answer = ""
while answer not in ["y", "n"]:
answer = input(f"""$(2) {a.b_red}[Y/n]{a.end} """).lower()
return answer == "y"
if confirm():
sys.exit(0)
else:
sys.exit(1)
#% endblock %#

View file

@ -13,16 +13,16 @@ create_string = $(subst $(\n),\n,$(call escape_shellstring,$(call escape_printf,
ifdef DEBUG
define _debug_runner
@printf "$(1) Script:\n"
@printf -- "<---------------->\n"
@printf -- "<----------------------------------->\n"
@printf "$(call create_string,$(3))\n"
@printf -- "<---------------->\n"
@printf -- "<----------------------------------->\n"
@printf "$(call create_string,$(3))" | $(2)
endef
py = $(call _debug_runner,Python,python3,$($(1)))
tbash = $(call _debug_runner,Bash,bash,$($(1)))
else
py = @printf "$(call create_string,$($(1)))" | python3
tbash = @printf "$(call create_string,$($(1)))" | bash
py = @python3 <(printf "$(call create_string,$($(1)))")
tbash = @bash <(printf "$(call create_string,$($(1)))")
endif
pysh = printf "$(call create_string,$($(1)))" | python3
pysh = python3 <(printf "$(call create_string,$($(1)))")

39
task.mk
View file

@ -1,7 +1,7 @@
# }> [github.com/daylinmorgan/task.mk] <{ #
# Copyright (c) 2022 Daylin Morgan
# MIT License
# version: 22.9.14
# version: v22.9.14-dev
#
# task.mk should be included at the bottom of your Makefile.
# See below for the standard configuration options that should be set prior to including this file.
@ -27,7 +27,6 @@ endef
# ---- [buitlin recipes] ---- #
## h, help | show this help
.PHONY: help h
help h:
@ -56,6 +55,8 @@ _print-ansi:
tprint = $(call py,info_py,$(1))
tprint-sh = $(call pysh,info_py,$(1))
tconfirm = $(call py,confirm_py,$(1))
_update-task.mk:
$(call tprint,Updating task.mk)
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/main/task.mk -o .task.mk
@ -77,19 +78,19 @@ create_string = $(subst $(\n),\n,$(call escape_shellstring,$(call escape_printf,
ifdef DEBUG
define _debug_runner
@printf "$(1) Script:\n"
@printf -- "<---------------->\n"
@printf -- "<----------------------------------->\n"
@printf "$(call create_string,$(3))\n"
@printf -- "<---------------->\n"
@printf -- "<----------------------------------->\n"
@printf "$(call create_string,$(3))" | $(2)
endef
py = $(call _debug_runner,Python,python3,$($(1)))
tbash = $(call _debug_runner,Bash,bash,$($(1)))
else
py = @printf "$(call create_string,$($(1)))" | python3
tbash = @printf "$(call create_string,$($(1)))" | bash
py = @python3 <(printf "$(call create_string,$($(1)))")
tbash = @bash <(printf "$(call create_string,$($(1)))")
endif
pysh = printf "$(call create_string,$($(1)))" | python3
pysh = python3 <(printf "$(call create_string,$($(1)))")
# ---- [python scripts] ---- #
@ -325,3 +326,27 @@ print()
endef
define confirm_py
import sys
$(ansi_py)
def confirm():
"""
Ask user to enter Y or N (case-insensitive).
:return: True if the answer is Y.
:rtype: bool
"""
answer = ""
while answer not in ["y", "n"]:
answer = input(f"""$(2) {a.b_red}[Y/n]{a.end} """).lower()
return answer == "y"
if confirm():
sys.exit(0)
else:
sys.exit(1)
endef