From 2642f263fa5aef2c72904fb5a9749f2bc0fb1e30 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Debian Linux)" <rob@synchro.net>
Date: Sat, 3 May 2025 18:22:52 -0700
Subject: [PATCH] Support left/right justification

Part of issue/request #922

I don't know if we really want separate cycling/selection of foreground
and background colors (most foreground colors aren't good combos for
most background colors) or the separate cycling/selection of the upper,
mid, and lower parts of the border. These are doable, but I'm not sure
the majority of the results would look good.
---
 exec/load/meme_lib.js | 14 +++++++++++---
 exec/postmeme.js      | 22 ++++++++++++++++++----
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/exec/load/meme_lib.js b/exec/load/meme_lib.js
index e24be7d073..e6b16ca9f9 100755
--- a/exec/load/meme_lib.js
+++ b/exec/load/meme_lib.js
@@ -14,6 +14,11 @@ var BORDER_ORNATE2 = 7;
 var BORDER_ORNATE3 = 8;
 var BORDER_COUNT = 9;
 
+var JUSTIFY_CENTER = 0;
+var JUSTIFY_LEFT = 1;
+var JUSTIFY_RIGHT = 2;
+var JUSTIFY_COUNT = 3;
+
 // We don't have String.repeat() in ES5
 function repeat(ch, length)
 {
@@ -118,16 +123,19 @@ function bottom_border(border, width)
 	return str + "\x01N\r\n";
 }
 
-function generate(attr, border, text)
+function generate(width, attr, border, text, justify)
 {
-	var width = 39;
 	var msg = attr + top_border(border, width);
 	var array = word_wrap(text, width - 4).split("\n");
 	for (var i in array) {
 		var line = truncsp(array[i]);
 		if (!line && i >= array.length - 1)
 			break;
-		var margin = Math.floor((width - line.length) / 2);
+		var margin = 2;
+		if (justify == JUSTIFY_CENTER)
+			margin = Math.floor((width - line.length) / 2);
+		else if (justify == JUSTIFY_RIGHT)
+			margin = width - (line.length + 2);
 		msg += attr + mid_border(border, width, margin, line);
 	}
 	msg += attr + bottom_border(border, width);
diff --git a/exec/postmeme.js b/exec/postmeme.js
index 83a20d4f09..36766fdea7 100755
--- a/exec/postmeme.js
+++ b/exec/postmeme.js
@@ -6,6 +6,8 @@
 //   border (default: 0)
 //   random (default: false)
 //   max_length (default: 500)
+//   width (default: 39)
+//   justify (default: center)
 
 "use strict";
 require("key_defs.js", "KEY_LEFT");
@@ -17,8 +19,8 @@ var lib = load({}, "meme_lib.js");
 
 function choose(border)
 {
-	console.mnemonics(format("Style: ~Color, ~@Next@, ~@Previous@, or ~@Quit@ [%u]: ", (border % lib.BORDER_COUNT) + 1));
-	var ch = console.getkeys("C" + KEY_LEFT + KEY_RIGHT + "\r" + console.next_key + console.prev_key + console.quit_key, lib.BORDER_COUNT);
+	console.mnemonics(format("~Border, ~Color, ~Justify, ~@Quit@, or [Select]: "));
+	var ch = console.getkeys("BCJ" + KEY_LEFT + KEY_RIGHT + "\r" + console.next_key + console.prev_key + console.quit_key, lib.BORDER_COUNT);
 	if (typeof ch == "number")
 		return ch - 1;
 	switch (ch) {
@@ -27,7 +29,9 @@ function choose(border)
 		case '\r':
 			return true;
 		case 'C':
-			return 'C';
+		case 'J':
+		case 'B':
+			return ch;
 		case KEY_UP:
 		case KEY_LEFT:
 		case console.prev_key:
@@ -50,6 +54,7 @@ var attr = [
 	"\x01H\x01W\x016",
 	"\x01N\x01K\x017",
 ];
+var justify = options.justify || 0;
 var border = options.border || 0;
 var color = options.color || 0;
 if (options.random) {
@@ -58,8 +63,13 @@ if (options.random) {
 }
 var msg;
 while (!js.terminated) {
-	msg = lib.generate(attr[color % attr.length], border  % lib.BORDER_COUNT, text);
+	msg = lib.generate(options.width || 39, attr[color % attr.length], border  % lib.BORDER_COUNT, text, justify % lib.JUSTIFY_COUNT);
 	console.clear();
+	console.attributes = WHITE | HIGH;
+	console.print(format("Meme \x01N\x01C(border \x01H%u \x01N\x01Cof \x01H%u\x01N\x01C, color \x01H%u\x01N\x01C of \x01H%u\x01N\x01C):"
+		, (border % lib.BORDER_COUNT) + 1, lib.BORDER_COUNT
+		, (color % attr.length) + 1, attr.length));
+	console.newline(2);
 	print(msg);
 	var ch = choose(border);
 	if (ch === false)
@@ -70,6 +80,10 @@ while (!js.terminated) {
 		border = ch;
 	else if (ch === 'C')
 		++color;
+	else if (ch === 'J')
+		++justify;
+	else if (ch === 'B')
+		++border;
 	else if (ch == console.next_key && border < lib.BORDER_COUNT - 1)
 		++border;
 	else if (ch == console.prev_key && border > 0)
-- 
GitLab