Reno is the second iteration of the AWMTK-powered AwesomeWM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.8 KiB

2 years ago
2 years ago
  1. -- This file is part of Reno desktop.
  2. --
  3. -- Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  4. --
  5. -- Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. --
  7. -- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>.
  8. -- Various utilitiy parsers
  9. local parsers = {}
  10. local function split_strings(text)
  11. -- probably the cleanest function to split by strings i've written
  12. local split = {}
  13. while text:find("\"") do
  14. local strstart = text:find("\"")
  15. while text:sub(strstart-1,strstart-1) == "\\" do
  16. strstart = text:find("\"",strstart+1)
  17. end
  18. local strend = text:find("\"",strstart+1)
  19. while text:sub(strend-1,strend-1) == "\\" do
  20. strend = text:find("\"",strend+1)
  21. end
  22. if not strend then
  23. return nil, "String not closed at "..strstart
  24. end
  25. local before_string = text:sub(1,strstart-1)
  26. local string = text:sub(strstart,strend):gsub("\\\"","\"")
  27. text = text:sub(strend+1,-1)
  28. table.insert(split,before_string)
  29. table.insert(split,string)
  30. end
  31. table.insert(split,text)
  32. return split
  33. end
  34. parsers.fast_split_yaml = function(cfgtext)
  35. -- Fast yaml splitter - incomplete parsing, only first layer is parsed
  36. -- Used within timers to find objects while decreasing CPU usage
  37. local items = {}
  38. local replacements = 1
  39. cfgtext = cfgtext:gsub("^%s*","")
  40. while replacements > 0 do
  41. cfgtext,replacements = cfgtext:gsub("^(.-\n)(%S+)",function(struct,n)
  42. table.insert(items,struct)
  43. return ""..n
  44. end)
  45. end
  46. table.insert(items,cfgtext)
  47. return items
  48. end
  49. parsers.yaml_pseudo = function(cfgtext)
  50. -- Somewhat yaml-like structure used by pactl
  51. local struct = {}
  52. local lines = {}
  53. cfgtext:gsub("(%s*)([^\n]*)",function(spacing,line)
  54. table.insert(lines,
  55. {
  56. spacing:len(),
  57. -- key
  58. line:match("^([^:=]-)%s*[:=]") or line,
  59. -- value
  60. line:match(":%s*(.-)%s*$") or
  61. line:match("=%s*(.-)%s*$")
  62. }
  63. )
  64. end)
  65. local history = {struct}
  66. local spacing_width = 0
  67. for k,v in pairs(lines) do
  68. if v[1] > spacing_width then
  69. history[#history][lines[k-1][2]] = {
  70. [lines[k-1][2]] = lines[k-1][3]
  71. }
  72. history[#history+1] = history[#history][lines[k-1][2]]
  73. elseif v[1] < spacing_width then
  74. history[#history] = nil
  75. end
  76. if v[3] and v[3]:match("^%s*\".*\"%s*$") then
  77. history[#history][v[2]] = v[3]:match("^%s*\"(.*)\"%s*$")
  78. else
  79. history[#history][v[2]] = v[3]
  80. end
  81. spacing_width = v[1]
  82. end
  83. return struct
  84. end
  85. parsers.conf = function(cfgtext)
  86. -- Conf style parser (not exactly TOML)
  87. cfgtext = cfgtext:gsub("#[^\n]*","")
  88. local split_by_strings,err = split_strings(cfgtext)
  89. if not split_by_strings then
  90. error(err)
  91. end
  92. local struct = {global = {}}
  93. local block = "global"
  94. local last_string_key = nil
  95. local err
  96. for k,v in pairs(split_by_strings) do
  97. if not v:match("^\".*\"$") then
  98. v:gsub("[^\n]*",function(line)
  99. -- Nothing
  100. if line:match("^%s*$") then
  101. return
  102. end
  103. -- Namespace block
  104. if line:match("^%s*%[[^%]]-%]%s*$") then
  105. block = line:match("%[([^%]]-)%]")
  106. struct[block] = {}
  107. return
  108. end
  109. -- String/Multiline string assignment
  110. if line:match("^%s*[^=]-%s*=%s*$") then
  111. last_string_key = line:match("^%s*([^=]-)%s*=%s*$")
  112. return
  113. end
  114. -- Number/boolean assignment
  115. local key,value = line:match("^%s*([^=]-)%s*=%s*(.-)%s*$")
  116. -- number
  117. if value:match("^[%d%.]*$") then
  118. value = tonumber(value)
  119. -- bool
  120. elseif (value:lower() == "yes") or (value:lower() == "no") then
  121. value = (value:lower() == "yes")
  122. end
  123. struct[block][key] = value
  124. end)
  125. else
  126. struct[block][last_string_key] = v:sub(2,-2)
  127. end
  128. end
  129. return struct
  130. end
  131. return parsers