summaryrefslogtreecommitdiff
path: root/script/filename.lua
blob: a28b004c90a90a17c6b7851dfe1af4f6d46d1e40 (plain)
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
local platform = require 'bee.platform'
local config = require 'config'
local m = {}

local TrueName = {}

function m.getFileName(path)
    local name = path:string()
    if platform.OS == 'Windows' then
        local lname = name:lower()
        TrueName[lname] = name
        return lname
    else
        return name
    end
end

function m.getTrueName(name)
    return TrueName[name] or name
end

local function split(str, sep)
    local t = {}
    for s in str:gmatch('[^' .. sep .. ']+') do
        t[#t+1] = s
    end
    return t
end

function m.similarity(a, b)
    local ta = split(a, '/\\')
    local tb = split(b, '/\\')
    for i = 1, #ta do
        if ta[i] ~= tb[i] then
            return i - 1
        end
    end
    return #ta
end

function m.isLuaFile(path)
    local pathStr = path:string()
    for k, v in pairs(config.other.associations) do
        if v == 'lua' then
            k = k:gsub('^%*', '')
            if m.fileNameEq(pathStr:sub(-#k), k) then
                return true
            end
        end
    end
    if m.fileNameEq(pathStr:sub(-4), '.lua') then
        return true
    end
    return false
end

function m.fileNameEq(a, b)
    if platform.OS == 'Windows' then
        return a:lower() == b:lower()
    else
        return a == b
    end
end

return m