improved menu display

This commit is contained in:
vishalxl 2022-08-19 12:17:55 +05:30
parent 6ee59e0e9a
commit a08280446a
2 changed files with 37 additions and 36 deletions

View File

@ -7,32 +7,32 @@ import 'package:crypto/crypto.dart';
import 'dart:convert'; // for the utf8.encode method import 'dart:convert'; // for the utf8.encode method
int showMenu(List<String> menuOptions) { int showMenu(List<String> menuOptions, String menuName) {
print("\n$menuName\n${getNumDashes(menuName.length)}");
print('Pick an option:');
while(true) { while(true) {
//print("in showmenu while");
for(int i = 0; i < menuOptions.length;i++) { for(int i = 0; i < menuOptions.length;i++) {
print(" ${i+1}. ${menuOptions[i]}"); print(" ${i+1}. ${menuOptions[i]}");
} }
stdout.write("Type menu option/number: "); stdout.write("Type menu option/number: ");
//print(">");
String? userOptionInput = stdin.readLineSync(); String? userOptionInput = stdin.readLineSync();
String userOption = userOptionInput??""; String userOption = userOptionInput??"";
//print("read option $userOption");
if( int.tryParse(userOption) != null) { if( int.tryParse(userOption) != null) {
int? valueOption = int.tryParse(userOption); try{
if( valueOption != null) { int? valueOption = int.tryParse(userOption);
if( valueOption < 1 || valueOption > menuOptions.length) { if( valueOption != null) {
print("Invalid option. Kindly try again.\n"); if( valueOption >= 1 && valueOption <= menuOptions.length) {
continue; return valueOption;
} else { }
return valueOption;
} }
} } on FormatException catch (e) {
} else { print(e.message);
print("Invalid option. Kindly try again.\n"); } on Exception catch (e) {
print(e);
}
print("Invalid option. Kindly try again. The valid\noptions are from 1 to ${menuOptions.length}");
} }
} }
} }
@ -48,10 +48,10 @@ String getShaId(String pubkey, int createdAt, String strTags, String content) {
Future<void> otherMenuUi(Tree node, var contactList) async { Future<void> otherMenuUi(Tree node, var contactList) async {
bool continueOtherMenu = true; bool continueOtherMenu = true;
while(continueOtherMenu) { while(continueOtherMenu) {
print('\n\nPick an option by typing the corresponding\nnumber and then pressing <enter>:'); int option = showMenu([ 'Display Contact List', // 1
int option = showMenu([ 'Display Contact List', // 1 'Change number of days printed', // 2
'Change number of days printed', 'Go back to main menu'], // 3
'Go back to main menu']); // 3 "Other Menu");
print('You picked: $option'); print('You picked: $option');
switch(option) { switch(option) {
case 1: case 1:
@ -61,19 +61,20 @@ Future<void> otherMenuUi(Tree node, var contactList) async {
print(""); print("");
break; break;
case 2: case 2:
String? $tempNumDays = stdin.readLineSync(); stdout.write("Enter number of days for which you want to see posts: ");
String newNumDays = $tempNumDays??""; String? $tempNumDays = stdin.readLineSync();
String newNumDays = $tempNumDays??"";
try { try {
gNumLastDays = int.parse(newNumDays); gNumLastDays = int.parse(newNumDays);
print("Changed number of days printed to $gNumLastDays"); print("Changed number of days printed to $gNumLastDays");
} on FormatException catch (e) { } on FormatException catch (e) {
print(e.message); print(e.message);
return; return;
} on Exception catch (e) { } on Exception catch (e) {
print(e); print(e);
return; return;
} }
break; break;
@ -118,11 +119,11 @@ Future<void> mainMenuUi(Tree node, var contactList) async {
await foo(); await foo();
// the main menu // the main menu
print('\n\nPick an option by typing the corresponding\nnumber and then pressing <enter>:');
int option = showMenu(['Display events', // 1 int option = showMenu(['Display events', // 1
'Post/Reply', // 2 'Post/Reply', // 2
'Other Options', //3 'Other Options', // 3
'Quit']); // 3 'Quit'], // 4
"Main Menu");
print('You picked: $option'); print('You picked: $option');
switch(option) { switch(option) {
case 1: case 1:

View File

@ -191,7 +191,7 @@ class Tree {
stdout.write("\n---------------------------------------\nNotifications: "); stdout.write("\n---------------------------------------\nNotifications: ");
if( newEventsId.isEmpty) { if( newEventsId.isEmpty) {
stdout.write("No new replies/posts.\nTotal posts: ${count()}\n"); stdout.write("No new replies/posts.\nTotal posts: ${count()}\n\n");
return; return;
} }
// TODO call count() less // TODO call count() less