Module:MOS modes: Difference between revisions

Ganaram inukshuk (talk | contribs)
Renamed params per guidance of fredg999
Ganaram inukshuk (talk | contribs)
Added support for multiple columns
Line 48: Line 48:


-- Helper function that parses mode names from a semicolon-delimited string and returns the names in an array
-- Helper function that parses mode names from a semicolon-delimited string and returns the names in an array
function p.parse_mode_names(mode_names_unparsed)
function p.parse_entries(mode_names_unparsed)
local mode_names = {}
local mode_names = {}
for name in string.gmatch(mode_names_unparsed, '([^;]+)') do
for name in string.gmatch(mode_names_unparsed, '([^;]+)') do
Line 70: Line 70:
if mosstring == "5L 2s" then
if mosstring == "5L 2s" then
mode_names_unparsed = "Lydian; Ionian (major); Mixolydian; Dorian; Aeolian (minor); Phrygian; Locrian"
mode_names_unparsed = "Lydian; Ionian (major); Mixolydian; Dorian; Aeolian (minor); Phrygian; Locrian"
mode_names = p.parse_mode_names(mode_names_unparsed)
mode_names = p.parse_entries(mode_names_unparsed)
else
else
mode_names_unparsed = frame.args['Mode Names']
mode_names_unparsed = frame.args['Mode Names']
mode_names = p.parse_mode_names(mode_names_unparsed)
mode_names = p.parse_entries(mode_names_unparsed)
end
end
-- Get the mos's modes
-- Get the mos's modes and the mode count
local mos_modes = p.modes_by_brightness(input_mos)
local mos_modes = p.modes_by_brightness(input_mos)
local periods = rat.gcd(input_mos.nL, input_mos.ns) -- Needed for UDP
local periods = rat.gcd(input_mos.nL, input_mos.ns) -- Needed for UDP
local number_of_modes = #mos_modes
 
-- This is for entering multiple columns of info, if a single column of mode names isn't enough
-- For n column headers, the number of entries must match the number of modes times the number of headers
-- or else column data won't be added
local column_headers_unparsed = frame.args['Column Headers']
local column_headers = p.parse_entries(column_headers_unparsed)
local column_entries_unparsed = frame.args['Column Entries']
local column_entries = p.parse_entries(column_entries_unparsed)
 
-- To determine whether to add additional columns, determine whether the number of entries
-- and the number of columns are nonzero, and if so, determine whether the number of entries
-- divided by the number of columns exactly matches the number of modes
local add_columns = number_of_columns ~= 0 and number_of_entries ~= 0
if add_columns then
add_columns = add_columns and #column_entries / #column_headers == number_of_modes
end
-- Make a table with a column for the mode (as a string of L's and s's) and UDP
-- Make a table with a column for the mode (as a string of L's and s's) and UDP
Line 89: Line 105:
-- If there are mode names (if the mode names array is not nil), then add a column for mode names
-- If there are mode names (if the mode names array is not nil), then add a column for mode names
if #mode_names == number_of_modes then
if #mode_names == #mos_modes then
result = result .. "! Mode names\n"
result = result .. "! Mode names\n"
end
-- Add columns
-- If mode names and columns are used, mode names come first
if add_columns then
for i = 1, #column_headers do
result = result .. "! " .. column_headers[i] .. "\n"
end
end
end
Line 111: Line 135:
-- Add the mode's name, if given
-- Add the mode's name, if given
if #mode_names == number_of_modes then
if #mode_names == #mos_modes  then
result = result .. "|" .. mode_names[i] .. "\n"
result = result .. "|" .. mode_names[i] .. "\n"
end
-- Add columns if given
if add_columns then
for j = 1, #column_headers do
result = result .. "|" .. column_entries[i + j - 1] .. "\n"
end
end
end
end
end