formatting changes, and improved mentions logic when displaying

This commit is contained in:
vishalxl 2022-08-19 21:22:16 +05:30
parent 87b4ce846c
commit 8bf61d346d
2 changed files with 40 additions and 38 deletions

View File

@ -4,12 +4,9 @@ import 'package:bip340/bip340.dart';
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/tree_ds.dart';
import 'package:nostr_console/relays.dart';
import 'package:nostr_console/console_ui.dart';
import 'package:args/args.dart';
// program arguments
const String pubkeyArg = "pubkey";
const String prikeyArg = "prikey";
@ -19,7 +16,7 @@ const String requestArg = "request";
const String helpArg = "help";
const String alignArg = "align"; // can be "left"
const String widthArg = "width";
const String maxDepthArg = "maxdepth";
const String maxDepthArg = "maxdepth";
void printUsage() {
String usage = """$exename version $version
@ -158,7 +155,6 @@ Future<void> main(List<String> arguments) async {
stdout.write("...received ${getRecievedEvents().length} events made by the user\n");
// get the latest kind 3 event for the user, which lists his 'follows' list
List<String> contactList = [];
int latestContactsTime = 0, latestContactIndex = -1;
for( int i = 0; i < getRecievedEvents().length; i++) {
var e = getRecievedEvents()[i];
@ -168,7 +164,8 @@ Future<void> main(List<String> arguments) async {
}
}
// if contact list was found, get user's feed
// if contact list was found, get user's feed, and keep the contact list for later use
List<String> contactList = [];
if (latestContactIndex != -1) {
contactList = getContactFeed(getRecievedEvents()[latestContactIndex].eventData.contactList, 300);
}
@ -183,7 +180,6 @@ Future<void> main(List<String> arguments) async {
// get mentioned ptags, and then get the events for those users
List<String> pTags = getpTags(getRecievedEvents());
getMultiUserEvents(defaultServerUrl, pTags, 300);
stdout.write('Waiting for rest of events to come in.....');
@ -193,8 +189,10 @@ Future<void> main(List<String> arguments) async {
numOtherEvents = numOtherEvents - numFeedEvents - numUserEvents;
stdout.write("received $numOtherEvents other events\n");
// get all events in Tree form
Tree node = getTree(getRecievedEvents());
clearEvents();
// call the mein UI function
mainMenuUi(node, contactList);
});

View File

@ -2,6 +2,16 @@ import 'dart:io';
import 'dart:convert';
import 'package:intl/intl.dart';
// name of executable
String exename = "nostr_console";
String version = "0.0.3";
// well known disposable test private key
const String gDefaultPrivateKey = "9d00d99c8dfad84534d3b395280ca3b3e81be5361d69dc0abf8e0fdf5a9d52f9";
const String gDefaultPublicKey = "e8caa2028a7090ffa85f1afee67451b309ba2f9dee655ec8f7e0a02c29388180";
String userPrivateKey = gDefaultPrivateKey;
String userPublicKey = gDefaultPublicKey;
const int gMinValidTextWidth = 60; // minimum text width acceptable
const int gDefaultTextWidth = 120; // default text width
int gTextWidth = gDefaultTextWidth; // is changed by --width option
@ -35,23 +45,15 @@ int gNumLastDays = 1;
// global user names from kind 0 events, mapped from public key to user name
Map<String, String> gKindONames = {};
// global reactions entry. Mapts from <event reacted to id, List of Reactors>
// reach Reactor is a list of 2-elements ( first is public id of reactor, second is comment )
// global reactions entry. Map of form <if of event reacted to, List of Reactors>
// reach Reactor is a list of 2-elements ( first is public id of reactor, second is comment)
Map< String, List<List<String>> > gReactions = {};
// bots ignored to reduce spam
List<String> gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2b5791c512", // robosats orderbook
"887645fef0ce0c3c1218d2f5d8e6132a19304cdc57cd20281d082f38cfea0072", // bestofhn
"887645fef0ce0c3c1218d2f5d8e6132a19304cdc57cd20281d082f38cfea0072", // bestofhn
"f4161c88558700d23af18d8a6386eb7d7fed769048e1297811dcc34e86858fb2" // bitcoin_bot
];
// well known disposable test private key
const String gDefaultPrivateKey = "9d00d99c8dfad84534d3b395280ca3b3e81be5361d69dc0abf8e0fdf5a9d52f9";
const String gDefaultPublicKey = "e8caa2028a7090ffa85f1afee67451b309ba2f9dee655ec8f7e0a02c29388180";
String userPrivateKey = gDefaultPrivateKey;
String userPublicKey = gDefaultPublicKey;
// name of executable
String exename = "nostr_console";
String version = "0.0.2";
int gDebug = 0;
@ -77,7 +79,6 @@ String getNumDashes(int num) {
return s;
}
String rightShiftContent(String s, int numSpaces) {
String newString = "";
int newlineCounter = 0;
@ -110,15 +111,15 @@ class Contact {
}
class EventData {
String id;
String pubkey;
int createdAt;
int kind;
String content;
List<String> eTagsRest;// rest of e tags
List<String> pTags;// list of p tags for kind:1
String id;
String pubkey;
int createdAt;
int kind;
String content;
List<String> eTagsRest;// rest of e tags
List<String> pTags;// list of p tags for kind:1
List<List<String>> tags;
bool isNotification; // whether its to be highlighted using highlight color
bool isNotification; // whether its to be highlighted using highlight color
List<Contact> contactList = []; // used for kind:3 events, which is contact list event
@ -134,8 +135,8 @@ class EventData {
factory EventData.fromJson(dynamic json) {
List<Contact> contactList = [];
List<String> eTagsRead = [];
List<String> pTagsRead = [];
List<String> eTagsRead = [];
List<String> pTagsRead = [];
List<List<String>> tagsRead = [];
var jsonTags = json['tags'];
@ -203,10 +204,17 @@ class EventData {
return content;
}
List<String> placeHolders = ["#[0]", "#[1]", "#[2]", "#[3]", "#[4]", "#[5]" ];
// just check if there is any square bracket in comment, if not we return
String squareBracketStart = "[", squareBracketEnd = "]";
if( !content.contains(squareBracketStart) || !content.contains(squareBracketEnd) ) {
return content;
}
// replace the patterns
List<String> placeHolders = ["#[0]", "#[1]", "#[2]", "#[3]", "#[4]", "#[5]", "#[6]", "#[7]" ];
for(int i = 0; i < placeHolders.length; i++) {
int index = -1;
Pattern p = placeHolders[i];
int index = -1;
Pattern p = placeHolders[i];
if( (index = content.indexOf(p)) != -1 ) {
if( i >= tags.length) {
continue;
@ -215,12 +223,8 @@ class EventData {
if( tags[i].isEmpty || tags[i].length < 2) {
continue;
}
String author = getAuthorName(tags[i][1]);
//print("\n\nauthor mention: i = $i index = $index tags[i][1] = ${tags[i][1]} author = $author");
//print("tags = $tags");
//print("in expandMentions: changing content at index i = $i");
content = "${content.substring(0, index)} @$author${content.substring(index + 4)}";
}
}