summaryrefslogtreecommitdiff
path: root/Data/Libraries/Penlight/examples/which.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-30 11:32:16 +0800
committerchai <chaifix@163.com>2021-10-30 11:32:16 +0800
commit42ec7286b2d36a9ba22925f816a17cb1cc2aa5ce (patch)
tree24bc7009457a8d7500f264e89946dc20d069294f /Data/Libraries/Penlight/examples/which.lua
parent164885fd98d48703bd771f802d79557b7db97431 (diff)
+ Penlight
Diffstat (limited to 'Data/Libraries/Penlight/examples/which.lua')
-rw-r--r--Data/Libraries/Penlight/examples/which.lua30
1 files changed, 30 insertions, 0 deletions
diff --git a/Data/Libraries/Penlight/examples/which.lua b/Data/Libraries/Penlight/examples/which.lua
new file mode 100644
index 0000000..0544cfc
--- /dev/null
+++ b/Data/Libraries/Penlight/examples/which.lua
@@ -0,0 +1,30 @@
+-- a simple implementation of the which command. This looks for
+-- the given file on the path. On windows, it will assume an extension
+-- of .exe if no extension is given.
+local List = require 'pl.List'
+local path = require 'pl.path'
+local app = require 'pl.app'
+
+local pathl = List.split(os.getenv 'PATH',path.dirsep)
+
+local function which (file)
+ local res = pathl:map(path.join,file)
+ res = res:filter(path.exists)
+ if res then return res[1] end
+end
+
+local _,lua = app.lua()
+local file = arg[1] or lua -- i.e. location of lua executable
+local try
+
+if not file then return print 'must provide a filename' end
+
+if path.extension(file) == '' and path.is_windows then
+ try = which(file..'.exe')
+else
+ try = which(file)
+end
+
+if try then print(try) else print 'cannot find on path' end
+
+