2

I created a string called fonts which stores a list of all of the fonts installed on the system, each separated by a newline:

local font = io.popen("fc-list | cut -d ' ' -f 2- | cut -d : -f 1 | cut -d , -f 1 | sort | uniq")

if font == nil then return end

local fonts = font:read("*a")

print(fonts)

font:close()

The output of printing it looks something like this:

Latin Modern Mono
Latin Modern Mono Caps
Latin Modern Mono Light

etc. I want to store it in a table instead. How can I get something like this:

local fonts = {
    "Latin Modern Mono",
    "Latin Modern Mono Caps",
    "Latin Modern Mono light"
}
Amarakon
  • 173
  • 5

1 Answers1

1

Thanks to @timrau, here is my final answer. The table is called fonttbl.

local font = io.popen([[
    fc-list | cut -d " " -f 2- | cut -d : -f 1 | cut -d , -f 1 | sort | uniq |
        sed -z "$ s/\n$//"
]])

if font == nil then return end

local fontstr = font:read("*a")

local pos,fonttbl = 0,{}
for st,sp in function() return string.find(fontstr, "\n", pos, true) end do
    table.insert(fonttbl, string.sub(fontstr, pos, st - 1))
    pos = sp + 1
end
table.insert(fonttbl, string.sub(fontstr, pos))

font:close()
Amarakon
  • 173
  • 5