public inbox for patches@ignore.pl
 help / color / mirror / Atom feed
From: Jakub Slepecki <jakub.slepecki@intel.com>
To: patches@ignore.pl
Cc: please@ignore.pl, Jakub Slepecki <jakub.slepecki@intel.com>
Subject: [PATCH] what: use pl.lapp for what command
Date: Fri, 15 May 2026 14:45:39 +0200	[thread overview]
Message-ID: <20260515124539.477794-1-jakub.slepecki@intel.com> (raw)

Penlight's pl.lapp has a very nice interface for users that want to
do `which what` being somewhat self-documenting.  It has some minor
problems, it misses some *nix or GNU traditions (e.g., lack of - with
file type that's addressed here) and some quirks like useless newlines.
These sound solvable upstream, but for now we need to wrap them.

No functional changes are done to the command interface.  All calls that
were correct are intended to continue being correct.

Remove io.stdin check before closing the input stream.  Lua has an
internal check that fails the io.close() returning error message.
We can just ignore it.

Signed-off-by: Jakub Slepecki <jakub.slepecki@intel.com>
---
 scripts/install |  6 +++---
 what.lua        | 17 ++++++++---------
 what/lapp.lua   | 27 +++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 12 deletions(-)
 create mode 100644 what/lapp.lua

diff --git a/scripts/install b/scripts/install
index 545a82a22d37..4b5d73951926 100755
--- a/scripts/install
+++ b/scripts/install
@@ -26,17 +26,17 @@ fi
 case $ID in
 arch)
 	[ -n "$buildtime" ] && buildtime=busted
-	[ -n "$runtime" ] && runtime=
+	[ -n "$runtime" ] && runtime=lua-penlight
 	echo pacman -S $buildtime $runtime
 	;;
 debian)
 	[ -n "$buildtime" ] && buildtime=lua-busted
-	[ -n "$runtime" ] && runtime=
+	[ -n "$runtime" ] && runtime=lua-penlight
 	echo apt install $buildtime $runtime
 	;;
 ubuntu)
 	[ -n "$buildtime" ] && buildtime=lua-busted
-	[ -n "$runtime" ] && runtime=
+	[ -n "$runtime" ] && runtime=lua-penlight
 	echo apt install $buildtime $runtime
 	;;
 *) echo "ID=$ID" >&2; exit 1;;
diff --git a/what.lua b/what.lua
index 400a908f65ab..cb01fb034170 100755
--- a/what.lua
+++ b/what.lua
@@ -1,12 +1,11 @@
 #!/usr/bin/env lua
+local lapp = require "what.lapp"
+local args = lapp[[
+Usage: what [<input>]
+
+  <input> (input default -)  Pathname or explicit -
+]]
 local config = require "what.config"
 local translate = require "what.translate"
-local input = io.stdin
-local pathname = arg[1]
-if pathname and pathname ~= "-" then
-	input = io.open(pathname) or error(string.format("%s: could not open: %s", arg[0], pathname))
-end
-translate(config, input)
-if input ~= io.stdin then
-	input:close()
-end
+translate(config, args.input)
+io.close(args.input)
diff --git a/what/lapp.lua b/what/lapp.lua
new file mode 100644
index 000000000000..81bbd27a0294
--- /dev/null
+++ b/what/lapp.lua
@@ -0,0 +1,27 @@
+--- Extension module for Penlight's pl.lapp.
+local lapp = require "pl.lapp"
+
+
+--- Built-in file type from pl.lapp has minor limitations.  For one, it does not
+--- support "-" as filename as an alias for standard input.  We do that here.
+---
+--- User must io.close() the file themselves.
+lapp.add_type(
+	"input",
+	function (name)
+		if not name or name == "-" then
+			return io.stdin
+		end
+		return io.open(name, "r") or name
+	end,
+	function (handle)
+		if io.type(handle) ~= "file" then
+			io.stderr:write(
+				string.format("what: could not open: %s\n", handle))
+			os.exit(1)
+			-- lapp.error() prints useless newline
+		end
+	end)
+
+
+return lapp
-- 
2.43.0


             reply	other threads:[~2026-05-15 12:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15 12:45 Jakub Slepecki [this message]
2026-05-15 21:12 ` Aki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260515124539.477794-1-jakub.slepecki@intel.com \
    --to=jakub.slepecki@intel.com \
    --cc=patches@ignore.pl \
    --cc=please@ignore.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox