From fd65725c0a048e440ad6b3303881294f30e0a949 Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Sun, 11 Sep 2022 17:17:25 +0530 Subject: [PATCH] improved paragraph shifting function and added test. --- lib/event_ds.dart | 112 +++++++++++++++++++++-------------- lib/settings.dart | 10 ++-- lib/tree_ds.dart | 2 +- test/nostr_console_test.dart | 87 +++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 48 deletions(-) diff --git a/lib/event_ds.dart b/lib/event_ds.dart index 6280b1b..c050469 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -370,7 +370,7 @@ class EventData { 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); stdout.write(strToPrint); @@ -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,53 +806,82 @@ 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 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; - break; - } - } - if(!foundSpace) { - newString += "\n"; - newString += spacesString; - numCharsInCurLine = 0; - } - } else { - newString += "\n"; - newString += spacesString; - numCharsInCurLine = 0; - } - } - newString += s[i]; - } - numCharsInCurLine++; + 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( 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 result = false; return result; diff --git a/lib/settings.dart b/lib/settings.dart index b655f88..014b1c1 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -151,6 +151,11 @@ a & b are orange List nameColorPalette = [brightGreenColor, brightCyanColor, brightYellowColor, brightMagentaColor, brightBlueColor, brightRedColor, brightBlackColor, brightWhiteColor, yellowColor, magentaColor, redColor ]; + +List nameColorPalette = [brightMagentaColor, brightBlueColor, brightCyanColor, brightGreenColor, + brightYellowColor, brightRedColor, yellowColor, redColor ]; + + */ Map pubkeyColor = { '0': brightMagentaColor, '1': brightMagentaColor, @@ -163,13 +168,10 @@ Map pubkeyColor = { '0': brightMagentaColor, '1': brightMagentaC 'e': redColor, 'f': redColor }; -List 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; diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index d28d91e..763d6ff 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -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); diff --git a/test/nostr_console_test.dart b/test/nostr_console_test.dart index ec04d3e..ad45d82 100644 --- a/test/nostr_console_test.dart +++ b/test/nostr_console_test.dart @@ -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;; + }); }