adding color to names in SN

This commit is contained in:
Vishal 2022-09-10 19:33:20 +05:30
parent ae95fc5199
commit 8bc965709d
3 changed files with 63 additions and 16 deletions

View File

@ -144,8 +144,8 @@ Future<void> main(List<String> arguments) async {
if( argResults[colorArg] != null) { if( argResults[colorArg] != null) {
String colorGiven = argResults[colorArg].toString().toLowerCase(); String colorGiven = argResults[colorArg].toString().toLowerCase();
if( gColorMap.containsKey(colorGiven)) { if( gColorMapForArguments.containsKey(colorGiven)) {
String color = gColorMap[colorGiven]??""; String color = gColorMapForArguments[colorGiven]??"";
if( color == "") { if( color == "") {
print("Invalid color."); print("Invalid color.");
} else } else

View File

@ -307,8 +307,26 @@ class EventData {
} else { } else {
commentColor = gCommentColor; commentColor = gCommentColor;
} }
int extraLen = name.length + 4; //name = "$name";
//name = name.padLeft(gNameLengthInPost);
int nameEffectiveLen = name.length < gNameLengthInPost? name.length: gNameLengthInPost;
name = name.substring(0,nameEffectiveLen);
String nameInside = " ";
if( name.length %2 == 1 ) // odd len names need an extra dash
name = name + nameInside;
String nameColor = getNameColor(name);
name = getNumDashes((gNameLengthInPost - name.length)~/2, nameInside ) + getStrInColor(name, nameColor) + getNumDashes((gNameLengthInPost - name.length)~/2 , nameInside);
int extraLen = name.length + 3; // get this before name is mangled by color
//name = getStrInColor(name, nameColor);
// colorify the name
String strToPrint = ""; String strToPrint = "";
//strToPrint += getDepthSpaces(depth); //strToPrint += getDepthSpaces(depth);
@ -316,7 +334,7 @@ class EventData {
strToPrint += "\n"; strToPrint += "\n";
} }
strToPrint += getDepthSpaces(depth); strToPrint += getDepthSpaces(depth);
strToPrint += " ${name}: "; strToPrint += " ${name}: ";
const int typicalxLen = "|id: 82b5 , 12:04 AM Sep 19".length + 5; // not sure where 5 comes from const int typicalxLen = "|id: 82b5 , 12:04 AM Sep 19".length + 5; // not sure where 5 comes from
@ -706,22 +724,29 @@ String getAuthorName(String pubkey, [int len = 3]) {
} }
// returns full public key(s) for the given username( which can be first few letters of pubkey, or the user name) // returns full public key(s) for the given username( which can be first few letters of pubkey, or the user name)
Set<String> getPublicKeyFromName(String userName) { Set<String> getPublicKeyFromName(String inquiredName) {
if( inquiredName.length < 2) {
return {};
}
Set<String> pubkeys = {}; Set<String> pubkeys = {};
//if(gDebug > 0) print("In getPublicKeyFromName: doing lookup for $userName len of gKindONames= ${gKindONames.length}"); //if(gDebug > 0) print("In getPublicKeyFromName: doing lookup for $userName len of gKindONames= ${gKindONames.length}");
gKindONames.forEach((pk, userInfo) { gKindONames.forEach((pubkey, userInfo) {
// check both the user name, and the pubkey to search for the user // check both the user name, and the pubkey to search for the user
//print(userInfo.name);
if( userName == userInfo.name) { // check username
pubkeys.add(pk); if( userInfo.name != null) {
int minNameLen = min( inquiredName.length, (userInfo.name?.length)??0);
if( inquiredName.toLowerCase() == userInfo.name?.substring(0, minNameLen).toLowerCase()) {
pubkeys.add(pubkey);
}
} }
if( userName.length <= pk.length) { // check public key
//print("$pk $userName" ); if( inquiredName.length <= pubkey.length) {
if( pk.substring(0, userName.length) == userName) { if( pubkey.substring(0, inquiredName.length) == inquiredName) {
pubkeys.add(pk); pubkeys.add(pubkey);
} }
} }
}); });

View File

@ -86,7 +86,7 @@ List<String> gDefaultFollows = [
// dummy account pubkey // dummy account pubkey
const String gDummyAccountPubkey = "Non"; const String gDummyAccountPubkey = "Non";
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// UI and Color related settings //////////////////////////////////////////////////////////////////////////////////////////////////////////////// UI and Color
const int gMinValidTextWidth = 60; // minimum text width acceptable const int gMinValidTextWidth = 60; // minimum text width acceptable
const int gDefaultTextWidth = 100; // default text width const int gDefaultTextWidth = 100; // default text width
int gTextWidth = gDefaultTextWidth; // is changed by --width option int gTextWidth = gDefaultTextWidth; // is changed by --width option
@ -94,6 +94,7 @@ const int gSpacesPerDepth = 4; // constant
int gNumLeftMarginSpaces = 0;// this number is modified in main int gNumLeftMarginSpaces = 0;// this number is modified in main
String gAlignment = "center"; // is modified in main if --align argument is given String gAlignment = "center"; // is modified in main if --align argument is given
const int gapBetweenTopTrees = 1; const int gapBetweenTopTrees = 1;
const int gNameLengthInPost = 12;
// after depth of maxDepthAllowed the thread is re-aligned to left by leftShiftThreadBy // after depth of maxDepthAllowed the thread is re-aligned to left by leftShiftThreadBy
const int gMinimumDepthAllowed = 2; const int gMinimumDepthAllowed = 2;
@ -102,29 +103,50 @@ const int gDefaultMaxDepth = 4;
int maxDepthAllowed = gDefaultMaxDepth; int maxDepthAllowed = gDefaultMaxDepth;
const int leftShiftThreadsBy = 2; const int leftShiftThreadsBy = 2;
// https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#8-colors
// Color related settings // Color related settings
const String defaultTextColor = "green"; const String defaultTextColor = "green";
const String greenColor = "\x1B[32m"; // green const String greenColor = "\x1B[32m"; // green
const String yelloColor = "\x1B[33m"; // yellow
const String magentaColor = "\x1B[35m"; // magenta
const String cyanColor = "\x1b[36m"; // cyan const String cyanColor = "\x1b[36m"; // cyan
const String whiteColor = "\x1b[97m"; // white const String whiteColor = "\x1b[97m"; // white
const String blackColor = "\x1b[30m"; // black const String blackColor = "\x1b[30m"; // black
const String redColor = "\x1B[31m"; // red const String redColor = "\x1B[31m"; // red
const String blueColor = "\x1b[34m"; // blue const String blueColor = "\x1b[34m"; // blue
Map<String, String> gColorMap = { "green": greenColor, Map<String, String> gColorMapForArguments = { "green": greenColor,
"cyan" : cyanColor, "cyan" : cyanColor,
"white": whiteColor, "white": whiteColor,
"black": blackColor, "black": blackColor,
"red" : redColor, "red" : redColor,
"blue" : blueColor}; "blue" : blueColor};
const String brightRedColor = "\x1B[91m"; // bright red
const String brightGreenColor = "\x1B[92m"; // bright green const String brightGreenColor = "\x1B[92m"; // bright green
const String brightYellowColor = "\x1B[93m"; // bright yellow
const String brightBlueColor = "\x1B[94m"; // bright blue
const String brightCyanColor = "\x1B[96m"; // bright cyan
const String brightMagentaColor = "\x1B[95m"; // bright magenta
// 33 yellow, 31 red, 34 blue, 35 magenta. Add 60 for bright versions. // 33 yellow, 31 red, 34 blue, 35 magenta. Add 60 for bright versions.
String gCommentColor = greenColor; String gCommentColor = greenColor;
String gNotificationColor = cyanColor; // cyan String gNotificationColor = cyanColor; // cyan
String gWarningColor = redColor; // red String gWarningColor = redColor; // red
const String gColorEndMarker = "\x1B[0m"; const String gColorEndMarker = "\x1B[0m";
List<String> nameColorPalette = [brightGreenColor, brightCyanColor, brightYellowColor, brightMagentaColor, brightBlueColor, brightRedColor,
yelloColor, magentaColor, redColor ];
String getNameColor( String name) {
if( name.length == 0)
return nameColorPalette[0];
int offset = name.substring(0, 1).codeUnits[0] % nameColorPalette.length;
return nameColorPalette[offset];
}
// By default the threads that were started in last one day are shown // By default the threads that were started in last one day are shown
// this can be changed with 'days' command line argument // this can be changed with 'days' command line argument