| 128 | } |
| 129 | |
| 130 | uint8_t ManualSelectorNode::selectChild() const |
| 131 | { |
| 132 | const size_t children_count = children_nodes_.size(); |
| 133 | |
| 134 | std::vector<std::string> list; |
| 135 | list.reserve(children_count); |
| 136 | for(const auto& child : children_nodes_) |
| 137 | { |
| 138 | list.push_back(child->name()); |
| 139 | } |
| 140 | |
| 141 | size_t width = 10; |
| 142 | for(const auto& str : list) |
| 143 | { |
| 144 | width = std::max(width, str.size() + 2); |
| 145 | } |
| 146 | |
| 147 | WINDOW* win = nullptr; |
| 148 | initscr(); |
| 149 | cbreak(); |
| 150 | |
| 151 | win = newwin(static_cast<int>(children_count) + 6, 70, 1, 1); // create a new window |
| 152 | |
| 153 | mvwprintw(win, 0, 0, "Use UP/DOWN arrow to select the child, Enter to confirm."); |
| 154 | mvwprintw(win, 1, 0, "Press: S to skip and return SUCCESSFUL,"); |
| 155 | mvwprintw(win, 2, 0, " F to skip and return FAILURE, or"); |
| 156 | mvwprintw(win, 3, 0, " R to skip and return RUNNING."); |
| 157 | |
| 158 | // now print all the menu items and highlight the first one |
| 159 | for(size_t i = 0; i < list.size(); i++) |
| 160 | { |
| 161 | mvwprintw(win, static_cast<int>(i) + 5, 0, "%2zu. %s", i + 1, list[i].c_str()); |
| 162 | } |
| 163 | |
| 164 | wrefresh(win); // update the terminal screen |
| 165 | noecho(); // disable echoing of characters on the screen |
| 166 | keypad(win, TRUE); // enable keyboard input for the window. |
| 167 | curs_set(0); // hide the default screen cursor. |
| 168 | |
| 169 | uint8_t row = 0; |
| 170 | int ch = 0; |
| 171 | while(true) |
| 172 | { |
| 173 | // right pad with spaces to make the items appear with even width. |
| 174 | wattroff(win, A_STANDOUT); |
| 175 | mvwprintw(win, row + 5, 4, "%s", list[row].c_str()); |
| 176 | // use a variable to increment or decrement the value based on the input. |
| 177 | if(ch == KEY_DOWN) |
| 178 | { |
| 179 | row = (row == children_count - 1) ? 0 : row + 1; |
| 180 | } |
| 181 | else if(ch == KEY_UP) |
| 182 | { |
| 183 | row = (row == 0) ? (children_count - 1) : row - 1; |
| 184 | } |
| 185 | else if(ch == KEY_ENTER || ch == 10) |
| 186 | { |
| 187 | break; |