improved paragraph shifting function

and added test.
This commit is contained in:
Vishal
2022-09-11 17:17:25 +05:30
parent f24e5adec3
commit fd65725c0a
4 changed files with 163 additions and 48 deletions

View File

@@ -378,8 +378,6 @@ class EventData {
String getAsLine({int len = 20}) {
String contentToPrint = evaluatedContent.isEmpty? content: evaluatedContent;
//print("$contentToPrint|");
//print("len = ${contentToPrint.length}");
if( len == 0 || len > contentToPrint.length) {
len = contentToPrint.length;
}
@@ -391,7 +389,6 @@ class EventData {
int strWidth = 40;
String paddedStrToPrint = strToPrint.padLeft(strWidth);
//print("\n$paddedStrToPrint");
paddedStrToPrint = paddedStrToPrint.substring(0, strWidth);
if( isNotification) {
@@ -809,51 +806,80 @@ String getNumDashes(int num, [String dashType = "-"]) {
return s;
}
String makeParagraphAtDepth3(String s, int depthInSpaces) {
String newString = "";
String spacesString = getNumSpaces(depthInSpaces + gNumLeftMarginSpaces);
int lenPerLine = gTextWidth - depthInSpaces;
List<String> lines = s.split("\n");
lines.forEach((line) {
newString += line.replaceAll("\n", "\n" + spacesString );
});
return newString;
}
// 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 numSpaces) {
String makeParagraphAtDepth(String s, int depthInSpaces) {
String newString = "";
int numCharsInCurLine = 0;
String spacesString = getNumSpaces(numSpaces + gNumLeftMarginSpaces);
String spacesString = getNumSpaces(depthInSpaces + gNumLeftMarginSpaces);
for(int i = 0; i < s.length; i++) {
if( s[i] == '\n') {
newString += "\n";
newString += spacesString;
numCharsInCurLine = 0;
} else {
if( numCharsInCurLine >= (gTextWidth - numSpaces)) {
if( i > 1 && !isWordSeparater(s[i])) {
// go back in output string and readjust it if needed
const int lookForSpace = 6;
bool foundSpace = false;
for(int j = 0; j < min(newString.length, lookForSpace); j++) {
if( newString[newString.length-1-j] == " ") {
foundSpace = true;
String charsInNextLine = "";
charsInNextLine = newString.substring(newString.length-j, newString.length);
String temp = newString.substring(0, newString.length-j) + "\n" + spacesString + charsInNextLine;
newString = temp;
numCharsInCurLine = charsInNextLine.length;
int lenPerLine = gTextWidth - depthInSpaces;
for(int startIndex = 0; startIndex < s.length; ) {
List listCulledLine = getLineWithMaxLen(s, startIndex, lenPerLine, spacesString);
String line = listCulledLine[0];
int lenReturned = listCulledLine[1] as int;
//print("returned len = $lenReturned");
if( line.length == 0 || lenReturned == 0) break;
newString += line;
startIndex += lenReturned;
}
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) {
//print("====================in getLineWithMaxlen. strlen = ${s.length} startIndex = $startIndex len = $len ");
if( startIndex >= s.length)
return ["", 0];
String line = "";
// 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];
int numCharsInLine = 0;
int i = startIndex;
for(; i < startIndex + len && i < s.length; i++) {
line += s[i];
numCharsInLine ++;
if( s[i] == "\n") {
//print(" found newline. also inserting spacesString");
i++;
numCharsInLine = 0;
line += spacesString;
break;
}
}
if(!foundSpace) {
newString += "\n";
newString += spacesString;
numCharsInCurLine = 0;
if( numCharsInLine > len || (numCharsInLine == len && s.length > startIndex + numCharsInLine)) {
//print(" line longer than $len at $numCharsInLine. also inserting spacesString");
line += "\n";
line += spacesString;
}
} else {
newString += "\n";
newString += spacesString;
numCharsInCurLine = 0;
}
}
newString += s[i];
}
numCharsInCurLine++;
}
return newString;
//print(" returning with inserted: ${i - startIndex}");
//print(" returning: |$line|");
return [line, i - startIndex];
}
bool nonEnglish(String str) {

View File

@@ -151,6 +151,11 @@ a & b are orange
List<String> nameColorPalette = [brightGreenColor, brightCyanColor, brightYellowColor, brightMagentaColor,
brightBlueColor, brightRedColor, brightBlackColor, brightWhiteColor,
yellowColor, magentaColor, redColor ];
List<String> nameColorPalette = [brightMagentaColor, brightBlueColor, brightCyanColor, brightGreenColor,
brightYellowColor, brightRedColor, yellowColor, redColor ];
*/
Map<String, String> pubkeyColor = { '0': brightMagentaColor, '1': brightMagentaColor,
@@ -163,13 +168,10 @@ Map<String, String> pubkeyColor = { '0': brightMagentaColor, '1': brightMagentaC
'e': redColor, 'f': redColor
};
List<String> nameColorPalette = [brightMagentaColor, brightBlueColor, brightCyanColor, brightGreenColor,
brightYellowColor, brightRedColor, yellowColor, redColor ];
String getNameColor( String pubkey) {
if( pubkey.length == 0)
return nameColorPalette[0];
return brightMagentaColor;
String firstChar = pubkey.substring(0, 1).toLowerCase();
return pubkeyColor[firstChar]??brightMagentaColor;

View File

@@ -270,7 +270,7 @@ class Tree {
numPrinted += children[i].printTree(depth+1, newerThan, false);
}
// https://gist.github.com/dsample/79a97f38bf956f37a0f99ace9df367b9
if( leftShifted) {
stdout.write("\n");
printDepth(depth+1);

View File

@@ -1,4 +1,5 @@
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/settings.dart';
import 'package:test/test.dart';
import 'package:nostr_console/tree_ds.dart';
@@ -55,5 +56,91 @@ void main() {
node.printTree(0, DateTime.now().subtract(Duration(days:1000)), selectorShowAllTrees); // will test for ~1000 days
});
test('make paragraph', () {
gTextWidth = 120;
//print(gNumLeftMarginSpaces);
//print(gTextWidth);
String paragraph = """
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 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 words
14 Testing paragraph with multiple lines. Testing paragraph with multiple lines. 92 words
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.
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
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
a""";
String res = makeParagraphAtDepth(paragraph, 30);
print(res);
return paragraph == expectedResult;;
});
}