--@action CSV files as a table --@version 1.0.0 --@author TheFile.Ninja --@id csv-table --@description Shows .csv and .tsv files as a table in the preview pane, a column for each field, numbers sorted as numbers. Put it in Scripts\Preview\. --@tags preview, data --@requires none -- Commas, semicolons, tabs or bars, whichever the first line has most of; -- quoted fields may hold any of them, line breaks and "" too. The table -- shows the first 1 000 rows that fit in the file's first 256 KB. local MAX_ROWS = 1000 local SEPARATORS = { [","] = "commas", [";"] = "semicolons", ["\t"] = "tabs", ["|"] = "bars" } local function separator(text, ext) if ext == "tsv" then return "\t" end local first, best, most = text:match("^[^\r\n]*"), ",", 0 for sep in pairs(SEPARATORS) do local _, n = first:gsub(sep, "") if n > most then best, most = sep, n end end return best end -- Rows of fields; true as well when there was more than max. local function parse(text, sep, max) local rows, row, buf = {}, {}, {} local special = "[" .. sep .. '"\r\n]' local i, n = 1, #text local function field() row[#row + 1] = table.concat(buf); buf = {} end while i <= n do local j = text:find(special, i) if not j then buf[#buf + 1] = text:sub(i); break end buf[#buf + 1] = text:sub(i, j - 1) local c = text:sub(j, j) if c == '"' then local k = j + 1 while true do -- a quoted part ends at a lone quote ("" is one quote) local q = text:find('"', k, true) if not q then buf[#buf + 1] = text:sub(k); i = n + 1; break end buf[#buf + 1] = text:sub(k, q - 1) if text:sub(q + 1, q + 1) == '"' then buf[#buf + 1] = '"' k = q + 2 else i = q + 1 break end end elseif c == sep then field() i = j + 1 else field() rows[#rows + 1] = row row = {} if c == "\r" and text:sub(j + 1, j + 1) == "\n" then j = j + 1 end i = j + 1 if #rows > max then return rows, true end end end if #buf > 0 or #row > 0 then field() rows[#rows + 1] = row end return rows, false end -- "1 234,5" and "1234.5" alike; nil when it is no number. local function number(s, sep) s = s:gsub("^%s+", ""):gsub("%s+$", "") if s == "" then return nil end return tonumber(s) or (sep ~= "," and tonumber((s:gsub("\194\160", ""):gsub("%s", ""):gsub(",", "."))) or nil) end preview.register{ id = "csv_table", title = "CSV table", extensions = { "csv", "tsv" }, wants = { "file_head" }, on_error = "fallback", generate = function(ctx) local text = ctx.head_text if not text or text == "" then return nil end text = text:gsub("^\239\187\191", "") -- a UTF-8 byte order mark local sep = separator(text, ctx.ext) local rows, more = parse(text, sep, MAX_ROWS + 1) if ctx.head_truncated and not more then table.remove(rows) end -- the last row may be cut local header = table.remove(rows, 1) or {} while #rows > MAX_ROWS do table.remove(rows) more = true end local width = #header for _, r in ipairs(rows) do width = math.max(width, #r) end -- A column whose every value is a number sorts as numbers. local columns = {} for c = 1, width do local all_numbers, any = true, false for _, r in ipairs(rows) do local v = r[c] if v and v ~= "" then any = true if not number(v, sep) then all_numbers = false break end end end local title = header[c] ~= nil and header[c] ~= "" and header[c] or ("Column " .. c) if any and all_numbers then columns[c] = { title, type = "number" } for _, r in ipairs(rows) do if r[c] then r[c] = number(r[c], sep) end end else columns[c] = title end end return { { text = #rows .. (more and "+" or "") .. (#rows == 1 and " row · " or " rows · ") .. width .. " columns · separated by " .. SEPARATORS[sep], muted = true, small = true }, { table = { columns = columns, rows = rows } }, } end, }