From a08280446ad10f7e9fb6986e9ae8eecaed277f5e Mon Sep 17 00:00:00 2001 From: vishalxl <> Date: Fri, 19 Aug 2022 12:17:55 +0530 Subject: [PATCH] improved menu display --- lib/console_ui.dart | 71 +++++++++++++++++++++++---------------------- lib/tree_ds.dart | 2 +- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/lib/console_ui.dart b/lib/console_ui.dart index 94a0f7e..66c48c6 100644 --- a/lib/console_ui.dart +++ b/lib/console_ui.dart @@ -7,32 +7,32 @@ import 'package:crypto/crypto.dart'; import 'dart:convert'; // for the utf8.encode method -int showMenu(List menuOptions) { +int showMenu(List menuOptions, String menuName) { + print("\n$menuName\n${getNumDashes(menuName.length)}"); + print('Pick an option:'); while(true) { - //print("in showmenu while"); for(int i = 0; i < menuOptions.length;i++) { print(" ${i+1}. ${menuOptions[i]}"); } stdout.write("Type menu option/number: "); - - //print(">"); String? userOptionInput = stdin.readLineSync(); String userOption = userOptionInput??""; - //print("read option $userOption"); if( int.tryParse(userOption) != null) { - int? valueOption = int.tryParse(userOption); - if( valueOption != null) { - if( valueOption < 1 || valueOption > menuOptions.length) { - print("Invalid option. Kindly try again.\n"); - continue; - } else { - - return valueOption; + try{ + int? valueOption = int.tryParse(userOption); + if( valueOption != null) { + if( valueOption >= 1 && valueOption <= menuOptions.length) { + return valueOption; + } } - } - } else { - print("Invalid option. Kindly try again.\n"); + } on FormatException catch (e) { + print(e.message); + } 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 otherMenuUi(Tree node, var contactList) async { bool continueOtherMenu = true; while(continueOtherMenu) { - print('\n\nPick an option by typing the corresponding\nnumber and then pressing :'); - int option = showMenu([ 'Display Contact List', // 1 - 'Change number of days printed', - 'Go back to main menu']); // 3 + int option = showMenu([ 'Display Contact List', // 1 + 'Change number of days printed', // 2 + 'Go back to main menu'], // 3 + "Other Menu"); print('You picked: $option'); switch(option) { case 1: @@ -61,19 +61,20 @@ Future otherMenuUi(Tree node, var contactList) async { print(""); break; case 2: - String? $tempNumDays = stdin.readLineSync(); - String newNumDays = $tempNumDays??""; + stdout.write("Enter number of days for which you want to see posts: "); + String? $tempNumDays = stdin.readLineSync(); + String newNumDays = $tempNumDays??""; - try { - gNumLastDays = int.parse(newNumDays); - print("Changed number of days printed to $gNumLastDays"); - } on FormatException catch (e) { - print(e.message); - return; - } on Exception catch (e) { - print(e); - return; - } + try { + gNumLastDays = int.parse(newNumDays); + print("Changed number of days printed to $gNumLastDays"); + } on FormatException catch (e) { + print(e.message); + return; + } on Exception catch (e) { + print(e); + return; + } break; @@ -118,11 +119,11 @@ Future mainMenuUi(Tree node, var contactList) async { await foo(); // the main menu - print('\n\nPick an option by typing the corresponding\nnumber and then pressing :'); int option = showMenu(['Display events', // 1 'Post/Reply', // 2 - 'Other Options', //3 - 'Quit']); // 3 + 'Other Options', // 3 + 'Quit'], // 4 + "Main Menu"); print('You picked: $option'); switch(option) { case 1: diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index ddd8a87..9313b96 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -191,7 +191,7 @@ class Tree { stdout.write("\n---------------------------------------\nNotifications: "); 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; } // TODO call count() less