mirror of
https://github.com/vishalxl/nostr_console.git
synced 2025-06-17 12:21:08 +02:00
added options align and width, which control display. now text can be left or center aligned. default is center. fixed issue with dashes when re-aligning tree
This commit is contained in:
parent
3c9110b539
commit
f7b78aa171
@ -23,6 +23,8 @@ const String prikeyArg = "prikey";
|
|||||||
const String lastdaysArg = "days";
|
const String lastdaysArg = "days";
|
||||||
const String relayArg = "relay";
|
const String relayArg = "relay";
|
||||||
const String helpArg = "help";
|
const String helpArg = "help";
|
||||||
|
const String alignArg = "align"; // can be "left"
|
||||||
|
const String widthArg = "width";
|
||||||
|
|
||||||
// By default the threads that were started in last one day are shown
|
// By default the threads that were started in last one day are shown
|
||||||
// this can be changed with 'days' command line argument
|
// this can be changed with 'days' command line argument
|
||||||
@ -38,9 +40,13 @@ usage: $exename [OPTIONS]
|
|||||||
--prikey <private key> The hex private key of user whose events and feed are shown. Also used to sign events
|
--prikey <private key> 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 -p
|
||||||
--relay <relay wss url> The relay url that is used as main relay. Default is $defaultServerUrl . Same as -r
|
--relay <relay wss url> The relay url that is used as main relay. Default is $defaultServerUrl . Same as -r
|
||||||
--days <N> The latest number of days for which events are shown. Default is 1. Same as -d
|
--days <N as num> The latest number of days for which events are shown. Default is 1. Same as -d
|
||||||
--request <REQ string> This request is sent verbatim to the default relay. It can be used to recieve all events
|
--request <REQ string> 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
|
from a relay. If not provided, then events for default or given user are shown. Same as -q
|
||||||
|
--align <left> 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 <width as num> This specifies how wide you want the text to be, in number of columns. Default is 80.
|
||||||
|
Cant be less than $gMinValidScreenWidth. Same as -c
|
||||||
--help Print this usage message and exit. Same as -h
|
--help Print this usage message and exit. Same as -h
|
||||||
|
|
||||||
""";
|
""";
|
||||||
@ -116,7 +122,11 @@ Future<void> terminalMenuUi(Tree node, var contactList) async {
|
|||||||
print('You picked: $option');
|
print('You picked: $option');
|
||||||
switch(option) {
|
switch(option) {
|
||||||
case 1:
|
case 1:
|
||||||
//print("in display events option");
|
// align the text again in case the window size has been changed
|
||||||
|
if( gAlignment == "center") {
|
||||||
|
gNumLeftMarginSpaces = (stdout.terminalColumns - 80 )~/2;
|
||||||
|
}
|
||||||
|
|
||||||
node.printTree(0, true, DateTime.now().subtract(Duration(days:numLastDays)));
|
node.printTree(0, true, DateTime.now().subtract(Duration(days:numLastDays)));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -161,7 +171,8 @@ Future<void> main(List<String> arguments) async {
|
|||||||
|
|
||||||
final parser = ArgParser()..addOption(requestArg, abbr: 'q') ..addOption(prikeyArg, abbr:"p")
|
final parser = ArgParser()..addOption(requestArg, abbr: 'q') ..addOption(prikeyArg, abbr:"p")
|
||||||
..addOption(lastdaysArg, abbr:"d") ..addOption(relayArg, abbr:"r")
|
..addOption(lastdaysArg, abbr:"d") ..addOption(relayArg, abbr:"r")
|
||||||
..addFlag(helpArg, abbr:"h", defaultsTo: false);
|
..addFlag(helpArg, abbr:"h", defaultsTo: false)..addOption(alignArg, abbr:"a")
|
||||||
|
..addOption(widthArg, abbr:"c");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ArgResults argResults = parser.parse(arguments);
|
ArgResults argResults = parser.parse(arguments);
|
||||||
@ -179,12 +190,34 @@ Future<void> main(List<String> arguments) async {
|
|||||||
userPrivateKey = argResults[prikeyArg];
|
userPrivateKey = argResults[prikeyArg];
|
||||||
userPublicKey = getPublicKey(userPrivateKey);
|
userPublicKey = getPublicKey(userPrivateKey);
|
||||||
}
|
}
|
||||||
if( argResults[lastdaysArg] != null) {
|
|
||||||
|
|
||||||
|
if( argResults[lastdaysArg] != null) {
|
||||||
numLastDays = int.parse(argResults[lastdaysArg]);
|
numLastDays = int.parse(argResults[lastdaysArg]);
|
||||||
print("Going to show posts for last $numLastDays days");
|
print("Going to show posts for last $numLastDays days");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( argResults[widthArg] != null) {
|
||||||
|
int tempScreenWidth = int.parse(argResults[widthArg]);
|
||||||
|
if( tempScreenWidth < gMinValidScreenWidth ) {
|
||||||
|
print("Screen-width cannot be less than $gMinValidScreenWidth. Going to use the defalt value of $screenWidth");
|
||||||
|
} else {
|
||||||
|
print("Going to use $screenWidth columns for text on screen.");
|
||||||
|
screenWidth = tempScreenWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// can be computed only after screenWidth has been found
|
||||||
|
gNumLeftMarginSpaces = (stdout.terminalColumns - 80 )~/2;
|
||||||
|
|
||||||
|
// undo above if left option is given
|
||||||
|
if( argResults[alignArg] != null) {
|
||||||
|
if( argResults[alignArg] == "left" ) {
|
||||||
|
print("Going to align to left.");
|
||||||
|
gAlignment = "left";
|
||||||
|
gNumLeftMarginSpaces = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if( argResults[requestArg] != null) {
|
if( argResults[requestArg] != null) {
|
||||||
stdout.write("Got argument request ${argResults[requestArg]}");
|
stdout.write("Got argument request ${argResults[requestArg]}");
|
||||||
sendRequest("wss://nostr-pub.wellorder.net", argResults[requestArg]);
|
sendRequest("wss://nostr-pub.wellorder.net", argResults[requestArg]);
|
||||||
|
@ -2,15 +2,21 @@ import 'dart:io';
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
const int screenWidth = 120;
|
const int gMinValidScreenWidth = 60;
|
||||||
|
const int defaultScreenWidth = 80;
|
||||||
|
int screenWidth = defaultScreenWidth;
|
||||||
const int spacesPerDepth = 8;
|
const int spacesPerDepth = 8;
|
||||||
|
int gNumLeftMarginSpaces = 0; // this number is modified in main
|
||||||
|
String gAlignment = "center"; // is modified in main if --align argument is given
|
||||||
|
|
||||||
const int maxDepthAllowed = 7;
|
const int maxDepthAllowed = 4;
|
||||||
const int leftShiftThreadsBy = 3;
|
const int leftShiftThreadsBy = 2;
|
||||||
|
|
||||||
// 33 yellow, 31 red, 34 blue, 35 magenta. Add 60 for bright versions.
|
// 33 yellow, 31 red, 34 blue, 35 magenta. Add 60 for bright versions.
|
||||||
const String commentColor = "\x1B[32m"; // green
|
const String commentColor = "\x1B[32m"; // green
|
||||||
const String notificationColor = "\x1b[36m"; // cyan
|
const String notificationColor = "\x1b[36m"; // cyan
|
||||||
|
const String warningColor = "\x1B[31m"; // red
|
||||||
|
const String colorEndMarker = "\x1B[0m";
|
||||||
|
|
||||||
//String defaultServerUrl = 'wss://relay.damus.io';
|
//String defaultServerUrl = 'wss://relay.damus.io';
|
||||||
String defaultServerUrl = 'wss://nostr-relay.untethr.me';
|
String defaultServerUrl = 'wss://nostr-relay.untethr.me';
|
||||||
@ -26,19 +32,32 @@ List<String> gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2
|
|||||||
int gDebug = 0;
|
int gDebug = 0;
|
||||||
|
|
||||||
void printDepth(int d) {
|
void printDepth(int d) {
|
||||||
for( int i = 0; i < spacesPerDepth * d ; i++) {
|
for( int i = 0; i < spacesPerDepth * d + gNumLeftMarginSpaces; i++) {
|
||||||
stdout.write(" ");
|
stdout.write(" ");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String getNumSpaces(int num) {
|
||||||
|
String s = "";
|
||||||
|
for( int i = 0; i < num; i++) {
|
||||||
|
s += " ";
|
||||||
}
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getNumDashes(int num) {
|
||||||
|
String s = "";
|
||||||
|
for( int i = 0; i < num; i++) {
|
||||||
|
s += "-";
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
String rightShiftContent(String s, int numSpaces) {
|
String rightShiftContent(String s, int numSpaces) {
|
||||||
String newString = "";
|
String newString = "";
|
||||||
int newlineCounter = 0;
|
int newlineCounter = 0;
|
||||||
String spacesString = "";
|
String spacesString = getNumSpaces(numSpaces + gNumLeftMarginSpaces);
|
||||||
|
|
||||||
for( int i = 0; i < numSpaces ; i++) {
|
|
||||||
spacesString += " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i = 0; i < s.length; i++) {
|
for(int i = 0; i < s.length; i++) {
|
||||||
if( s[i] == '\n') {
|
if( s[i] == '\n') {
|
||||||
@ -182,7 +201,7 @@ class EventData {
|
|||||||
void printEventData(int depth) {
|
void printEventData(int depth) {
|
||||||
int n = 3;
|
int n = 3;
|
||||||
String maxN(String v) => v.length > n? v.substring(0,n) : v.substring(0, v.length);
|
String maxN(String v) => v.length > n? v.substring(0,n) : v.substring(0, v.length);
|
||||||
void printGreen(String s, String commentColor) => stdout.supportsAnsiEscapes ?stdout.write("$commentColor$s\x1B[0m"):stdout.write(s);
|
void printGreen(String s, String commentColor) => stdout.supportsAnsiEscapes ?stdout.write("$commentColor$s$colorEndMarker"):stdout.write(s);
|
||||||
|
|
||||||
DateTime dTime = DateTime.fromMillisecondsSinceEpoch(createdAt *1000);
|
DateTime dTime = DateTime.fromMillisecondsSinceEpoch(createdAt *1000);
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ class Relays {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// following is too restrictive. TODO improve it
|
// following is too restrictive casuse changed sinceWhen is not considered. TODO improve it
|
||||||
for(int i = 0; i < users.length; i++) {
|
for(int i = 0; i < users.length; i++) {
|
||||||
if( users[i] == publicKey) {
|
if( users[i] == publicKey) {
|
||||||
return;
|
return;
|
||||||
@ -118,8 +118,8 @@ class Relays {
|
|||||||
print( 'exception in fromJson for event');
|
print( 'exception in fromJson for event');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: (e) { print("in onError"); print(e); },
|
onError: (e) { print("\n${warningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay=<relay wss url>"); print(colorEndMarker); },
|
||||||
onDone: () { print('in onDone'); }
|
onDone: () { print('Info: In onDone'); }
|
||||||
);
|
);
|
||||||
} on WebSocketException {
|
} on WebSocketException {
|
||||||
print('WebSocketException exception');
|
print('WebSocketException exception');
|
||||||
@ -172,8 +172,6 @@ class Relays {
|
|||||||
relays.forEach((key, value) {
|
relays.forEach((key, value) {
|
||||||
print("for relay: $key");
|
print("for relay: $key");
|
||||||
print("$value\n");
|
print("$value\n");
|
||||||
|
|
||||||
|
|
||||||
String? reason = value.closeReason;
|
String? reason = value.closeReason;
|
||||||
print( reason??"reason not found");
|
print( reason??"reason not found");
|
||||||
});
|
});
|
||||||
@ -214,7 +212,6 @@ void getMultiUserEvents(serverUrl, publicKeys, numUserEvents) {
|
|||||||
relays.getMultiUserEvents(serverUrl, publicKeys, numUserEvents);
|
relays.getMultiUserEvents(serverUrl, publicKeys, numUserEvents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void sendRequest(serverUrl, request) {
|
void sendRequest(serverUrl, request) {
|
||||||
relays.sendRequest(serverUrl, request);
|
relays.sendRequest(serverUrl, request);
|
||||||
}
|
}
|
||||||
|
@ -34,8 +34,8 @@ class Tree {
|
|||||||
mAllEvents[parentId]?.addChildNode(value); // in this if condition this will get called
|
mAllEvents[parentId]?.addChildNode(value); // in this if condition this will get called
|
||||||
} else {
|
} else {
|
||||||
// in case where the parent of the new event is not in the pool of all events,
|
// in case where the parent of the new event is not in the pool of all events,
|
||||||
// then we create a dummy event and put it at top ( or make this a top event?)
|
// then we create a dummy event and put it at top ( or make this a top event?) TODO handle so that this can be replied to, and is fetched
|
||||||
Tree dummyTopNode = Tree(Event("","",EventData("Unk","Non", value.e.eventData.createdAt , 0, "Unknown/Dummy Event", [], [], [], [[]]), [""], "[json]"), [], {}, []);
|
Tree dummyTopNode = Tree(Event("","",EventData("Unk" ,"Non", value.e.eventData.createdAt , 0, "Unknown parent event", [], [], [], [[]]), [""], "[json]"), [], {}, []);
|
||||||
dummyTopNode.addChildNode(value);
|
dummyTopNode.addChildNode(value);
|
||||||
tempWithoutParent.add(value.e.eventData.id);
|
tempWithoutParent.add(value.e.eventData.id);
|
||||||
|
|
||||||
@ -142,8 +142,7 @@ class Tree {
|
|||||||
if( depth > maxDepthAllowed) {
|
if( depth > maxDepthAllowed) {
|
||||||
depth = maxDepthAllowed - leftShiftThreadsBy;
|
depth = maxDepthAllowed - leftShiftThreadsBy;
|
||||||
printDepth(depth+1);
|
printDepth(depth+1);
|
||||||
stdout.write("+-------------------------------+\n");
|
stdout.write("+${getNumDashes((leftShiftThreadsBy + 1) * 8 - 1)}+\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
children[i].printTree(depth+1, false, newerThan);
|
children[i].printTree(depth+1, false, newerThan);
|
||||||
}
|
}
|
||||||
@ -225,6 +224,11 @@ class Tree {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( latestEventId.isEmpty) {
|
||||||
|
// search for it in the dummy event id's
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//print("latestEventId = $latestEventId");
|
//print("latestEventId = $latestEventId");
|
||||||
if( latestEventId.isNotEmpty) {
|
if( latestEventId.isNotEmpty) {
|
||||||
strTags = '["e","$latestEventId"]';
|
strTags = '["e","$latestEventId"]';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user