Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance form and widget button label functionality #28

Merged
merged 5 commits into from
Jan 5, 2024
Merged
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
4 changes: 2 additions & 2 deletions commit/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion libs/chatmark/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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(),
"```",
]
Expand Down
43 changes: 30 additions & 13 deletions libs/chatmark/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(),
"```",
]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down