mirror of
https://github.com/TangentFoxy/.lua-files.git
synced 2024-11-20 21:34:23 +00:00
major update to utility functions
This commit is contained in:
parent
90f2f70082
commit
dceba36b67
@ -4,6 +4,7 @@ It's like dotfiles, but no, it's just Lua scripts I find useful.
|
|||||||
## Scripts
|
## Scripts
|
||||||
- `popen-command-test.lua`: A badly-named WIP script for working with LLMs through WSL using [ollama](https://github.com/jmorganca/ollama).
|
- `popen-command-test.lua`: A badly-named WIP script for working with LLMs through WSL using [ollama](https://github.com/jmorganca/ollama).
|
||||||
- `print-arguments.lua`: For testing how a Lua script receives arguments. (It can be platform-specific.)
|
- `print-arguments.lua`: For testing how a Lua script receives arguments. (It can be platform-specific.)
|
||||||
|
- `test.lua`: (Dev Test) Used repeatedly while working on these scripts to verify minor details I'm forgetful about.
|
||||||
- `utility-functions.lua`: (Library) Required for many of these scripts to run.
|
- `utility-functions.lua`: (Library) Required for many of these scripts to run.
|
||||||
- `video-dl.lua`: A few premade command lines for using `yt-dlp` to download what I want quicker.
|
- `video-dl.lua`: A few premade command lines for using `yt-dlp` to download what I want quicker.
|
||||||
|
|
||||||
|
18
test.lua
Normal file
18
test.lua
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env luajit
|
||||||
|
|
||||||
|
-- if utility-functions.lua has an error, this won't show it, so for testing purposes, I don't use it here
|
||||||
|
-- local error_occurred, utility = pcall(function() return require("utility-functions") end) if not error_occurred then error("This script is installed improperly. Follow instructions at https://github.com/TangentFoxy/.lua-files#installation") end
|
||||||
|
utility = require("utility-functions")
|
||||||
|
|
||||||
|
print("---")
|
||||||
|
|
||||||
|
-- local ls = utility.ls()
|
||||||
|
-- ls(function(file_name)
|
||||||
|
-- print(file_name)
|
||||||
|
-- end)
|
||||||
|
|
||||||
|
utility.ls()(function(file_name)
|
||||||
|
print(file_name)
|
||||||
|
end)
|
||||||
|
|
||||||
|
print("---")
|
@ -1,21 +1,40 @@
|
|||||||
math.randomseed(os.time())
|
math.randomseed(os.time())
|
||||||
|
|
||||||
-- TODO look for popen command and fall back to outputting to file if its unavailable
|
local utility = {}
|
||||||
function os.command(command)
|
|
||||||
|
if package.config:sub(1, 1) == "\\" then
|
||||||
|
utility.OS = "Windows"
|
||||||
|
else
|
||||||
|
utility.OS = "UNIX-like"
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO look for popen command and fall back to outputting to file if its unavailable (this should always output a warning!)
|
||||||
|
function os.capture(command)
|
||||||
local file = assert(io.popen(command, 'r'))
|
local file = assert(io.popen(command, 'r'))
|
||||||
local output = assert(file:read('*all'))
|
local output = assert(file:read('*all'))
|
||||||
file:close()
|
file:close()
|
||||||
return output
|
return output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- always uses outputting to a temporary file to guarantee safety
|
||||||
|
function os.capture_safe(command, tmp_file_name)
|
||||||
|
local file_name = tmp_file_name or utility.tmp_file_name()
|
||||||
|
os.execute(command .. " > " .. file_name)
|
||||||
|
|
||||||
|
local file = io.open(file_name, "r")
|
||||||
|
local output = file:read("*all")
|
||||||
|
file:close()
|
||||||
|
os.execute("rm " .. file_name) -- NOTE may not work on all systems, I have a version somewhere that always does
|
||||||
|
return output
|
||||||
|
end
|
||||||
|
|
||||||
-- trim6 from Lua users wiki (best all-round pure Lua performance)
|
-- trim6 from Lua users wiki (best all-round pure Lua performance)
|
||||||
function string.trim(s)
|
function string.trim(s)
|
||||||
return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)'
|
return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)'
|
||||||
end
|
end
|
||||||
|
|
||||||
local utility = {}
|
-- TODO: This needs to use which on a non-Windows platform.
|
||||||
|
-- NOTE: This will sometimes print errors that do not matter. Windows, am I right?
|
||||||
-- NOTE: This will print errors sometimes. :D
|
|
||||||
utility.required_program = function(name)
|
utility.required_program = function(name)
|
||||||
if os.execute("where " .. tostring(name)) ~= 0 then
|
if os.execute("where " .. tostring(name)) ~= 0 then
|
||||||
error(tostring(name) .. " must be installed and in the path")
|
error(tostring(name) .. " must be installed and in the path")
|
||||||
@ -37,9 +56,33 @@ utility.tmp_file_name = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
utility.escape_quotes = function(input)
|
utility.escape_quotes = function(input)
|
||||||
|
-- the order of these commands is important and must be preserved
|
||||||
input = input:gsub("\\", "\\\\")
|
input = input:gsub("\\", "\\\\")
|
||||||
input = input:gsub("\"", "\\\"")
|
input = input:gsub("\"", "\\\"")
|
||||||
return input
|
return input
|
||||||
end
|
end
|
||||||
|
|
||||||
|
utility.ls = function(path)
|
||||||
|
local command
|
||||||
|
if utility.OS == "Windows" then
|
||||||
|
command = "dir /w /b"
|
||||||
|
else
|
||||||
|
command = "ls -1"
|
||||||
|
end
|
||||||
|
|
||||||
|
local tmp_file_name = utility.tmp_file_name()
|
||||||
|
local output = os.capture_safe(command, tmp_file_name)
|
||||||
|
-- local output = os.capture_safe(command)
|
||||||
|
-- output = output:trim() -- verifying that even without trailing newlines, the gmatch below will work
|
||||||
|
-- print(output .. "\n---") -- DEBUG
|
||||||
|
|
||||||
|
return function(fn)
|
||||||
|
for line in output:gmatch("[^\r\n]+") do
|
||||||
|
if line ~= tmp_file_name then -- exclude temporary file name
|
||||||
|
fn(line)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return utility
|
return utility
|
||||||
|
Loading…
Reference in New Issue
Block a user