Skip to content

Commit

Permalink
Add Checkbox and Radio Button fields to Micron format
Browse files Browse the repository at this point in the history
  • Loading branch information
RFnexus committed Nov 27, 2024
1 parent a5aa209 commit 912c510
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion nomadnet/examples/various/input_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
>> Checkbox Fields
`B444`<?|sign_up|1`>`b Sign me up
`B444`<?|sign_up|1|*`>`b Sign me up
>> Radio group
Expand Down
4 changes: 4 additions & 0 deletions nomadnet/ui/textui/Guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,10 @@ def focus_reader(self):
`B444`<?|sign_up|1`>`b Sign me up`
You can also pre-check both checkboxes and radio groups by appending a |* after the field value.
`B444`<?|checkbox|1|*`>`b Pre-checked checkbox`
>>> Radio groups
Radio groups are another input that lets the user chose from a set of options. Unlike checkboxes, radio buttons with the same field name are mutually exclusive.
Expand Down
26 changes: 22 additions & 4 deletions nomadnet/ui/textui/MicronParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def parse_line(line, state, url_delegate):
fv = o["value"]
flabel = o["label"]
fs = o["style"]
f = urwid.CheckBox(flabel, state=False)
fprechecked = o.get("prechecked", False)
f = urwid.CheckBox(flabel, state=fprechecked)
f.field_name = fn
f.field_value = fv
fa = urwid.AttrMap(f, fs)
Expand All @@ -187,19 +188,21 @@ def parse_line(line, state, url_delegate):
fv = o["value"]
flabel = o["label"]
fs = o["style"]
if not "radio_groups" in state:
fprechecked = o.get("prechecked", False)
if "radio_groups" not in state:
state["radio_groups"] = {}
if not fn in state["radio_groups"]:
if fn not in state["radio_groups"]:
state["radio_groups"][fn] = []
group = state["radio_groups"][fn]
f = urwid.RadioButton(group, flabel, state=False, user_data=fv)
f = urwid.RadioButton(group, flabel, state=fprechecked, user_data=fv)
f.field_name = fn
f.field_value = fv
fa = urwid.AttrMap(f, fs)
widgets.append((urwid.PACK, fa))




columns_widget = urwid.Columns(widgets, dividechars=0)
text_widget = columns_widget
# text_widget = urwid.Text("<"+output+">", align=state["align"])
Expand Down Expand Up @@ -501,6 +504,7 @@ def make_output(state, line, url_delegate):
field_name = field_content
field_value = ""
field_data = ""
field_prechecked = False

# check if field_content contains '|'
if '|' in field_content:
Expand All @@ -526,10 +530,23 @@ def make_output(state, line, url_delegate):
except ValueError:
pass # Ignore invalid width

# Check for value and pre-checked flag
if len(f_components) > 2:
field_value = f_components[2]
else:
field_value = ""
if len(f_components) > 3:
if f_components[3] == '*':
field_prechecked = True

else:
# No '|', so field_name is field_content
field_name = field_content
field_type = "field"
field_masked = False
field_width = 24
field_value = ""
field_prechecked = False

# Find the closing '>' character
field_end = line.find('>', backtick_pos)
Expand All @@ -546,6 +563,7 @@ def make_output(state, line, url_delegate):
"name": field_name,
"value": field_value if field_value else field_data,
"label": field_data,
"prechecked": field_prechecked,
"style": make_style(state)
})
else:
Expand Down

0 comments on commit 912c510

Please sign in to comment.