In Lua, you can remove all elements from a table by setting all the table's keys to nil. This can be done either manually using a loop or by creating a new empty table and assigning it to the previous table's variable. Here are the two common methods:
Method 1: Using a Loop
You can iterate over the table and set each key to nil.
lua
local myTable = {1, 2, 3, 4, 5}
-- Loop through the table and set each key to nil
for key in pairs(myTable) do
myTable[key] = nil
end
-- myTable is now an empty table
print(#myTable) -- Output will be 0
Method 2: Reassigning a New Table
Create a new empty table and reassign it to the variable.
lua
local myTable = {1, 2, 3, 4, 5}
-- Reassign myTable to an empty table
myTable = {}
-- myTable is now an empty table
print(#myTable) -- Output will be 0
Method 3: Using table library functions
Lua's built-in table library does not provide a direct function to clear a table, so the above methods are commonly used.
Important Notes
Using a loop (Method 1) will clear out all existing keys in the table but retains the same table object.
Reassigning the table (Method 2) creates a new table and the old table will eventually be garbage collected if there are no other references to it.
Method 1 can be useful if you need to retain the table reference (such as when other parts of your program hold references to the same table), while Method 2 is simpler if you can reassign freely without concerns about lingering references.
local function VSliderDouble_Speed(ctx, label, width, height, v, min, max, speed, format)
local changed = false
format = format or '%.02f'
local draw_list = ImGui.GetWindowDrawList(ctx)
local inner_spacing_x, inner_spacing_y =
ImGui.GetStyleVar(ctx, ImGui.StyleVar_ItemInnerSpacing)
local frame_padding_x, frame_padding_y =
ImGui.GetStyleVar(ctx, ImGui.StyleVar_FramePadding)
ImGui.BeginGroup(ctx)
-- hit box
ImGui.InvisibleButton(ctx, label, width, height)
local x1, y1 = ImGui.GetItemRectMin(ctx)
local x2, y2 = ImGui.GetItemRectMax(ctx)
local active = ImGui.IsItemActive(ctx)
local hovered = ImGui.IsItemHovered(ctx)
-- compute value
local grab_pad, grab_sz = 2, ImGui.GetStyleVar(ctx, ImGui.StyleVar_GrabMinSize)
local usable_h = height - (grab_pad * 2) - grab_sz
local frac = 1 - ((v - min) / (max - min))
if ImGui.IsItemActivated(ctx) then
local mouse_y = select(2, ImGui.GetMousePos(ctx)) - (y1 + grab_pad + grab_sz / 2)
changed, frac = true, mouse_y / usable_h
elseif active then
local mouse_dy = select(2, ImGui.GetMouseDragDelta(ctx, nil, nil,
ImGui.MouseButton_Left, 0))
ImGui.ResetMouseDragDelta(ctx)
local range = 1 / (max - min)
changed, frac = true, frac + (mouse_dy * range * speed)
end
if changed then
frac = math.max(0, math.min(1, frac))
v = ((max - min) * (1 - frac)) + min
end
-- background
ImGui.DrawList_AddRectFilled(draw_list, x1, y1, x2, y2,
ImGui.GetColor(ctx, active and ImGui.Col_FrameBgActive or
hovered and ImGui.Col_FrameBgHovered or ImGui.Col_FrameBg))
-- grab box
local grab_x1, grab_y1 = x1 + grab_pad, y1 + grab_pad + math.floor(usable_h * frac)
local grab_x2, grab_y2 = x2 - grab_pad, grab_y1 + grab_sz
ImGui.DrawList_AddRectFilled(draw_list, grab_x1, grab_y1, grab_x2, grab_y2,
ImGui.GetColor(ctx, active and ImGui.Col_SliderGrabActive or ImGui.Col_SliderGrab),
ImGui.GetStyleVar(ctx, ImGui.StyleVar_GrabRounding))
-- value
local v_f = (format):format(v)
local v_w = ImGui.CalcTextSize(ctx, v_f)
local v_x = math.max(x1, x1 + ((width - v_w) / 2))
-- Enable these 2 lines to get the visual value of the slider
--ImGui.DrawList_AddTextEx(draw_list, nil, 0, v_x, y1 + frame_padding_y,
--ImGui.GetColor(ctx, ImGui.Col_Text), v_f, 0, x1, y1, x2, y2)
-- end of CFillion's Vertical Slider
function get_files_in_directory(directory, files)
files = files or {}
local idx = 0
local file = reaper.EnumerateFiles(directory, idx)
code
while file do
if not file:match("^%.") then -- Skip hidden files
table.insert(files, directory .. "\\" .. file)
end
idx = idx + 1
file = reaper.EnumerateFiles(directory, idx)
end
idx = 0
local subdirectory = reaper.EnumerateSubdirectories(directory, idx)
while subdirectory do
if not subdirectory:match("^%.") then -- Skip hidden directories
get_files_in_directory(directory .. "\\" .. subdirectory, files)
end
idx = idx + 1
subdirectory = reaper.EnumerateSubdirectories(directory, idx)
end
return files
end
function files_variables()
idx_min = 0
idx_max = 30 -- change this to show more items
idx = 0
end
function fonts()
font1 = reaper.ImGui_CreateFont("Arial", 16)
font2 = reaper.ImGui_CreateFont("Arial", 14)
reaper.ImGui_Attach(ctx, font1)
reaper.ImGui_Attach(ctx, font2)
end
function images()
fader_image = reaper.ImGui_CreateImage("C:\Users\franc\Downloads\mcp_volthumb.png")
fader_line_image = reaper.ImGui_CreateImage("C:\Users\franc\Downloads\gen_volbg_vert.png")
preferences_image = reaper.ImGui_CreateImage("C:\Users\franc\Downloads\preference.png")
reaper.ImGui_Attach(ctx, fader_image)
reaper.ImGui_Attach(ctx, fader_line_image)
reaper.ImGui_Attach(ctx, preferences_image)
end
function default_preferences()
dark_mode = true
light_mode = false
function get_file_names()
if files then
filenames = {} -- Initialize or clear the filenames table
for _, file in ipairs(files) do
local fileName = file:match("^.+\([^\]+)$") -- extract the file name from the full path
table.insert(filenames, fileName)
end
table.sort(filenames) -- Sort after inserting all file names
end
end
function GUI()
code
end
function main()
code
reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_WindowBg(), 0x2C2C2CFF)
local is_visible, is_open = reaper.ImGui_Begin(ctx, "Media Browser", true)
if is_visible then
reaper.ImGui_PushFont(ctx, font1)
GUI()
reaper.ImGui_PopFont(ctx)
reaper.ImGui_End(ctx)
end
reaper.ImGui_PopStyleColor(ctx)
if is_open then
reaper.defer(main)
end
end
--directory_for_test = "C:\Users\franc\Desktop\Desktop (A organizar)\RYKORD\Projects\Bounces"
ctx = reaper.ImGui_CreateContext("Media Browser")
files_variables()
fonts()
images()
default_preferences()
reaper.ImGui_SetNextWindowDockID(ctx, -7)
main()
-- PROFILER END
profiler.attachToWorld() -- after all functions have been defined
profiler.run()
some filenames aren't sorted by alphabetical order. give me brief explanation why
If some filenames are not being sorted alphabetically, there are a few potential reasons for this. Here are some common causes and potential solutions to check for:
1. File Extraction
Ensure that filenames are correctly extracted from file paths. If the separation and extraction logic has issues, the filenames array might contain unexpected results.
The function responsible for extracting filenames looks as follows:
lua
function get_file_names()
if files then
filenames = {} -- Initialize or clear the filenames table
for _, file in ipairs(files) do
local fileName = file:match("^.+\\([^\\]+)$") -- extract the file name from the full path
table.insert(filenames, fileName)
end
table.sort(filenames) -- Sort after inserting all file names
end
end
Ensure the file:match("^.+\\([^\\]+)$") pattern correctly extracts the filename from the path.
2. Sorting Operation
When sorting the filenames table, Lua's table.sort() function performs a standard alphabetical sort. It's essential to ensure it is being applied correctly and after all filenames have been inserted into the table.
3. Case Sensitivity in Sorting
By default, table.sort() in Lua is case-sensitive, meaning "A" comes before "a," and so forth. This can cause issues if your filenames have mixed case and you want a case-insensitive sort.
You can use a custom sort function to perform case-insensitive sorting:
lua
table.sort(filenames, function(a, b)
return a:lower() < b:lower()
end)
4. Presence of Numeric or Special Characters
Filenames containing numeric or special characters might have an unexpected order when sorted alphabetically. You might need to handle these cases separately if they disrupt the natural order you expect.
5. Debugging
Print out filenames before and after sorting to verify the correct order and identify potential issues:
lua
function get_file_names()
if files then
filenames = {} -- Initialize or clear the filenames table
for _, file in ipairs(files) do
local fileName = file:match("^.+\\([^\\]+)$") -- extract the file name from the full path
table.insert(filenames, fileName)
end
-- Print filenames before sorting
for i, v in ipairs(filenames) do
print('Before:', v)
end
-- Sort the filenames
table.sort(filenames)
-- Print filenames after sorting
for i, v in ipairs(filenames) do
print('After:', v)
end
end
end
Conclusion
Ensure the filenames are correctly extracted, use a case-insensitive sort if needed, and carefully check for any syntax or logical errors. By following these steps, you can identify why some filenames aren't sorted alphabetically and address the root cause effectively.
reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_FrameRounding(), 1)
reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_FrameBg(), 0x1C1C1CFF)
reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_Button(), 0x1C1C1CFF)
-- SEARCH BAR
reaper.ImGui_SetNextItemWidth(ctx, 300)
reaper.ImGui_InputTextWithHint(ctx, "##", "SEARCH (SAMPLES, FX, ETC..)")
-- FILTER BUTTON
reaper.ImGui_SameLine(ctx)
if reaper.ImGui_BeginPopup(ctx, "FILTER POPUP") then
if reaper.ImGui_Checkbox(ctx, "WAV", wav_filter) then
wav_filter = not wav_filter
end
if reaper.ImGui_Checkbox(ctx, "MP3", mp3_filter) then
mp3_filter = not mp3_filter
end
if reaper.ImGui_Checkbox(ctx, "AIFF", aiff_filter) then
aiff_filter = not aiff_filter
end
if reaper.ImGui_Checkbox(ctx, "PNG", png_filter) then
png_filter = not png_filter
end
if reaper.ImGui_Checkbox(ctx, "MP4", mp4_filter) then
mp4_filter = not mp4_filter
end
if reaper.ImGui_Checkbox(ctx, "VST2", vst2_filter) then
vst2_filter = not vst2_filter
end
if reaper.ImGui_Checkbox(ctx, "VST3", vst3_filter) then
vst3_filter = not vst3_filter
end
reaper.ImGui_EndPopup(ctx)
end
if reaper.ImGui_Button(ctx, "FILTER", 102) then
reaper.ImGui_OpenPopup(ctx, "FILTER POPUP")
end
-- PREFERENCES BUTTON
reaper.ImGui_SameLine(ctx)
if reaper.ImGui_BeginPopup(ctx, "HELLO") then
reaper.ImGui_PushFont(ctx, font2)
reaper.ImGui_Text(ctx, "THEME")
if reaper.ImGui_RadioButton(ctx, "LIGHT MODE", light_mode) then
light_mode = true
dark_mode = false
end
reaper.ImGui_SameLine(ctx)
if reaper.ImGui_RadioButton(ctx, "DARK MODE", dark_mode) then
dark_mode = true
light_mode = false
end
reaper.ImGui_Text(ctx, "FONT")
if reaper.ImGui_RadioButton(ctx, "ARIAL", arial_font) then
arial_font = true
default_font = false
end
reaper.ImGui_SameLine(ctx)
if reaper.ImGui_RadioButton(ctx, "ORIGINAL", default_font) then
default_font = true
arial_font = false
end
reaper.ImGui_PopFont(ctx)
reaper.ImGui_EndPopup(ctx)
end
if reaper.ImGui_Button(ctx, "##Preferences Button", 20) then
reaper.ImGui_OpenPopup(ctx, "HELLO")
end
reaper.ImGui_SetCursorPos(ctx, 429, 12)
reaper.ImGui_Image(ctx, preferences_image, 15, 15)
reaper.ImGui_SetCursorPosY(ctx, 35)
-- FILE WINDOW
if reaper.ImGui_BeginChild(ctx, "##File Windoww", 300, 650, reaper.ImGui_ChildFlags_Border()|reaper.ImGui_ChildFlags_FrameStyle()) then
-- FILES
-- IF ENTER KEY IS PRESSED
filenames = filenames or {}
-- Display the selectables with sorted filenames
local itemsPerPage = 20
local fileCount = #filenames
idx_max = math.min(idx_min + itemsPerPage, fileCount)
-- Display the selectables with sorted filenames within the current page
for i = idx_min + 1, idx_max do
if filenames[i] then -- Check if the index is valid
reaper.ImGui_Selectable(ctx, filenames[i]) -- display only the file name
end
end
if reaper.ImGui_GetMouseWheel(ctx) ~= 0 then
if idx_min >= 0 then
idx_max = idx_max - reaper.ImGui_GetMouseWheel(ctx)
idx_min = idx_min - reaper.ImGui_GetMouseWheel(ctx)
if idx_min < 0 then
idx_min = 0
idx_max = 10
--elseif idx_max > #files then
-- idx_min = idx_max - 10
--idx_max = #files
end
end
end
-- FILE SCROLL BAR
reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_SliderGrab(), 0x4C4C4CFF)
reaper.ImGui_SetCursorPos(ctx, 288, 0)
reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_GrabRounding(), 12)
reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_FrameBgHovered(), 0x1C1C1CFF)
reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_FrameBgActive(), 0x1C1C1CFF)
reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_SliderGrabActive(), 0x4C4C4CFF)
local touched, vslidervalue = VSliderDouble_Speed(ctx, " ", 12, 640, idx_min, 0 , 2, 0.2)
if touched then
idx_min = idx_min + math.ceil(vslidervalue)
end
reaper.ImGui_PopStyleVar(ctx, 1)
reaper.ImGui_PopStyleColor(ctx, 4)
reaper.ImGui_EndChild(ctx)
end
-- DIRECTORY WINDOW
reaper.ImGui_SameLine(ctx)
if reaper.ImGui_BeginChild(ctx, "##DIR Window", 130, 650, reaper.ImGui_ChildFlags_Border()|reaper.ImGui_ChildFlags_FrameStyle()) then
reaper.ImGui_Button(ctx, "PLUG-INS", 122, 20)
reaper.ImGui_Button(ctx, "MIDI TOOLS", 122, 20)
if reaper.ImGui_Button(ctx, "BOUNCES", 122, 20) then
files = {}
files = get_files_in_directory("C:\\Users\\franc\\Desktop\\Desktop (A organizar)\\RYKORD\\Projects\\Bounces")
filenames = {}
get_file_names()
-- sort them
end
if reaper.ImGui_Button(ctx, "+", 122, 20) then
-- Add directory functions
end
reaper.ImGui_EndChild(ctx)
end
-- PLAY BUTTON
if reaper.ImGui_ArrowButton(ctx, "##PlayButton", 1) then
-- Preview File Function
end
-- PREVIEW VOLUME SLIDER
reaper.ImGui_SameLine(ctx)
reaper.ImGui_SetNextItemWidth(ctx, 270)
reaper.ImGui_SetCursorPosX(ctx, 50)
reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_Alpha(), 0)
local changed, new_value = reaper.ImGui_SliderDouble(ctx, "##Volume Slider", value, 0, 2)
if changed then
value = new_value
end
reaper.ImGui_PopStyleVar(ctx, 1)
reaper.ImGui_SetCursorPos(ctx, 30, 692)
reaper.ImGui_Image(ctx, fader_line_image, 295, 15)
reaper.ImGui_SetCursorPos(ctx, 30 + new_value * 120, 682)
reaper.ImGui_Image(ctx, fader_image, 48, 36)
reaper.ImGui_PopStyleColor(ctx, 2)
reaper.ImGui_PopStyleVar(ctx, 1)