-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
277 lines (239 loc) · 7.25 KB
/
Copy pathutils.lua
File metadata and controls
277 lines (239 loc) · 7.25 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
-- utils.lua - Utility Functions
local utils = {}
-- Table utility functions
function utils.table_copy(original)
local copy = {}
for key, value in pairs(original) do
if type(value) == "table" then
copy[key] = utils.table_copy(value)
else
copy[key] = value
end
end
return copy
end
-- Add table.count globally if not present
if not table.count then
function table.count(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
end
function utils.table_find(t, value)
for i, v in ipairs(t) do
if v == value then return i end
end
return nil
end
-- String utility functions
function utils.string_trim(str)
return str:match("^%s*(.-)%s*$")
end
-- Add trim method to strings globally for compatibility
if not string.trim then
function string.trim(str)
return str:match("^%s*(.-)%s*$")
end
end
function utils.string_split(str, delimiter)
local result = {}
local pattern = string.format("([^%s]+)", delimiter)
for match in str:gmatch(pattern) do
table.insert(result, match)
end
return result
end
-- Note utility functions
function utils.note_value_to_string(note_value)
if note_value == 120 then return "OFF"
elseif note_value == 121 then return "---"
else
local octave = math.floor(note_value / 12) - 2
local note_names = {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"}
local note_index = (note_value % 12) + 1
return string.format("%s%d", note_names[note_index], octave)
end
end
-- Instrument utility functions
function utils.get_current_instrument()
local song = renoise.song()
return song.selected_instrument
end
function utils.get_current_instrument_index()
local song = renoise.song()
return song.selected_instrument_index
end
-- Sample utility functions
function utils.has_sliced_samples(instrument)
return instrument and #instrument.samples > 1
end
function utils.get_slice_count(instrument)
if not instrument then return 0 end
return math.max(0, #instrument.samples - 1)
end
-- Phrase utility functions
function utils.has_phrases(instrument)
return instrument and #instrument.phrases > 0
end
function utils.get_phrase_count(instrument)
if not instrument then return 0 end
return #instrument.phrases
end
function utils.clear_phrase(phrase)
for i = 1, phrase.number_of_lines do
local line = phrase:line(i)
for j = 1, 12 do
line:note_column(j):clear()
end
for j = 1, 8 do
line:effect_column(j):clear()
end
end
end
-- Dialog utility functions
function utils.calculate_ui_scale(item_count, base_count)
base_count = base_count or 16
return math.max(0.5, math.min(1, base_count / item_count))
end
-- File utility functions
function utils.get_safe_filename(name)
if not name or name == "" then
return "untitled"
end
-- Replace problematic characters with underscores
return name:gsub("[%c%p%s]", "_"):gsub("_+", "_"):gsub("^_+", ""):gsub("_+$", "")
end
function utils.ensure_csv_extension(filepath)
if not filepath:lower():match("%.csv$") then
return filepath .. ".csv"
end
return filepath
end
-- Validation utility functions
function utils.validate_hex_key(key)
return key and key:match("^%x%x$")
end
function utils.validate_break_string(str)
if not str or str == "" then
return false, "Break string cannot be empty"
end
-- Clean the string
local cleaned = str:upper():gsub("%s+", "")
-- Check for valid characters (A-E for now, could extend for composite)
if not cleaned:match("^[A-E]+$") then
return false, "Break string can only contain letters A through E"
end
return true, cleaned
end
-- Debug utility functions
function utils.debug_print_table(t, name, indent)
name = name or "table"
indent = indent or ""
print(indent .. name .. ":")
for k, v in pairs(t) do
if type(v) == "table" then
utils.debug_print_table(v, tostring(k), indent .. " ")
else
print(indent .. " " .. tostring(k) .. ": " .. tostring(v))
end
end
end
function utils.debug_print_phrase_info(phrase, name)
name = name or "phrase"
print("\n=== " .. name .. " Info ===")
print("Name: " .. phrase.name)
print("Lines: " .. phrase.number_of_lines)
print("LPB: " .. phrase.lpb)
local note_count = 0
for i = 1, phrase.number_of_lines do
local line = phrase:line(i)
local note_column = line:note_column(1)
if note_column.note_value ~= renoise.PatternLine.EMPTY_NOTE then
note_count = note_count + 1
print(string.format("Line %02d: %s Inst %02d Delay %03d",
i-1,
utils.note_value_to_string(note_column.note_value),
note_column.instrument_value,
note_column.delay_value))
end
end
print("Total notes: " .. note_count)
print("========================\n")
end
-- Math utility functions
function utils.clamp(value, min_val, max_val)
return math.max(min_val, math.min(max_val, value))
end
function utils.round(value)
return math.floor(value + 0.5)
end
-- Error handling utilities
function utils.safe_call(func, error_message)
local success, result = pcall(func)
if not success then
if error_message then
renoise.app():show_error(error_message .. "\n\nError: " .. tostring(result))
else
renoise.app():show_error("An error occurred: " .. tostring(result))
end
return nil
end
return result
end
function utils.safe_file_operation(filepath, mode, operation, error_context)
local file, err = io.open(filepath, mode)
if not file then
local context = error_context or "file operation"
renoise.app():show_error(string.format("Unable to open file for %s: %s", context, tostring(err)))
return nil
end
local success, result = pcall(operation, file)
file:close()
if not success then
local context = error_context or "file operation"
renoise.app():show_error(string.format("Error during %s: %s", context, tostring(result)))
return nil
end
return result
end
-- Keybinding utility functions
function utils.get_available_keys()
local keys = {}
-- A-T (20 keys)
for i = string.byte('A'), string.byte('T') do
table.insert(keys, string.char(i))
end
-- 0-9 (10 keys)
for i = string.byte('0'), string.byte('9') do
table.insert(keys, string.char(i))
end
return keys
end
function utils.get_key_display_items()
local items = {"None"}
local available_keys = utils.get_available_keys()
for _, key in ipairs(available_keys) do
table.insert(items, key)
end
return items
end
-- Add trim method to strings globally for compatibility
if not string.trim then
function string.trim(str)
return str:match("^%s*(.-)%s*$")
end
end
-- Add table.count globally if not present
if not table.count then
function table.count(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
end
return utils