mirror of
https://github.com/vishalxl/nostr_console.git
synced 2025-12-06 18:51:08 +01:00
improved paragraph shifting function
and added test.
This commit is contained in:
@@ -370,7 +370,7 @@ class EventData {
|
|||||||
temp = temp + "$idDateLikes";
|
temp = temp + "$idDateLikes";
|
||||||
}
|
}
|
||||||
//temp = temp + " |$idDateLikes";
|
//temp = temp + " |$idDateLikes";
|
||||||
String contentShifted = makeParagraphAtDepth( temp, gSpacesPerDepth * depth + effectiveNameFieldLen);
|
String contentShifted = makeParagraphAtDepth( temp, gSpacesPerDepth * depth + effectiveNameFieldLen);
|
||||||
|
|
||||||
strToPrint += getStrInColor(contentShifted + "\n", commentColor);
|
strToPrint += getStrInColor(contentShifted + "\n", commentColor);
|
||||||
stdout.write(strToPrint);
|
stdout.write(strToPrint);
|
||||||
@@ -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,53 +806,82 @@ 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;
|
|
||||||
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;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!foundSpace) {
|
|
||||||
newString += "\n";
|
|
||||||
newString += spacesString;
|
|
||||||
numCharsInCurLine = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
newString += "\n";
|
|
||||||
newString += spacesString;
|
|
||||||
numCharsInCurLine = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newString += s[i];
|
|
||||||
}
|
|
||||||
numCharsInCurLine++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return newString;
|
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( numCharsInLine > len || (numCharsInLine == len && s.length > startIndex + numCharsInLine)) {
|
||||||
|
//print(" line longer than $len at $numCharsInLine. also inserting spacesString");
|
||||||
|
line += "\n";
|
||||||
|
line += spacesString;
|
||||||
|
}
|
||||||
|
|
||||||
|
//print(" returning with inserted: ${i - startIndex}");
|
||||||
|
//print(" returning: |$line|");
|
||||||
|
return [line, i - startIndex];
|
||||||
|
}
|
||||||
|
|
||||||
bool nonEnglish(String str) {
|
bool nonEnglish(String str) {
|
||||||
bool result = false;
|
bool result = false;
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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;;
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user