-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.lua
148 lines (131 loc) · 4.41 KB
/
helpers.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env lua
--------------------------------------------------------
-- -
-- CODE : Hyperspace-Cli Bot v1.0 -
-- LUA : v5.4.7 -
-- Author: Furqonflynn (cmalf) -
-- TG : https://t.me/furqonflynn -
-- GH : https://github.com/cmalf -
-- -
--------------------------------------------------------
-- This code is open-source and welcomes contributions!
--
-- If you'd like to add features or improve this code, please follow these steps:
-- 1. Fork this repository to your own GitHub account.
-- 2. Make your changes in your forked repository.
-- 3. Submit a pull request to the original repository.
--
-- This allows me to review your contributions and ensure the codebase maintains high quality.
--
-- Let's work together to improve this project!
--
-- P.S. Remember to always respect the original author's work and avoid plagiarism.
-- Let's build a community of ethical and collaborative developers.
-- ------------------------------------------------------
local io = io
local os = os
local string = string
local math = math
local Colors = require("colors")
-- Helper function to trim whitespace
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- Helper function to convert a size in bytes into a human-readable string
local function humanize_bytes(n)
if n >= 1073741824 then
return string.format("%.2f GB", n / 1073741824)
elseif n >= 1048576 then
return string.format("%.2f MB", n / 1048576)
elseif n >= 1024 then
return string.format("%.2f KB", n / 1024)
else
return tostring(n) .. " B"
end
end
-- Helper function to process a line from the available models table.
local function processAvailableLine(line)
if not line:find("│") then
return line
end
local fields = {}
-- Split the line into fields based on the vertical bar delimiter.
for field in line:gmatch("│([^│]+)") do
table.insert(fields, field)
end
-- If there are no fields or not enough columns, return the original line.
if #fields < 1 then
return line
end
-- The last field is the size in bytes.
local lastIndex = #fields
local possibleSize = trim(fields[lastIndex])
local sizeNum = tonumber(possibleSize)
if sizeNum then
fields[lastIndex] = " " .. humanize_bytes(sizeNum) .. " "
end
-- Reconstruct the line with the original vertical bar delimiters.
local newLine = "│" .. table.concat(fields, "│") .. "│"
return newLine
end
-- Helper function to clear the console
local function clearConsole()
os.execute("clear")
end
-- Helper function to prompt user to press enter
local function promptEnter(callback)
io.write(Colors.Teal .. Colors.Bright .. "\nEnter to return to the main menu..." .. Colors.Reset)
io.read("*l")
clearConsole()
callback()
end
-- Helper function to prompt user to press enter with custom message
local function promptEnterCustom(message, callback)
io.write(Colors.Blue .. message .. Colors.Reset)
io.read("*l")
clearConsole()
callback()
end
-- Helper function to run shell commands and output logs with color.
local function runCommand(commandStr, onClose)
-- Execute command with os.execute to inherit stdio.
local res = os.execute(commandStr)
if res ~= 0 then
io.stderr:write(Colors.Red .. "Command exited with code " .. tostring(res) .. Colors.Reset .. "\n")
end
if type(onClose) == "function" then
promptEnter(onClose)
end
end
-- Helper
local function exec(command, callback)
local process = io.popen(command .. " 2>&1")
if not process then
callback("Error executing command: " .. command, "", "")
return
end
local stdout = process:read("*a")
local ok, exit_reason, exit_code = process:close()
if not ok then
callback(true, stdout, stdout)
else
callback(nil, stdout, "")
end
end
-- Helper function to prompt a question and return the answer (synchronously)
local function prompt(question)
io.write(Colors.Cyan .. question .. Colors.Reset)
local answer = io.read("*l") or ""
return trim(answer)
end
return {
trim = trim,
humanize_bytes = humanize_bytes,
processAvailableLine = processAvailableLine,
clearConsole = clearConsole,
promptEnter = promptEnter,
promptEnterCustom = promptEnterCustom,
runCommand = runCommand,
exec = exec,
prompt = prompt,
}