diff --git a/README.md b/README.md index 1b2d348..63085bf 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/generate.py b/generate.py index 59c8e0c..d8877e7 100755 --- a/generate.py +++ b/generate.py @@ -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(): diff --git a/src/builtins.mk b/src/builtins.mk index c06230f..632fe51 100644 --- a/src/builtins.mk +++ b/src/builtins.mk @@ -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 \ No newline at end of file +export MAKEFILE_LIST diff --git a/src/confirm.py b/src/confirm.py new file mode 100644 index 0000000..a3ecb7f --- /dev/null +++ b/src/confirm.py @@ -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 %# diff --git a/src/runners.mk b/src/runners.mk index f1fa05a..36262a9 100644 --- a/src/runners.mk +++ b/src/runners.mk @@ -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)))") diff --git a/task.mk b/task.mk index 433512f..3b33c42 100644 --- a/task.mk +++ b/task.mk @@ -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 +