From a09c046787beb49c7ba2e5771bd7c6fc690a9ae1 Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Sat, 27 Aug 2022 13:28:36 +0530 Subject: [PATCH] Added color option and made file as default of name all_nostr_events.txt which can be disabled with --disable-file --- .gitignore | 2 ++ bin/nostr_console.dart | 50 +++++++++++++++++++++----- lib/console_ui.dart | 4 +-- lib/event_ds.dart | 8 ++--- lib/relays.dart | 2 +- lib/settings.dart | 81 +++++++++++++++++++++++++++--------------- 6 files changed, 103 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index dbef116..189f2fa 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ doc/api/ *.js_ *.js.deps *.js.map +del*.txt +all_nostr_events.txt diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index 9ed4ab9..20ee8a0 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -19,7 +19,10 @@ const String alignArg = "align"; // can be "left" const String widthArg = "width"; const String maxDepthArg = "maxdepth"; const String eventFileArg = "file"; +const String disableFileArg = "disable-file"; + const String translateArg = "translate"; +const String colorArg = "color"; void printUsage() { print(gUsage); @@ -30,7 +33,9 @@ Future main(List arguments) async { ..addOption(lastdaysArg, abbr:"d") ..addOption(relayArg, abbr:"r") ..addFlag(helpArg, abbr:"h", defaultsTo: false)..addOption(alignArg, abbr:"a") ..addOption(widthArg, abbr:"w")..addOption(maxDepthArg, abbr:"m") - ..addOption(eventFileArg, abbr:"f")..addFlag(translateArg, abbr: "t", defaultsTo: false); + ..addOption(eventFileArg, abbr:"f", defaultsTo: gDefaultEventsFilename)..addFlag(disableFileArg, abbr:"n", defaultsTo: false) + ..addFlag(translateArg, abbr: "t", defaultsTo: false) + ..addOption(colorArg, abbr:"c"); try { ArgResults argResults = parser.parse(arguments); if( argResults[helpArg]) { @@ -97,25 +102,54 @@ Future main(List arguments) async { print("Going to take threads to maximum depth of $gNumLastDays days"); } } - if( argResults[eventFileArg] != null) { - gEventsFilename = argResults[eventFileArg]; - if( gEventsFilename != "") { - print("Going to use file to read from and store events: $gEventsFilename"); + + if( argResults[colorArg] != null) { + String colorGiven = argResults[colorArg].toString().toLowerCase(); + if( gColorMap.containsKey(colorGiven)) { + String color = gColorMap[colorGiven]??""; + if( color == "") { + print("Invalid color."); + } else + { + gCommentColor = color; + print("Going to use color $gCommentColor."); + } + } else { + print("Invalid color."); } } + + if( argResults[disableFileArg]) { + gEventsFilename = ""; + print("Not going to use any file to read/write events."); + } + + String whetherDefault = "the given "; + if( argResults[eventFileArg] != null && !argResults[disableFileArg]) { + if( gDefaultEventsFilename == argResults[eventFileArg]) { + whetherDefault = "default "; + } + + gEventsFilename = argResults[eventFileArg]; + + if( gEventsFilename != "") { + print("Going to use ${whetherDefault}file to read from and store events: $gEventsFilename"); + } + } + + if( gEventsFilename != "") { print("\n"); - stdout.write('Reading events from the given file.......'); + stdout.write('Reading events from ${whetherDefault}file.......'); List eventsFromFile = readEventsFromFile(gEventsFilename); setRelaysIntialEvents(eventsFromFile); eventsFromFile.forEach((element) { element.eventData.kind == 1? numFileEvents++: numFileEvents;}); print("read $numFileEvents posts from file \"$gEventsFilename\""); } if( argResults[requestArg] != null) { - //stdout.write("Got argument request: ${argResults[requestArg]}"); stdout.write('Sending request and waiting for events...'); sendRequest(gListRelayUrls, argResults[requestArg]); - Future.delayed(const Duration(milliseconds: 6000), () { + Future.delayed(const Duration(milliseconds: numWaitSeconds * 2), () { List receivedEvents = getRecievedEvents(); stdout.write("received ${receivedEvents.length - numFileEvents} events\n"); diff --git a/lib/console_ui.dart b/lib/console_ui.dart index 49038f8..fe4c555 100644 --- a/lib/console_ui.dart +++ b/lib/console_ui.dart @@ -13,8 +13,8 @@ Future processNotifications(Tree node) async { List newEventsId = node.insertEvents(getRecievedEvents()); String nameToDisplay = userPrivateKey.length == 64? - "$commentColor${getAuthorName(userPublicKey)}$colorEndMarker": - "${warningColor}You are not signed in$colorEndMarker but are using public key $userPublicKey"; + "$gCommentColor${getAuthorName(userPublicKey)}$colorEndMarker": + "${gWarningColor}You are not signed in$colorEndMarker but are using public key $userPublicKey"; node.printNotifications(newEventsId, nameToDisplay); clearEvents(); }); diff --git a/lib/event_ds.dart b/lib/event_ds.dart index 9d5d8c0..7a666c0 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -317,10 +317,10 @@ class EventData { printDepth(depth); stdout.write("|Message: "); if( isNotification) { - printInColor(contentShifted, notificationColor); + printInColor(contentShifted, gNotificationColor); isNotification = false; } else { - printInColor(contentShifted, commentColor); + printInColor(contentShifted, gCommentColor); } } @@ -345,7 +345,7 @@ class EventData { String reactorId = reactors[i][0]; if( newLikes.contains(reactorId)) { // colorify - reactorNames += notificationColor + getAuthorName(reactorId) + colorEndMarker; + reactorNames += gNotificationColor + getAuthorName(reactorId) + colorEndMarker; } else { reactorNames += getAuthorName(reactorId); } @@ -512,7 +512,7 @@ List readEventsFromFile(String filename) { events.add(e); } } on Exception catch(err) { - print("Cannot open file $gEventsFilename"); + print("cannot open file $gEventsFilename"); } return events; diff --git a/lib/relays.dart b/lib/relays.dart index f808acc..86d5ef0 100644 --- a/lib/relays.dart +++ b/lib/relays.dart @@ -154,7 +154,7 @@ class Relays { return; } }, - onError: (err) { print("\n${warningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay="); print(colorEndMarker); }, + onError: (err) { print("\n${gWarningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay="); print(colorEndMarker); }, onDone: () { if( gDebug != 0) print('Info: In onDone'); } ); } on WebSocketException { diff --git a/lib/settings.dart b/lib/settings.dart index cdea05a..153d91e 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -2,10 +2,9 @@ // for debugging String gCheckEventId = "a4479de655094679cdfb10f347521aa58f24717cdc5ddba89fb346453a8a99ed"; - String gRemoteAdminPubkey = ""; -const int numWaitSeconds = 3000; +const int numWaitSeconds = 3000; // global counters of total events read or processed int numFileEvents = 0, numUserEvents = 0, numFeedEvents = 0, numOtherEvents = 0; @@ -16,10 +15,9 @@ List gListRelayUrls = [defaultServerUrl, "wss://nostr-pub.wellorder.net", "wss://relay.damus.io"]; - // name of executable const String exename = "nostr_console"; -const String version = "0.0.6"; +const String version = "0.0.7"; // well known disposable test private key const String gDefaultPrivateKey = "9d00d99c8dfad84534d3b395280ca3b3e81be5361d69dc0abf8e0fdf5a9d52f9"; @@ -42,12 +40,31 @@ const int gDefaultMaxDepth = 4; int maxDepthAllowed = gDefaultMaxDepth; const int leftShiftThreadsBy = 2; + +// text color +const String defaultTextColor = "green"; + +const String greenColor = "\x1B[32m"; // green +const String cyanColor = "\x1b[36m"; // cyan +const String whiteColor = "\x1b[37m"; // white +const String blackColor = "\x1b[30m"; // black +const String redColor = "\x1B[31m"; // red +const String blueColor = "\x1b[34m"; // blue + +Map gColorMap = { "green": greenColor, + "cyan" : cyanColor, + "white": whiteColor, + "black": blackColor, + "red" : redColor, + "blue" : blueColor}; + // 33 yellow, 31 red, 34 blue, 35 magenta. Add 60 for bright versions. -const String commentColor = "\x1B[32m"; // green -const String notificationColor = "\x1b[36m"; // cyan -const String warningColor = "\x1B[31m"; // red +String gCommentColor = greenColor; +String gNotificationColor = cyanColor; // cyan +String gWarningColor = redColor; // red const String colorEndMarker = "\x1B[0m"; + //String defaultServerUrl = 'wss://relay.damus.io'; const String nostrRelayUnther = 'wss://nostr-relay.untethr.me'; String defaultServerUrl = nostrRelayUnther; @@ -57,7 +74,8 @@ const String gDummyAccountPubkey = "Non"; // By default the threads that were started in last one day are shown // this can be changed with 'days' command line argument -int gNumLastDays = 1; +const int gDefaultNumLastDays = 1; +int gNumLastDays = gDefaultNumLastDays; // global user names from kind 0 events, mapped from public key to user name Map gKindONames = {}; @@ -75,7 +93,7 @@ List gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2 "6a9eb714c2889aa32e449cfbb7854bc9780feed4ff3d887e03910dcb22aa560a" // "bible bot" ]; -//const String gDefaultEventsFilename = "events_store_nostr.txt"; +const String gDefaultEventsFilename = "all_nostr_events.txt"; String gEventsFilename = ""; // is set in arguments, and if set, then file is read from and written to const String gUsage = """$exename version $version @@ -85,27 +103,32 @@ usage: $exename [OPTIONS] OPTIONS - --pubkey The hex public key of user whose events and feed are shown. Default is a hard-coded - well known private key. When given, posts/replies can't be sent. Same as -p - --prikey The hex private key of user whose events and feed are shown. Also used to sign events - sent. Default is a hard-coded well known private key. Same as -k - --relay The relay url that is used as main relay. Default is $nostrRelayUnther . Same as -r - --days The latest number of days for which events are shown. Default is 1. Same as -d - --request This request is sent verbatim to the default relay. It can be used to recieve all events - from a relay. If not provided, then events for default or given user are shown. Same as -q - --file Read from given file, if it is present, and at the end of the program execution, write - to it all the events (including the ones read, and any new received). Same as -f - --translate This flag, if present, will make the application translate some of the recent posts using - google translate. Save as -t + -p, --pubkey The hex public key of user whose events and feed are shown. Default is a hard-coded + well known private key. When given, posts/replies can't be sent. + -k, --prikey The hex private key of user whose events and feed are shown. Also used to sign events + sent. Default is a hard-coded well known private key. + -r, --relay The relay url that is used as main relay. Default is $nostrRelayUnther. + -d, --days The latest number of days for which events are shown. Default is $gDefaultNumLastDays. + -q, --request This request is sent verbatim to the default relay. It can be used to recieve all events + from a relay. If not provided, then events for default or given user are shown. + -f, --file Read from given file, if it is present, and at the end of the program execution, write + to it all the events (including the ones read, and any new received). Even if not given, + the default is to read from and write to $gDefaultEventsFilename . Can be turned off by + the --disable-file flag + -n, --disable-file When turned on, even the default filename is not read from. + -t, --translate This flag, if present, will make the application translate some of the recent posts using + google translate. + UI Options - --align When "left" is given as option to this argument, then the text is aligned to left. By default - the posts or text is aligned to the center of the terminal. Same as -a - --width This specifies how wide you want the text to be, in number of columns. Default is $gDefaultTextWidth. - Cant be less than $gMinValidTextWidth. Same as -w - --maxdepth The maximum depth to which the threads can be displayed. Minimum is $gMinimumDepthAllowed and - maximum allowed is $gMaximumDepthAllowed. Same as -m - --help Print this usage message and exit. Same as -h - + -a, --align When "left" is given as option to this argument, then the text is aligned to left. By default + the posts or text is aligned to the center of the terminal. + -w, --width This specifies how wide you want the text to be, in number of columns. Default is $gDefaultTextWidth. + Cant be less than $gMinValidTextWidth. + -m, --maxdepth The maximum depth to which the threads can be displayed. Minimum is $gMinimumDepthAllowed and + maximum allowed is $gMaximumDepthAllowed. + -c, --color Color option can be green, cyan, white, black, red and blue. + -h, --help Print this usage message and exit. + """; const String helpAndAbout =