Skip to content

Commit

Permalink
Update text entry widget
Browse files Browse the repository at this point in the history
- Added documentation
- Now respects the provided max_length
- Now correctly returns EXIT_FAILURE or EXIT_SUCCESS
- Renamed username argument to user_input since the field can be used
  for any input
  • Loading branch information
bitoffdev committed Jan 31, 2021
1 parent c8e15b2 commit ccbd88a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/widgets.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@

#define UINT_TO_INT(n) n < 32767 ? (int)n : (n > 32768 ? -(int)-n : -32768)

void ttviz_entry(char *username, char *label, int max_length) {
int ttviz_entry(char *user_input, char *label, int max_length) {
int ch, _exit;
CursesTextEntry *entry;

erase();

mvprintw(2, 2, "Enter a Username:");
mvprintw(2, 2, label);

if ((_exit = curses_text_field_create(&entry, stdscr, 3, 20, 3, 1,
max_length)) != EXIT_SUCCESS)
exit(_exit);
return _exit;
curses_text_field_refresh(entry);

while ((ch = getch()) != '\n') {
curses_text_field_feed(entry, ch);
curses_text_field_refresh(entry);
}

strncpy(username, curses_text_field_value(entry), 7);
strncpy(user_input, curses_text_field_value(entry), max_length);

curses_text_field_destroy(entry);

return EXIT_SUCCESS;
}

struct ttetris_widget_selection {
Expand Down
9 changes: 8 additions & 1 deletion src/widgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ int selection_to_index(WidgetSelection *selection);
StringArray *selection_to_string_array(WidgetSelection *selection);
void selection_destroy(WidgetSelection *selection);

void ttviz_entry(char *username, char *label, int max_length);
/**
* full-screen synchronous text entry using curses
* @param user_input pointer to buffer where the entered text will be written
* @param label text to display to user above the input field
* @param max_length size of the user_input buffer
* @return EXIT_SUCCESS or EXIT_FAILURE
*/
int ttviz_entry(char *user_input, char *label, int max_length);
WidgetSelection *ttviz_select(char **options, int num_options, char *desc,
int is_single_selection);

Expand Down

0 comments on commit ccbd88a

Please sign in to comment.