improved paragraph creation logic

This commit is contained in:
Vishal 2022-09-12 01:36:14 +05:30
parent 985ffc89f2
commit 45fe733a42
5 changed files with 164 additions and 92 deletions

View File

@ -192,17 +192,19 @@ void reAdjustAlignment() {
}
}
int depth = 0;
Store.reCalculateMarkerStr();
/* int depth = 0;
Store.startMarkerStr = getDepthSpaces(depth);
Store.startMarkerStr += ("▄────────────\n"); // bottom half
int endMarkerDepth = depth + 1 + gTextWidth~/ gSpacesPerDepth - 1;
int endMarkerDepth = depth + 2 + gTextWidth~/ gSpacesPerDepth - 1;
Store.endMarkerStr = getDepthSpaces(endMarkerDepth);
Store.endMarkerStr += "\n";
Store.endMarkerStr += "────────────▀".padLeft((endMarkerDepth) * gSpacesPerDepth + gNumLeftMarginSpaces + 1) ;
Store.endMarkerStr += "\n";
*/
}

View File

@ -340,39 +340,19 @@ class EventData {
const int typicalxLen = "|id: 82b5 , 12:04 AM Sep 19".length + 5; // not sure where 5 comes from
String idDateLikes = " |id: ${maxN(id)} , $strDate ${getReactionStr(depth)}" ;
//print("$typicalxLen ${idDateLikes.length}");
idDateLikes = idDateLikes.padRight(typicalxLen);
String temp = tempEvaluatedContent==""?tempContent: tempEvaluatedContent;
// comment extends from gNumLeftMarginSpaces + extraLen TO +gTextWidth
// if left_stuff + extraLen + comment len + idStrLike < gTextWidth
// then pad idStrLike with gTextWidth - ( left_stuff + extraLen + comment len )
if( (gSpacesPerDepth * depth + effectiveNameFieldLen + temp.length + idDateLikes.length ) > gTextWidth) {
// number of lines taken by comment = (comment.length + (extraLen))/ ( gTextWidth) + 1
/*int printedTextWidth = ( gTextWidth - ( gSpacesPerDepth * depth + extraLen));
int totalCommentWidth = temp.length + idDateLikes.length + 5;
int nCommentLines = (totalCommentWidth )~/ printedTextWidth + 1;
print(nCommentLines);
int lastLineLen = totalCommentWidth - printedTextWidth * (nCommentLines - 1);
int padLeftBy = (gTextWidth - (gSpacesPerDepth * depth + extraLen)) - ( lastLineLen ) ;
print("comment len = ${temp.length} iDateLikes len = ${idDateLikes.length} dividor = ${ printedTextWidth} padLeftBy = $padLeftBy");
idDateLikes = idDateLikes.padLeft( padLeftBy);*/
temp = temp + "$idDateLikes";
}
else {
idDateLikes = idDateLikes.padLeft((gTextWidth ) - (gSpacesPerDepth * depth + effectiveNameFieldLen + temp.length));
temp = temp + "$idDateLikes";
}
//temp = temp + " |$idDateLikes";
String contentShifted = makeParagraphAtDepth( temp, gSpacesPerDepth * depth + effectiveNameFieldLen);
strToPrint += getStrInColor(contentShifted + "\n", commentColor);
@ -826,10 +806,13 @@ String makeParagraphAtDepth3(String s, int depthInSpaces) {
// make a paragraph of s that starts at numSpaces ( from screen left), and does not extend beyond gTextWidth+gNumLeftMarginSpaces. break it, or add
// a newline if it goes beyond gTextWidth + gNumLeftMarginSpaces
String makeParagraphAtDepth(String s, int depthInSpaces) {
String newString = "";
String spacesString = getNumSpaces(depthInSpaces + gNumLeftMarginSpaces);
int lenPerLine = gTextWidth - depthInSpaces;
//print("In makeParagraphAtDepth: gNumLeftMarginSpaces = $gNumLeftMarginSpaces depthInSPaces = $depthInSpaces LenPerLine = $lenPerLine gTextWidth = $gTextWidth ");
for(int startIndex = 0; startIndex < s.length; ) {
List listCulledLine = getLineWithMaxLen(s, startIndex, lenPerLine, spacesString);
@ -838,15 +821,18 @@ String makeParagraphAtDepth(String s, int depthInSpaces) {
//print("returned len = $lenReturned");
if( line.length == 0 || lenReturned == 0) break;
newString += line;
startIndex += lenReturned;
}
//print("Returning newString with len = ${newString.length}");
return newString;
}
// returns from string[startIndex:] the first len number of chars. no newline is added.
List getLineWithMaxLen(String s, int startIndex, int len, String spacesString) {
List getLineWithMaxLen(String s, int startIndex, int lenPerLine, String spacesString) {
//print("====================in getLineWithMaxlen. strlen = ${s.length} startIndex = $startIndex len = $len ");
if( startIndex >= s.length)
@ -854,6 +840,7 @@ List getLineWithMaxLen(String s, int startIndex, int len, String spacesString) {
String line = "";
int firstLineLen = -1;
// if length required is greater than the length of string remaing, return whatever remains
//if( len > (s.length - startIndex))
// return [s.substring(startIndex), s.length - startIndex];
@ -861,7 +848,7 @@ List getLineWithMaxLen(String s, int startIndex, int len, String spacesString) {
int numCharsInLine = 0;
int i = startIndex;
for(; i < startIndex + len && i < s.length; i++) {
for(; i < startIndex + lenPerLine && i < s.length; i++) {
line += s[i];
numCharsInLine ++;
@ -874,14 +861,53 @@ List getLineWithMaxLen(String s, int startIndex, int len, String spacesString) {
}
}
if( numCharsInLine > len || (numCharsInLine == len && s.length > startIndex + numCharsInLine)) {
//print(" line longer than $len at $numCharsInLine. also inserting spacesString");
line += "\n";
line += spacesString;
//print(" ${numCharsInLine > lenPerLine} || ${( (numCharsInLine == lenPerLine) && (s.length > startIndex + numCharsInLine))}");
if( numCharsInLine > lenPerLine || ( (numCharsInLine == lenPerLine) && (s.length > startIndex + numCharsInLine) )) {
bool lineBroken = false;
//print(" line longer than $lenPerLine at $numCharsInLine.");
//print(" first check if it needs to be broken.");
//print("line.length = ${line.length} lenPerLine = $lenPerLine");
// break line at end if its cutting words; like is broken only if the returned line is the longest it can be, and
// if its length is greater than the gMaxLenBrokenWord constant
if( line.length >= lenPerLine && line.length > gMaxLenUnbrokenWord ) {
//print(" line ending seems to be cutting words");
int i = line.length - 1;
// find a whitespace character
for( ; i > 0 && !isWordSeparater(line[i]); i--);
// for ended
//print(" for ended with i = $i");
if( line.length - i < gMaxLenUnbrokenWord) {
// break the line here if its not a word separator
if( isWordSeparater(line[i])) {
//print(" line[i] = ${line[i]} is not a word separator");
firstLineLen = i;
line = line.substring(0, i) + "\n" + spacesString + line.substring(i + 1, line.length);
lineBroken = true;
//print(" line does get broken");
}
}
}
if( !lineBroken ) {
if( s.length > i ) {
line += "\n";
line += spacesString;
}
}
}
//print(" returning with inserted: ${i - startIndex}");
//print(" returning: |$line|");
//print(" Returning line with len = ${line.length} first line len = ${firstLineLen}");
return [line, i - startIndex];
}

View File

@ -102,6 +102,8 @@ const int gDefaultMaxDepth = 6;
int maxDepthAllowed = gDefaultMaxDepth;
const int leftShiftThreadsBy = 4;
int gMaxLenUnbrokenWord = 8; // lines are broken if space is at end of line for this number of places
// https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#8-colors
// Color related settings
const String defaultTextColor = "green";

View File

@ -504,27 +504,17 @@ class Store {
if( tree.store == null) {
tree.setStore(this);
}
int depth = 0;
startMarkerStr = getDepthSpaces(depth);
startMarkerStr += ("▄────────────\n"); // bottom half
int endMarkerDepth = depth + 1 + gTextWidth~/ gSpacesPerDepth - 1;
Store.endMarkerStr = getDepthSpaces(endMarkerDepth);
Store.endMarkerStr += "\n";
Store.endMarkerStr += "────────────▀".padLeft((endMarkerDepth) * gSpacesPerDepth + gNumLeftMarginSpaces + 1) ;
Store.endMarkerStr += "\n";
});
reCalculateMarkerStr();
}
static void reCalculateMarkerStr() {
int depth = 0;
Store.startMarkerStr = getDepthSpaces(depth);
Store.startMarkerStr += ("▄────────────\n"); // bottom half
int endMarkerDepth = depth + 1 + gTextWidth~/ gSpacesPerDepth - 1;
int endMarkerDepth = depth + 2 + gTextWidth~/ gSpacesPerDepth - 1;
Store.endMarkerStr = getDepthSpaces(endMarkerDepth);
Store.endMarkerStr += "\n";
Store.endMarkerStr += "────────────▀".padLeft((endMarkerDepth) * gSpacesPerDepth + gNumLeftMarginSpaces + 1) ;

View File

@ -1,3 +1,4 @@
import 'dart:io';
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/settings.dart';
import 'package:test/test.dart';
@ -13,7 +14,11 @@ Event exampleEventChild = Event('event', 'id4', exampleEdataChild, ['relay name'
Store exampleStore = Store([], {}, [], [], []);
Tree exampleTree = Tree.withoutStore(exampleEvent, []);
//bool skipTest = true;
void main() {
/*
test('PrintEmptyEvent', () {
expect(EventData("non","",1,1,"", [], [], [], [[]], {}).toString(), "");
});
@ -31,11 +36,13 @@ void main() {
store.printTree(0, DateTime.now().subtract(Duration(days:1)), selectorShowAllTrees);
});
*/
test('createNodeTree_ordered', () {
Event exampleEvent1 = Event.fromJson('["EVENT","latest",{"id":"167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722388,"kind":1,"tags":[],"content":"nostr is not federated is it? this is like a global feed of all nostr freaks?","sig":"6db0b287015d9529dfbacef91561cb4e32afd6968edd8454867b8482bde01452e17b6f3de69bffcb2d9deba2a52d3c9ff82e04f7b18eb32428daf7eab5fd27c5"}]', "");
Event exampleEvent2 = Event.fromJson('["EVENT","latest",{"id":"f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722463,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"]],"content":"I dont get the technical stuff about relays and things","sig":"9f68031687214a24862226f291e3baadd956dc14ba9c5c552f8c881a40aacd34feda667ef4e4b09711cd43950eec2d272d5b11bd7636de5f457f38f31eaff398"}]', "");
Event exampleEvent3 = Event.fromJson('["EVENT","latest",{"id":"dfc5765da281c0ad99cb8693fc98c87f0f86ad56042a414f06f19d41c1315fc3","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722537,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"],["e","f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67"]],"content":"different clients make sense to me. I can use different clients to access nostr but is just one giant soup like twitter","sig":"d4fdc288e3cb95fc5ab46177fc0982d2aaa3b028eef6649f8200500da9c2e9a16c7a0462638afef7635bfea3094ec10901de759a48e362b60cb08f7e6585e02f"}]', "");
Event exampleEvent1 = Event.fromJson('["EVENT","latest",{"id":"167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de","pubkey":"137d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722388,"kind":1,"tags":[],"content":"nostr is not federated is it? this is like a global feed of all nostr freaks?","sig":"6db0b287015d9529dfbacef91561cb4e32afd6968edd8454867b8482bde01452e17b6f3de69bffcb2d9deba2a52d3c9ff82e04f7b18eb32428daf7eab5fd27c5"}]', "");
Event exampleEvent2 = Event.fromJson('["EVENT","latest",{"id":"f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67","pubkey":"137d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722463,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"]],"content":"I dont get the technical stuff about relays and things","sig":"9f68031687214a24862226f291e3baadd956dc14ba9c5c552f8c881a40aacd34feda667ef4e4b09711cd43950eec2d272d5b11bd7636de5f457f38f31eaff398"}]', "");
Event exampleEvent3 = Event.fromJson('["EVENT","latest",{"id":"dfc5765da281c0ad99cb8693fc98c87f0f86ad56042a414f06f19d41c1315fc3","pubkey":"137d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722537,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"],["e","f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67"]],"content":"different clients make sense to me. I can use different clients to access nostr but is just one giant soup like twitter","sig":"d4fdc288e3cb95fc5ab46177fc0982d2aaa3b028eef6649f8200500da9c2e9a16c7a0462638afef7635bfea3094ec10901de759a48e362b60cb08f7e6585e02f"}]', "");
Set<Event> listEvents = {exampleEvent1, exampleEvent2, exampleEvent3};
@ -46,9 +53,9 @@ void main() {
test('createNodeTree_unordered1', () {
Event exampleEvent1 = Event.fromJson('["EVENT","latest",{"id":"167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722388,"kind":1,"tags":[],"content":"nostr is not federated is it? this is like a global feed of all nostr freaks?","sig":"6db0b287015d9529dfbacef91561cb4e32afd6968edd8454867b8482bde01452e17b6f3de69bffcb2d9deba2a52d3c9ff82e04f7b18eb32428daf7eab5fd27c5"}]', "");
Event exampleEvent2 = Event.fromJson('["EVENT","latest",{"id":"f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722463,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"]],"content":"I dont get the technical stuff about relays and things","sig":"9f68031687214a24862226f291e3baadd956dc14ba9c5c552f8c881a40aacd34feda667ef4e4b09711cd43950eec2d272d5b11bd7636de5f457f38f31eaff398"}]', "");
Event exampleEvent3 = Event.fromJson('["EVENT","latest",{"id":"dfc5765da281c0ad99cb8693fc98c87f0f86ad56042a414f06f19d41c1315fc3","pubkey":"e37d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722537,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"],["e","f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67"]],"content":"different clients make sense to me. I can use different clients to access nostr but is just one giant soup like twitter","sig":"d4fdc288e3cb95fc5ab46177fc0982d2aaa3b028eef6649f8200500da9c2e9a16c7a0462638afef7635bfea3094ec10901de759a48e362b60cb08f7e6585e02f"}]', "");
Event exampleEvent1 = Event.fromJson('["EVENT","latest",{"id":"167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de","pubkey":"137d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722388,"kind":1,"tags":[],"content":"nostr is not federated is it? this is like a global feed of all nostr freaks?","sig":"6db0b287015d9529dfbacef91561cb4e32afd6968edd8454867b8482bde01452e17b6f3de69bffcb2d9deba2a52d3c9ff82e04f7b18eb32428daf7eab5fd27c5"}]', "");
Event exampleEvent2 = Event.fromJson('["EVENT","latest",{"id":"f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67","pubkey":"137d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722463,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"]],"content":"I dont get the technical stuff about relays and things","sig":"9f68031687214a24862226f291e3baadd956dc14ba9c5c552f8c881a40aacd34feda667ef4e4b09711cd43950eec2d272d5b11bd7636de5f457f38f31eaff398"}]', "");
Event exampleEvent3 = Event.fromJson('["EVENT","latest",{"id":"dfc5765da281c0ad99cb8693fc98c87f0f86ad56042a414f06f19d41c1315fc3","pubkey":"137d948a0eee45e6cd113faaad934fcf17a97de2236c655b70650d4252daa9d3","created_at":1659722537,"kind":1,"tags":[["e","167063f491c41b7b8f79bc74f318e8a8b0a802bf8364b8bb7d19c887d59ec5de"],["e","f3a267ecbb631012da618de620bc1fe265f6429f412359bf02330b437cf88e67"]],"content":"different clients make sense to me. I can use different clients to access nostr but is just one giant soup like twitter","sig":"d4fdc288e3cb95fc5ab46177fc0982d2aaa3b028eef6649f8200500da9c2e9a16c7a0462638afef7635bfea3094ec10901de759a48e362b60cb08f7e6585e02f"}]', "");
Set<Event> listEvents = { exampleEvent3, exampleEvent2, exampleEvent1};
@ -56,7 +63,7 @@ void main() {
node.printTree(0, DateTime.now().subtract(Duration(days:1000)), selectorShowAllTrees); // will test for ~1000 days
});
test('make paragraph', () {
test('make_paragraph', () {
gTextWidth = 120;
//print(gNumLeftMarginSpaces);
//print(gTextWidth);
@ -91,56 +98,101 @@ void main() {
a
""";
a""";
String expectedResult =
"""
1 Testing paragraph with multiple lines. Testing paragraph with multiple lines. Testing pa
ragraph with multiple lines. Testing paragraph with multiple lines.
2 Testing paragraph with multiple lines. Testing paragraph with multiple lines. Testing pa
ragraph with multiple lines.
3 Testing paragraph with multiple lines.
5 Testing paragraph with multiple lines. Testing paragraph with multiple lines. Testing pa
ragraph with multiple lines.
6 Testing paragraph with multiple lines.
1 Testing paragraph with multiple lines. Testing paragraph with multiple lines. Testing
paragraph with multiple lines. Testing paragraph with multiple lines.
2 Testing paragraph with multiple lines. Testing paragraph with multiple lines. Testing
paragraph with multiple lines.
3 Testing paragraph with multiple lines.
5 Testing paragraph with multiple lines. Testing paragraph with multiple lines. Testing
paragraph with multiple lines.
6 Testing paragraph with multiple lines.
7 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 89 words
8 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 90 words
9 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 91 word
s
10 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 92 wor
ds
8 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 90
words
9 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 91
words
10 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 92
words
11 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 89 words
12 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 90 words
13 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 91 word
s
14 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 92 wor
ds
12 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 90
words
13 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 91
words
14 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 92
words
a""";
String res = makeParagraphAtDepth(paragraph, 30);
print(res);
/* stdout.write(getNumSpaces(30));
stdout.write("\n");
stdout.write(getNumDashes(30));
//stdout.write(getNumSpaces(30)); */
stdout.write(res);
return paragraph == expectedResult;;
//return res == expectedResult;;
expect( res, expectedResult);
});
}
test('break_line ', () {
gTextWidth = 120;
String paragraph = """
1 Testing paragraph with breaks in lines. Testing paragraph with multiple lines. Testing paragraph with multiple lines. Testing paragraph with multiple lines.
8 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 90 words
9 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 91 words
10 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 92 words""";
String expectedResult =
"""1 Testing paragraph with breaks in lines. Testing paragraph with multiple lines. Testing
paragraph with multiple lines. Testing paragraph with multiple lines.
8 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 90
words
9 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 91
words
10 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 92
words""";
String res = makeParagraphAtDepth(paragraph, 30);
/*stdout.write(getNumSpaces(30));
stdout.write("\n");
stdout.write(getNumDashes(30));
stdout.write(getNumSpaces(30));
stdout.write(res); */
/*
print("paragraph length = ${paragraph.length} res length = ${res.length}");
print("paragraph last: ${paragraph.substring(paragraph.length - 4, paragraph.length)}");
print( res.substring(res.length - 100, res.length).codeUnits);
//res = res.trim();
print("res trimmed len = ${res.length}");
*/
expect( res, expectedResult);
});
return ;
} // end main