more code moved to utils.dart

This commit is contained in:
Vishal 2022-11-28 15:42:25 +05:30
parent 059d72881e
commit fe69a6503a
8 changed files with 140 additions and 145 deletions

View File

@ -26,14 +26,6 @@ const String translateArg = "translate";
const String colorArg = "color";
const String overWriteFlag = "overwrite";
void printUsage() {
print(gUsage);
}
void printVersion() {
print("$version");
}
Future<void> main(List<String> arguments) async {
Logger.root.level = Level.ALL; // defaults to Level.INFO
DateTime appStartTime = DateTime.now();

View File

@ -6,7 +6,6 @@ import 'package:nostr_console/relays.dart';
import 'package:nostr_console/settings.dart';
import 'package:nostr_console/utils.dart';
import 'package:bip340/bip340.dart';
import 'package:qr/qr.dart';
Future<void> processAnyIncomingEvents(Store node, [bool printNotifications = true]) async {
reAdjustAlignment();
@ -277,54 +276,6 @@ void reAdjustAlignment() {
Store.reCalculateMarkerStr();
}
String getQrCodeAsString(String str) {
String output = "";
final qrCode = QrCode(4, QrErrorCorrectLevel.L)
..addData('$str');
final qrImage = QrImage(qrCode);
assert( qrImage.moduleCount == 33);
//print("qrimage modulecount = ${qrImage.moduleCount}");
String leftPadding = " ";
var x = 0;
for (x = 0; x < qrImage.moduleCount -1 ; x += 2) {
output += leftPadding;
for (var y = 0; y < qrImage.moduleCount ; y++) {
bool topDark = qrImage.isDark(y, x);
bool bottomDark = qrImage.isDark(y, x + 1);
if (topDark && bottomDark) {
output += "";
}
else if (topDark ) {
output += "";
} else if ( bottomDark) {
output += "";
} else if( !topDark && !bottomDark) {
output += " ";
}
}
output += "\n";
}
if( qrImage.moduleCount %2 == 1) {
output += leftPadding;
for (var y = 0; y < qrImage.moduleCount ; y++) {
bool dark = qrImage.isDark(y, x);
if (dark ) {
output += "";
} else {
output += " ";
}
}
output += "\n";
}
return output;
}
void printProfile(Store node, String profilePubkey) {
bool onlyUserPostAndLike (Tree t) => t.treeSelectorUserPostAndLike(profilePubkey);
node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), onlyUserPostAndLike);
@ -457,9 +408,6 @@ void printVerifiedAccounts(Store node) {
print("\nHow to use: To get best results, print the main feed a couple of times right after starting; and then check NIP verified list. This gives application time to do the verification from user's given servers.\n\n");
}
void clearScreen() {
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
int showMenu(List<String> menuOptions, String menuName, [String menuInfo = ""]) {
@ -644,17 +592,6 @@ Future<void> otherOptionsMenuUi(Store node) async {
return;
}
// returns a string entered by the user
String getStringFromUser(String prompt, [String defaultValue=""] ) {
String str = "";
stdout.write(prompt);
str = (stdin.readLineSync())??"";
if( str.length == 0)
str = defaultValue;
return str;
}
// sends event creating a new public channel
Future<void> createPublicChannel(Store node) async {
@ -1034,8 +971,6 @@ Future<void> encryptedChannelMenuUI(Store node) async {
break;
}
stdout.write("\nType message; or type 'x' to exit, or press <enter> to refresh: ");
$tempUserInput = stdin.readLineSync(encoding: utf8);
String messageToSend = $tempUserInput??"";
@ -1559,4 +1494,3 @@ Future<void> mainMenuUi(Store node) async {
} // end while
} // end mainMenuUi()

View File

@ -231,66 +231,7 @@ class Relays {
Relays relays = Relays({}, {}, {});
String getKindRequest(String subscriptionId, List<int> kind, int limit, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since":${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{"kinds":[';
var strSubscription2 ='], "limit":$limit$strTime } ]';
String strKind = "";
for(int i = 0; i < kind.length; i++) {
String comma = ",";
if( i == kind.length-1) {
comma = "";
}
strKind = strKind + kind[i].toString() + comma;
}
String strRequest = strSubscription1 + strKind + strSubscription2;
//print(strRequest);
return strRequest;
}
String getUserRequest(String subscriptionId, String publicKey, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "authors": ["';
var strSubscription2 ='"], "limit": $numUserEvents $strTime } ]';
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
}
String getMentionRequest(String subscriptionId, String publicKey, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "#p": ["';
var strSubscription2 ='"], "limit": $numUserEvents $strTime } ]';
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
}
String getMultiUserRequest(String subscriptionId, List<String> publicKeys, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "authors": [';
var strSubscription2 ='], "limit": $numUserEvents $strTime } ]';
String s = "";
for(int i = 0; i < publicKeys.length; i++) {
s += "\"${publicKeys[i].toLowerCase()}\"";
if( i < publicKeys.length - 1) {
s += ",";
}
}
return strSubscription1 + s + strSubscription2;
}
void getContactFeed(List<String> relayUrls, Set<String> setContacts, int numEventsToGet, int sinceWhen) {

View File

@ -327,6 +327,7 @@ Source Code and Binaries: https://github.com/vishalxl/nostr_console
''';
/////////////////////////////////////////////////////////print intro
void printIntro(String msg) {
String intro =
@ -358,3 +359,13 @@ if( stdout.hasTerminal )
lines.forEach((line) {print(line.length > terminalColumns ? line.substring(0, terminalColumns) : line );});
}
/////////////////////////////////////////////////////////other settings related functions
void printUsage() {
print(gUsage);
}
void printVersion() {
print("$version");
}

View File

@ -68,7 +68,6 @@ bool userInvolved(String pubkey, Event e) {
}
}
}
return false;
}
@ -78,7 +77,6 @@ bool selectorTrees_all(Tree t) {
// only show in which user is involved
bool selectorTrees_userRepliesLikes(Tree t) {
if( userInvolved(userPublicKey, t.event)) {
return true;
}
@ -121,7 +119,6 @@ bool followsInvolved(Event e, Event? contactEvent) {
return false;
}
// only show in which user is involved
bool selectorTrees_followsPosts(Tree t) {
Event? contactEvent = gKindONames[userPublicKey]?.latestContactEvent;
@ -139,7 +136,6 @@ bool selectorTrees_followsPosts(Tree t) {
return false;
}
bool selectorShowAllRooms(ScrollableMessages room) {
return true;
}

View File

@ -1,4 +1,5 @@
import 'dart:io';
import 'package:qr/qr.dart';
class HistogramEntry {
String str;
@ -15,7 +16,6 @@ class HistogramEntry {
}
}
bool nonEnglish(String str) {
bool result = false;
return result;
@ -112,7 +112,6 @@ extension StringX on String {
}
}
bool isValidPubkey(String pubkey) {
if( pubkey.length == 64) {
return true;
@ -157,7 +156,6 @@ String getStrTagsFromJson(dynamic json) {
return str;
}
String addEscapeChars(String str) {
String temp = "";
//temp = temp.replaceAll("\\", "\\\\");
@ -192,7 +190,6 @@ String getNumDashes(int num, [String dashType = "-"]) {
return s;
}
List<List<int>> getUrlRanges(String s) {
List<List<int>> urlRanges = [];
String regexp1 = "http[s]*:\/\/[a-zA-Z0-9]+([.a-zA-Z0-9/_\\-\\#\\+=\\&\\?]*)";
@ -206,7 +203,6 @@ List<List<int>> getUrlRanges(String s) {
return urlRanges;
}
// returns true if n is in any of the ranges given in list
int isInRange( int n, List<List<int>> ranges ) {
for( int i = 0; i < ranges.length; i++) {
@ -216,3 +212,128 @@ int isInRange( int n, List<List<int>> ranges ) {
}
return 0;
}
String getQrCodeAsString(String str) {
String output = "";
final qrCode = QrCode(4, QrErrorCorrectLevel.L)
..addData('$str');
final qrImage = QrImage(qrCode);
assert( qrImage.moduleCount == 33);
//print("qrimage modulecount = ${qrImage.moduleCount}");
String leftPadding = " ";
var x = 0;
for (x = 0; x < qrImage.moduleCount -1 ; x += 2) {
output += leftPadding;
for (var y = 0; y < qrImage.moduleCount ; y++) {
bool topDark = qrImage.isDark(y, x);
bool bottomDark = qrImage.isDark(y, x + 1);
if (topDark && bottomDark) {
output += "";
}
else if (topDark ) {
output += "";
} else if ( bottomDark) {
output += "";
} else if( !topDark && !bottomDark) {
output += " ";
}
}
output += "\n";
}
if( qrImage.moduleCount %2 == 1) {
output += leftPadding;
for (var y = 0; y < qrImage.moduleCount ; y++) {
bool dark = qrImage.isDark(y, x);
if (dark ) {
output += "";
} else {
output += " ";
}
}
output += "\n";
}
return output;
}
void clearScreen() {
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
// returns a string entered by the user
String getStringFromUser(String prompt, [String defaultValue=""] ) {
String str = "";
stdout.write(prompt);
str = (stdin.readLineSync())??"";
if( str.length == 0)
str = defaultValue;
return str;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// relay related functions
String getKindRequest(String subscriptionId, List<int> kind, int limit, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since":${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{"kinds":[';
var strSubscription2 ='], "limit":$limit$strTime } ]';
String strKind = "";
for(int i = 0; i < kind.length; i++) {
String comma = ",";
if( i == kind.length-1) {
comma = "";
}
strKind = strKind + kind[i].toString() + comma;
}
String strRequest = strSubscription1 + strKind + strSubscription2;
//print(strRequest);
return strRequest;
}
String getUserRequest(String subscriptionId, String publicKey, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "authors": ["';
var strSubscription2 ='"], "limit": $numUserEvents $strTime } ]';
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
}
String getMentionRequest(String subscriptionId, String publicKey, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "#p": ["';
var strSubscription2 ='"], "limit": $numUserEvents $strTime } ]';
return strSubscription1 + publicKey.toLowerCase() + strSubscription2;
}
String getMultiUserRequest(String subscriptionId, List<String> publicKeys, int numUserEvents, int sinceWhen) {
String strTime = "";
if( sinceWhen != 0) {
strTime = ', "since": ${sinceWhen.toString()}';
}
var strSubscription1 = '["REQ","$subscriptionId",{ "authors": [';
var strSubscription2 ='], "limit": $numUserEvents $strTime } ]';
String s = "";
for(int i = 0; i < publicKeys.length; i++) {
s += "\"${publicKeys[i].toLowerCase()}\"";
if( i < publicKeys.length - 1) {
s += ",";
}
}
return strSubscription1 + s + strSubscription2;
}

View File

@ -7,7 +7,7 @@ homepage: https://github.com/vishalxl/nostr_console
# added utils.dart
# reply shortended
# chmod in actions
environment:
sdk: '>=2.17.3 <3.0.0'

View File

@ -1,6 +1,6 @@
import 'package:nostr_console/console_ui.dart';
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/settings.dart';
import 'package:nostr_console/utils.dart';
import 'package:test/test.dart';
import 'package:nostr_console/tree_ds.dart';