From f3bc6d707905a5bffe8478478fc1e1ab0716ac5a Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Debian Linux)" <rob@synchro.net>
Date: Thu, 24 Apr 2025 17:43:40 -0700
Subject: [PATCH] Auto-detect the screen width when the width option isn't
 specified

This fixes the center/right justification options for terminals not exactly
80 columns in width.
---
 exec/load/tdfonts_lib.js | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/exec/load/tdfonts_lib.js b/exec/load/tdfonts_lib.js
index 657af79a4c..7ec57fd3eb 100644
--- a/exec/load/tdfonts_lib.js
+++ b/exec/load/tdfonts_lib.js
@@ -129,7 +129,7 @@ function loadfont(fn_arg) {
     if (this.opt && opt.info) {
 		writeln("index: " + opt.index);
 		writeln("font: " + font.name);
-		writeln("char list: ");
+		write("char list: ");
     }
 
     // Determine overall font height and validate glyph addresses
@@ -380,11 +380,19 @@ function output(str, font) {
         }
     }
 
+	var width = this.opt && opt.width;
+	if (!width) {
+		if (js.global.console) // Auto-detect screen width, when possible
+			width = console.screen_columns;
+		else
+			width = DEFAULT_WIDTH;
+	}
+
     // Calculate padding for justification
     if (this.opt && opt.justify === CENTER_JUSTIFY) {
-        padding = Math.floor((opt.width - linewidth) / 2);
+        padding = Math.floor((width - linewidth) / 2);
     } else if (this.opt && opt.justify === RIGHT_JUSTIFY) {
-        padding = opt.width - linewidth;
+        padding = width - linewidth;
     }
 
     // Ensure padding is not negative
-- 
GitLab