From dc7ddaffb1b5cf0b14bbc5fdd754f5257ad6dfca Mon Sep 17 00:00:00 2001 From: vishalxl <> Date: Fri, 19 Aug 2022 00:04:41 +0530 Subject: [PATCH] added pubkey and maxdepth arguments. Fixed so that re-shifting of thread to left shows where the reshifting part ends --- bin/nostr_console.dart | 65 +++++++++++++++++++++++++++++++----------- lib/event_ds.dart | 16 +++++++---- lib/relays.dart | 6 ++-- lib/tree_ds.dart | 16 +++++++++-- 4 files changed, 74 insertions(+), 29 deletions(-) diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index b5c38f1..35016d6 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -12,19 +12,21 @@ String exename = "nostr_console"; String version = "0.0.2"; // well known disposable test private key -const String testPrivateKey = "9d00d99c8dfad84534d3b395280ca3b3e81be5361d69dc0abf8e0fdf5a9d52f9"; -const String testPublicKey = "e8caa2028a7090ffa85f1afee67451b309ba2f9dee655ec8f7e0a02c29388180"; -String userPrivateKey = testPrivateKey; -String userPublicKey = testPublicKey; +const String gDefaultPrivateKey = "9d00d99c8dfad84534d3b395280ca3b3e81be5361d69dc0abf8e0fdf5a9d52f9"; +const String gDefaultPublicKey = "e8caa2028a7090ffa85f1afee67451b309ba2f9dee655ec8f7e0a02c29388180"; +String userPrivateKey = gDefaultPrivateKey; +String userPublicKey = gDefaultPublicKey; -// program arguments1 -const String requestArg = "request"; +// program arguments +const String pubkeyArg = "pubkey"; const String prikeyArg = "prikey"; const String lastdaysArg = "days"; const String relayArg = "relay"; +const String requestArg = "request"; const String helpArg = "help"; const String alignArg = "align"; // can be "left" -const String widthArg = "width"; +const String widthArg = "width"; +const String maxDepthArg = "maxdepth"; // By default the threads that were started in last one day are shown // this can be changed with 'days' command line argument @@ -32,21 +34,27 @@ int numLastDays = 1; void printUsage() { String usage = """$exename version $version +The nostr console client built using dart. 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 -p + 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 $defaultServerUrl . 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 + 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 -c + 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 """; @@ -131,6 +139,11 @@ Future terminalMenuUi(Tree node, var contactList) async { break; case 2: + // in case the program was invoked with --pubkey, then user can't send messages + if( userPrivateKey == "") { + print("Since no user private key has been supplied, messages can't sent. Invoke with --prikey \n"); + break; + } stdout.write("Type comment to post/reply: "); String? $contentVar = stdin.readLineSync(); String content = $contentVar??""; @@ -169,10 +182,10 @@ Future terminalMenuUi(Tree node, var contactList) async { Future main(List arguments) async { - final parser = ArgParser()..addOption(requestArg, abbr: 'q') ..addOption(prikeyArg, abbr:"p") + final parser = ArgParser()..addOption(requestArg, abbr: 'q') ..addOption(pubkeyArg, abbr:"p")..addOption(prikeyArg, abbr:"k") ..addOption(lastdaysArg, abbr:"d") ..addOption(relayArg, abbr:"r") ..addFlag(helpArg, abbr:"h", defaultsTo: false)..addOption(alignArg, abbr:"a") - ..addOption(widthArg, abbr:"c"); + ..addOption(widthArg, abbr:"w")..addOption(maxDepthArg, abbr:"m"); try { ArgResults argResults = parser.parse(arguments); @@ -181,14 +194,21 @@ Future main(List arguments) async { return; } - if( argResults[relayArg] != null) { - defaultServerUrl = argResults[relayArg]; - print("Going to use relay: $defaultServerUrl"); + if( argResults[pubkeyArg] != null) { + userPublicKey = argResults[pubkeyArg]; + userPrivateKey = ""; + print("Going to use public key $userPublicKey. You will not be able to send posts/replies."); } if( argResults[prikeyArg] != null) { userPrivateKey = argResults[prikeyArg]; userPublicKey = getPublicKey(userPrivateKey); + print("Going to use the provided private key"); + } + + if( argResults[relayArg] != null) { + defaultServerUrl = argResults[relayArg]; + print("Going to use relay: $defaultServerUrl"); } if( argResults[lastdaysArg] != null) { @@ -201,8 +221,8 @@ Future main(List arguments) async { if( tempTextWidth < gMinValidTextWidth ) { print("Text-width cannot be less than $gMinValidTextWidth. Going to use the defalt value of $gTextWidth"); } else { - print("Going to use $gTextWidth columns for text on screen."); gTextWidth = tempTextWidth; + print("Going to use $gTextWidth columns for text on screen."); } } @@ -210,12 +230,23 @@ Future main(List arguments) async { gNumLeftMarginSpaces = (stdout.terminalColumns - gTextWidth )~/2; // undo above if left option is given - if( argResults[alignArg] != null) { + if( argResults[alignArg] != null ) { if( argResults[alignArg] == "left" ) { print("Going to align to left."); gAlignment = "left"; gNumLeftMarginSpaces = 0; - } + } + } + + if( argResults[maxDepthArg] != null) { + + int tempMaxDepth = int.parse(argResults[maxDepthArg]); + if( tempMaxDepth < gMinimumDepthAllowed || tempMaxDepth > gMaximumDepthAllowed) { + print("Maximum depth cannot be less than $gMinimumDepthAllowed and cannot be more than $gMaximumDepthAllowed. Going to use the default maximum depth, which is $gDefaultMaxDepth."); + } else { + maxDepthAllowed = tempMaxDepth; + print("Going to take threads to maximum depth of $numLastDays days"); + } } if( argResults[requestArg] != null) { diff --git a/lib/event_ds.dart b/lib/event_ds.dart index 9fd684b..a115d14 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -2,15 +2,19 @@ import 'dart:io'; import 'dart:convert'; import 'package:intl/intl.dart'; -const int gMinValidTextWidth = 60; -const int gDefaultTextWidth = 120; -int gTextWidth = gDefaultTextWidth; -const int gSpacesPerDepth = 8; +const int gMinValidTextWidth = 60; // minimum text width acceptable +const int gDefaultTextWidth = 120; // default text width +int gTextWidth = gDefaultTextWidth; // is changed by --width option +const int gSpacesPerDepth = 8; // constant int gNumLeftMarginSpaces = 0; // this number is modified in main String gAlignment = "center"; // is modified in main if --align argument is given -const int maxDepthAllowed = 4; -const int leftShiftThreadsBy = 2; +// after depth of maxDepthAllowed the thread is re-aligned to left by leftShiftThreadBy +const int gMinimumDepthAllowed = 2; +const int gMaximumDepthAllowed = 12; +const int gDefaultMaxDepth = 4; +int maxDepthAllowed = gDefaultMaxDepth; +const int leftShiftThreadsBy = 2; // 33 yellow, 31 red, 34 blue, 35 magenta. Add 60 for bright versions. const String commentColor = "\x1B[32m"; // green diff --git a/lib/relays.dart b/lib/relays.dart index 06a8ec4..b024771 100644 --- a/lib/relays.dart +++ b/lib/relays.dart @@ -119,7 +119,7 @@ class Relays { } }, onError: (e) { print("\n${warningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay="); print(colorEndMarker); }, - onDone: () { print('Info: In onDone'); } + onDone: () { if( gDebug != 0) print('Info: In onDone'); } ); } on WebSocketException { print('WebSocketException exception'); @@ -147,8 +147,8 @@ class Relays { relays[relay] = fws; fws.stream.listen( (d) {}, // - onError: (e) { print("in onError"); print(e); }, - onDone: () { print('in onDone'); } + onError: (e) { print("\n${warningColor}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('in onDone'); } ); } on WebSocketException { print('WebSocketException exception'); diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index 74df90b..dc3f55c 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -119,6 +119,7 @@ class Tree { depth = depth - 1; } + bool leftShifted = false; for( int i = 0; i < children.length; i++) { if(!onlyPrintChildren) { stdout.write("\n"); @@ -137,15 +138,24 @@ class Tree { } // if the thread becomes too 'deep' then reset its depth, so that its - // children will not be displayed too much the right, but are shifted - // left by about places + // children will not be displayed too much on the right, but are shifted + // left by about places if( depth > maxDepthAllowed) { depth = maxDepthAllowed - leftShiftThreadsBy; printDepth(depth+1); - stdout.write("+${getNumDashes((leftShiftThreadsBy + 1) * 8 - 1)}+\n"); + stdout.write("<${getNumDashes((leftShiftThreadsBy + 1) * gSpacesPerDepth - 1)}+\n"); + leftShifted = true; } + children[i].printTree(depth+1, false, newerThan); } + + if( leftShifted) { + stdout.write("\n"); + printDepth(depth+1); + print(">"); + } + } Tree getTopTree(Tree t) {