diff --git a/commit/commit.py b/commit/commit.py index 77f6339..64cd6fe 100644 --- a/commit/commit.py +++ b/commit/commit.py @@ -169,7 +169,7 @@ def get_marked_files(modified_files, staged_files): form_list.append("Unstaged:\n\n") form_list.append(unstaged_checkbox) - form = Form(form_list) + form = Form(form_list, submit_button_name="Continue") # Render the Form and get user input form.render() @@ -283,7 +283,7 @@ def display_commit_message_and_commit(commit_message): None。 """ - text_editor = TextEditor(commit_message) + text_editor = TextEditor(commit_message, submit_button_name="Commit") text_editor.render() new_commit_message = text_editor.new_text diff --git a/libs/chatmark/form.py b/libs/chatmark/form.py index c16c30b..139dfe8 100644 --- a/libs/chatmark/form.py +++ b/libs/chatmark/form.py @@ -14,6 +14,8 @@ def __init__( self, components: List[Union[Widget, str]], title: Optional[str] = None, + submit_button_name: Optional[str] = None, + cancel_button_name: Optional[str] = None, ): """ components: components in the form, can be widgets (except Button) or strings @@ -27,12 +29,15 @@ def __init__( self._title = title self._rendered = False + self._submit = submit_button_name + self._cancel = cancel_button_name @property def components(self) -> List[Union[Widget, str]]: """ Return the components """ + return self._components def _in_chatmark(self) -> str: @@ -74,8 +79,12 @@ def render(self): self._rendered = True + chatmark_header = "```chatmark" + chatmark_header += f" submit={self._submit}" if self._submit else "" + chatmark_header += f" cancel={self._cancel}" if self._cancel else "" + lines = [ - "```chatmark type=form", + chatmark_header, self._in_chatmark(), "```", ] diff --git a/libs/chatmark/widgets.py b/libs/chatmark/widgets.py index a64c76b..ebfc019 100644 --- a/libs/chatmark/widgets.py +++ b/libs/chatmark/widgets.py @@ -9,10 +9,12 @@ class Widget(ABC): Abstract base class for widgets """ - def __init__(self): + def __init__(self, submit: Optional[str] = None, cancel: Optional[str] = None): self._rendered = False # Prefix for IDs/keys in the widget self._id_prefix = self.gen_id_prefix() + self._submit = submit + self._cancel = cancel @abstractmethod def _in_chatmark(self) -> str: @@ -40,8 +42,12 @@ def render(self) -> None: self._rendered = True + chatmark_header = "```chatmark" + chatmark_header += f" submit={self._submit}" if self._submit else "" + chatmark_header += f" cancel={self._cancel}" if self._cancel else "" + lines = [ - "```chatmark", + chatmark_header, self._in_chatmark(), "```", ] @@ -89,13 +95,15 @@ def __init__( options: List[str], check_states: Optional[List[bool]] = None, title: Optional[str] = None, + submit_button_name: str = "Submit", + cancel_button_name: str = "Cancel", ): """ options: options to be selected check_states: initial check states of options, default to all False title: title of the widget """ - super().__init__() + super().__init__(submit_button_name, cancel_button_name) if check_states is not None: assert len(options) == len(check_states) @@ -183,8 +191,14 @@ class TextEditor(Widget): ``` """ - def __init__(self, text: str, title: Optional[str] = None): - super().__init__() + def __init__( + self, + text: str, + title: Optional[str] = None, + submit_button_name: str = "Submit", + cancel_button_name: str = "Cancel", + ): + super().__init__(submit_button_name, cancel_button_name) self._title = title self._text = text @@ -236,25 +250,25 @@ class Radio(Widget): def __init__( self, options: List[str], - # TODO: implement default_selected after the design is ready - # default_selected: Optional[int] = None, + default_selected: Optional[int] = None, title: Optional[str] = None, + submit_button_name: str = "Submit", + cancel_button_name: str = "Cancel", ) -> None: """ options: options to be selected default_selected: index of the option to be selected by default, default to None title: title of the widget """ - # TODO: implement default_selected after the design is ready - # if default_selected is not None: - # assert 0 <= default_selected < len(options) + if default_selected is not None: + assert 0 <= default_selected < len(options) - super().__init__() + super().__init__(submit_button_name, cancel_button_name) self._options = options self._title = title - self._selection: Optional[int] = None + self._selection: Optional[int] = default_selected @property def options(self) -> List[str]: @@ -282,7 +296,10 @@ def _in_chatmark(self) -> str: for idx, option in enumerate(self._options): key = self.gen_id(self._id_prefix, idx) - lines.append(f"> - ({key}) {option}") + if self._selection is not None and self._selection == idx: + lines.append(f"> x ({key}) {option}") + else: + lines.append(f"> - ({key}) {option}") text = "\n".join(lines) return text