mirror of
https://github.com/vishalxl/nostr_console.git
synced 2025-04-02 17:08:12 +02:00
added basic page support for channels with too many messages. 15 msg per page now are displayed.
This commit is contained in:
parent
5a24daa7c2
commit
1137068f68
@ -163,7 +163,6 @@ Future<void> main(List<String> arguments) async {
|
||||
}
|
||||
|
||||
gEventsFilename = argResults[eventFileArg];
|
||||
|
||||
if( gEventsFilename != "") {
|
||||
print("Going to use ${whetherDefault}file to read from and store events: $gEventsFilename");
|
||||
}
|
||||
@ -182,10 +181,19 @@ Future<void> main(List<String> arguments) async {
|
||||
print("read $numFileEvents posts from file $gEventsFilename");
|
||||
}
|
||||
|
||||
// process request string. If this is blank then the application only reads from file and does not connect to internet.
|
||||
if( argResults[requestArg] != null) {
|
||||
stdout.write('Sending request ${argResults[requestArg]} and waiting for events...');
|
||||
sendRequest(gListRelayUrls, argResults[requestArg]);
|
||||
Future.delayed(const Duration(milliseconds: numWaitSeconds * 2), () {
|
||||
int numWaitSeconds = gDefaultNumWaitSeconds;
|
||||
|
||||
if( argResults[requestArg] != "") {
|
||||
stdout.write('Sending request ${argResults[requestArg]} and waiting for events...');
|
||||
sendRequest(gListRelayUrls, argResults[requestArg]);
|
||||
} else {
|
||||
numWaitSeconds = 0;
|
||||
gEventsFilename = ""; // so it wont write it back to keep it faster ( and since without internet no new event is there to be written )
|
||||
}
|
||||
|
||||
Future.delayed(Duration(milliseconds: numWaitSeconds * 2), () {
|
||||
Set<Event> receivedEvents = getRecievedEvents();
|
||||
stdout.write("received ${receivedEvents.length - numFileEvents} events\n");
|
||||
|
||||
@ -195,9 +203,8 @@ Future<void> main(List<String> arguments) async {
|
||||
//clearEvents(); // cause we have consumed them above
|
||||
if( gDebug > 0) stdout.write("Total events of kind 1 in created tree: ${node.count()} events\n");
|
||||
clearEvents();
|
||||
mainMenuUi(node, []);
|
||||
// call main menu
|
||||
|
||||
|
||||
mainMenuUi(node, []);
|
||||
});
|
||||
return;
|
||||
}
|
||||
@ -216,7 +223,7 @@ Future<void> main(List<String> arguments) async {
|
||||
// then get the events of user-id's mentioned in p-tags of received events
|
||||
// then display them all
|
||||
stdout.write('Waiting for user posts to come in.....');
|
||||
Future.delayed(const Duration(milliseconds: numWaitSeconds), () {
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds), () {
|
||||
// count user events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numUserEvents++: numUserEvents;});
|
||||
numUserEvents -= numFileEvents;
|
||||
@ -240,7 +247,7 @@ Future<void> main(List<String> arguments) async {
|
||||
}
|
||||
|
||||
stdout.write('Waiting for feed to come in..............');
|
||||
Future.delayed(const Duration(milliseconds: numWaitSeconds * 1), () {
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 1), () {
|
||||
|
||||
// count feed events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numFeedEvents++: numFeedEvents;});
|
||||
@ -253,7 +260,7 @@ Future<void> main(List<String> arguments) async {
|
||||
getMultiUserEvents(gListRelayUrls, pTags, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor));
|
||||
|
||||
stdout.write('Waiting for rest of posts to come in.....');
|
||||
Future.delayed(const Duration(milliseconds: numWaitSeconds * 2), () {
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 2), () {
|
||||
|
||||
// count other events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numOtherEvents++: numOtherEvents;});
|
||||
|
@ -9,19 +9,19 @@ import 'package:bip340/bip340.dart';
|
||||
Future<void> processNotifications(Tree node) async {
|
||||
// need a bit of wait to give other events to execute, so do a delay, which allows
|
||||
// relays to recieve and handle new events
|
||||
const int waitMilliSeconds = 400;
|
||||
const int waitMilliSeconds = 150;
|
||||
Future.delayed(const Duration(milliseconds: waitMilliSeconds), () {
|
||||
|
||||
Set<String> newEventIdsSet = node.insertEvents(getRecievedEvents());
|
||||
String nameToDisplay = userPrivateKey.length == 64?
|
||||
"$gCommentColor${getAuthorName(userPublicKey)}$colorEndMarker":
|
||||
"${gWarningColor}You are not signed in$colorEndMarker but are using public key $userPublicKey";
|
||||
"$gCommentColor${getAuthorName(userPublicKey)}$gColorEndMarker":
|
||||
"${gWarningColor}You are not signed in$gColorEndMarker but are using public key $userPublicKey";
|
||||
node.printNotifications(newEventIdsSet, nameToDisplay);
|
||||
clearEvents();
|
||||
});
|
||||
|
||||
Future<void> foo() async {
|
||||
await Future.delayed(Duration(milliseconds: waitMilliSeconds + 100));
|
||||
await Future.delayed(Duration(milliseconds: waitMilliSeconds + 50));
|
||||
return;
|
||||
}
|
||||
await foo();
|
||||
@ -34,7 +34,7 @@ Future<void> processNotifications(Tree node) async {
|
||||
Future<void> sendReplyPostLike(Tree node, String replyToId, String replyKind, String content) async {
|
||||
String strTags = node.getTagStr(replyToId, exename);
|
||||
if( replyToId.isNotEmpty && strTags == "") { // this returns empty only when the given replyto ID is non-empty, but its not found ( nor is it 64 bytes)
|
||||
print("${gWarningColor}The given target id was not found and/or is not a valid id. Not sending the event.$colorEndMarker");
|
||||
print("${gWarningColor}The given target id was not found and/or is not a valid id. Not sending the event.$gColorEndMarker");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -307,7 +307,7 @@ Future<void> otherMenuUi(Tree node, var contactList) async {
|
||||
if( words != "") {
|
||||
bool onlyWords (Tree t) => t.hasWords(words.toLowerCase());
|
||||
node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), onlyWords); // search for last gNumLastDays only
|
||||
}
|
||||
} else print("word = $words");
|
||||
break;
|
||||
|
||||
case 5:
|
||||
@ -405,8 +405,9 @@ Future<void> channelMenuUI(Tree node, var contactList) async {
|
||||
if( channelId == "x") {
|
||||
showChannelOption = false;
|
||||
}
|
||||
int pageNum = 1;
|
||||
while(showChannelOption) {
|
||||
String fullChannelId = node.showChannel(channelId);
|
||||
String fullChannelId = node.showChannel(channelId, pageNum);
|
||||
if( fullChannelId == "") {
|
||||
print("Could not find the given channel.");
|
||||
showChannelOption = false;
|
||||
@ -421,8 +422,13 @@ Future<void> channelMenuUI(Tree node, var contactList) async {
|
||||
if( messageToSend == 'x') {
|
||||
showChannelOption = false;
|
||||
} else {
|
||||
// send message to the given room
|
||||
await sendChatMessage(node, fullChannelId, messageToSend);
|
||||
if( messageToSend.isChannelPageNumber(gMaxChannelPagesDisplayed) ) {
|
||||
pageNum = (int.tryParse(messageToSend))??1;
|
||||
} else {
|
||||
// send message to the given room
|
||||
await sendChatMessage(node, fullChannelId, messageToSend);
|
||||
pageNum = 1; // reset it
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print("Refreshing...");
|
||||
|
@ -101,6 +101,16 @@ bool isWhitespace(String s) {
|
||||
|
||||
|
||||
extension StringX on String {
|
||||
isChannelPageNumber(int max) {
|
||||
|
||||
int? n = int.tryParse(this);
|
||||
if( n != null) {
|
||||
if( n < max)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
isEnglish( ) {
|
||||
// since smaller words can be smileys they should not be translated
|
||||
if( length < 6)
|
||||
@ -343,7 +353,7 @@ class EventData {
|
||||
|
||||
int n = 4;
|
||||
String maxN(String v) => v.length > n? v.substring(0,n) : v.substring(0, v.length);
|
||||
void printInColor(String s, String commentColor) => stdout.supportsAnsiEscapes ?stdout.write("$commentColor$s$colorEndMarker"):stdout.write(s);
|
||||
void printInColor(String s, String commentColor) => stdout.supportsAnsiEscapes ?stdout.write("$commentColor$s$gColorEndMarker"):stdout.write(s);
|
||||
|
||||
// TODO do it in one call
|
||||
final df1 = DateFormat('hh:mm a');
|
||||
@ -393,7 +403,7 @@ class EventData {
|
||||
String reactorId = reactors[i][0];
|
||||
if( newLikes.contains(reactorId)) {
|
||||
// colorify
|
||||
reactorNames += gNotificationColor + getAuthorName(reactorId) + colorEndMarker;
|
||||
reactorNames += gNotificationColor + getAuthorName(reactorId) + gColorEndMarker;
|
||||
} else {
|
||||
reactorNames += getAuthorName(reactorId);
|
||||
}
|
||||
|
@ -171,7 +171,7 @@ class Relays {
|
||||
return;
|
||||
}
|
||||
},
|
||||
onError: (err) { print("\n${gWarningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay=<relay wss url>"); 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=<relay wss url>"); print(gColorEndMarker); },
|
||||
onDone: () { if( gDebug != 0) print('Info: In onDone'); }
|
||||
);
|
||||
} on WebSocketException {
|
||||
|
@ -5,7 +5,7 @@ final log = Logger('ExampleLogger');
|
||||
// for debugging
|
||||
String gCheckEventId = "a4479de655094679cdfb10f347521aa58f24717cdc5ddba89fb346453a8a99ed";
|
||||
|
||||
const int numWaitSeconds = 3000; // is used in main()
|
||||
const int gDefaultNumWaitSeconds = 3000; // is used in main()
|
||||
|
||||
const String gDefaultEventsFilename = "all_nostr_events.txt";
|
||||
String gEventsFilename = ""; // is set in arguments, and if set, then file is read from and written to
|
||||
@ -84,7 +84,7 @@ Map<String, String> gColorMap = { "green": greenColor,
|
||||
String gCommentColor = greenColor;
|
||||
String gNotificationColor = cyanColor; // cyan
|
||||
String gWarningColor = redColor; // red
|
||||
const String colorEndMarker = "\x1B[0m";
|
||||
const String gColorEndMarker = "\x1B[0m";
|
||||
|
||||
// dummy account pubkey
|
||||
const String gDummyAccountPubkey = "Non";
|
||||
@ -103,9 +103,14 @@ List<String> gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2
|
||||
"6a9eb714c2889aa32e449cfbb7854bc9780feed4ff3d887e03910dcb22aa560a" // "bible bot"
|
||||
];
|
||||
|
||||
// difficulty related settings
|
||||
const int gMaxDifficultyAllowed = 24;
|
||||
int gDifficulty = 0;
|
||||
|
||||
// channel related settings
|
||||
const int gNumChannelMessagesToShow = 15;
|
||||
const int gMaxChannelPagesDisplayed = 50;
|
||||
|
||||
const String gUsage = """$exename version $version
|
||||
The nostr console client built using dart.
|
||||
|
||||
|
@ -458,7 +458,12 @@ class Tree {
|
||||
});
|
||||
}
|
||||
|
||||
void printChannel(ChatRoom room) {
|
||||
void printChannel(ChatRoom room, [int page = 1]) {
|
||||
if( page < 1) {
|
||||
if( gDebug > 0) log.info("In printChannel got page = $page");
|
||||
page = 1;
|
||||
}
|
||||
|
||||
String displayName = room.chatRoomId;
|
||||
if( room.name != "") {
|
||||
displayName = "${room.name} ( ${displayName.substring(0, 6)} )";
|
||||
@ -469,24 +474,47 @@ class Tree {
|
||||
print(" ${getNumSpaces(gNumLeftMarginSpaces + displayName.length~/2 + 4)}In Channel");
|
||||
print("\n$str\n");
|
||||
|
||||
for(int i = 0; i < room.messageIds.length; i++) {
|
||||
String eId = room.messageIds[i];
|
||||
Event? e = allChildEventsMap[eId]?.e;
|
||||
if( e!= null) {
|
||||
e.printEvent(0);
|
||||
print("");
|
||||
}
|
||||
|
||||
int i = 0, startFrom = 0, endAt = room.messageIds.length;
|
||||
int numPages = 1;
|
||||
|
||||
if( room.messageIds.length > gNumChannelMessagesToShow ) {
|
||||
startFrom = room.messageIds.length - ( room.messageIds.length ~/ gNumChannelMessagesToShow) * gNumChannelMessagesToShow - (gNumChannelMessagesToShow * (page -1));
|
||||
endAt = startFrom + gNumChannelMessagesToShow;
|
||||
if( startFrom < 0) startFrom = 0;
|
||||
numPages = (room.messageIds.length ~/ gNumChannelMessagesToShow) + 1;
|
||||
if( page > numPages) {
|
||||
page = numPages;
|
||||
}
|
||||
}
|
||||
if( gDebug > 0) print("StartFrom $startFrom endAt $endAt numPages $numPages room.messageIds.length = ${room.messageIds.length}");
|
||||
for( i = startFrom; i < endAt; i++) {
|
||||
String eId = room.messageIds[i];
|
||||
Event? e = allChildEventsMap[eId]?.e;
|
||||
if( e!= null) {
|
||||
e.printEvent(0);
|
||||
print("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( startFrom != 0) {
|
||||
print("\n");
|
||||
printDepth(0);
|
||||
stdout.write("${gNotificationColor}Displayed page number ${page+1} (out of total $numPages pages, where 1st is the latest 'page').\n");
|
||||
printDepth(0);
|
||||
stdout.write("To see older pages, enter numbers from 1-${numPages}.${gColorEndMarker}\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
// shows the given channelId, where channelId is prefix-id or channel name as mentioned in room.name. returns full id of channel.
|
||||
String showChannel(String channelId) {
|
||||
String showChannel(String channelId, [int page = 1]) {
|
||||
|
||||
for( String key in chatRooms.keys) {
|
||||
if( key.substring(0, channelId.length) == channelId ) {
|
||||
ChatRoom? room = chatRooms[key];
|
||||
if( room != null) {
|
||||
printChannel(room);
|
||||
printChannel(room, page);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ description: A nostr client built for terminal/console.
|
||||
version: 0.0.7
|
||||
homepage: https://github.com/vishalxl/nostr_console
|
||||
|
||||
# testing pubspec action based on path
|
||||
# testing pubspec action based on path, test build
|
||||
|
||||
environment:
|
||||
sdk: '>=2.17.3 <3.0.0'
|
||||
|
Loading…
x
Reference in New Issue
Block a user