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

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
import 'package:nostr_console/event_ds.dart'; import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/settings.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:nostr_console/tree_ds.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 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;;
});
} }