My configuration files for Debian/Ubuntu applications
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

157 linhas
5.1 KiB

  1. --[[
  2. Licensed under GNU General Public License v2
  3. * (c) 2018, Uli Schlacter
  4. * (c) 2018, Otto Modinos
  5. * (c) 2013, Luca CPZ
  6. --]]
  7. local helpers = require("lain.helpers")
  8. local Gio = require("lgi").Gio
  9. local focused = require("awful.screen").focused
  10. local wibox = require("wibox")
  11. local naughty = require("naughty")
  12. local gears = require("gears")
  13. local math = math
  14. local string = string
  15. local tconcat = table.concat
  16. local type = type
  17. local query_size = Gio.FILE_ATTRIBUTE_FILESYSTEM_SIZE
  18. local query_free = Gio.FILE_ATTRIBUTE_FILESYSTEM_FREE
  19. local query_used = Gio.FILE_ATTRIBUTE_FILESYSTEM_USED
  20. local query = query_size .. "," .. query_free .. "," .. query_used
  21. -- File systems info
  22. -- lain.widget.fs
  23. local function factory(args)
  24. args = args or {}
  25. local fs = {
  26. widget = args.widget or wibox.widget.textbox(),
  27. units = {
  28. [1] = "Kb", [2] = "Mb", [3] = "Gb",
  29. [4] = "Tb", [5] = "Pb", [6] = "Eb",
  30. [7] = "Zb", [8] = "Yb"
  31. }
  32. }
  33. function fs.hide()
  34. if not fs.notification then return end
  35. naughty.destroy(fs.notification)
  36. fs.notification = nil
  37. end
  38. function fs.show(seconds, scr)
  39. fs.hide()
  40. fs.update(function()
  41. fs.notification_preset.screen = fs.followtag and focused() or scr or 1
  42. fs.notification = naughty.notify {
  43. preset = fs.notification_preset,
  44. timeout = type(seconds) == "number" and seconds or 5
  45. }
  46. end)
  47. end
  48. local timeout = args.timeout or 600
  49. local partition = args.partition
  50. local threshold = args.threshold or 99
  51. local showpopup = args.showpopup or "on"
  52. local settings = args.settings or function() end
  53. fs.followtag = args.followtag or false
  54. fs.notification_preset = args.notification_preset
  55. if not fs.notification_preset then
  56. fs.notification_preset = {
  57. font = "Monospace 10",
  58. fg = "#FFFFFF",
  59. bg = "#000000"
  60. }
  61. end
  62. local function update_synced()
  63. local pathlen = 10
  64. fs_now = {}
  65. local notifypaths = {}
  66. for _, mount in ipairs(Gio.unix_mounts_get()) do
  67. local path = Gio.unix_mount_get_mount_path(mount)
  68. local root = Gio.File.new_for_path(path)
  69. local info = root:query_filesystem_info(query)
  70. if info then
  71. local size = info:get_attribute_uint64(query_size)
  72. local used = info:get_attribute_uint64(query_used)
  73. local free = info:get_attribute_uint64(query_free)
  74. if size > 0 then
  75. local units = math.floor(math.log(size)/math.log(1024))
  76. fs_now[path] = {
  77. units = fs.units[units],
  78. percentage = math.floor(100 * used / size), -- used percentage
  79. size = size / math.pow(1024, units),
  80. used = used / math.pow(1024, units),
  81. free = free / math.pow(1024, units)
  82. }
  83. if fs_now[path].percentage > 0 then -- don't notify unused file systems
  84. notifypaths[#notifypaths+1] = path
  85. if #path > pathlen then
  86. pathlen = #path
  87. end
  88. end
  89. end
  90. end
  91. end
  92. widget = fs.widget
  93. settings()
  94. if partition and fs_now[partition] and fs_now[partition].percentage >= threshold then
  95. if not helpers.get_map(partition) then
  96. naughty.notify {
  97. preset = naughty.config.presets.critical,
  98. title = "Warning",
  99. text = string.format("%s is above %d%% (%d%%)", partition, threshold, fs_now[partition].percentage)
  100. }
  101. helpers.set_map(partition, true)
  102. else
  103. helpers.set_map(partition, false)
  104. end
  105. end
  106. local fmt = "%-" .. tostring(pathlen) .. "s %4s\t%6s\t%6s\n"
  107. local notifytable = { [1] = string.format(fmt, "path", "used", "free", "size") }
  108. fmt = "\n%-" .. tostring(pathlen) .. "s %3s%%\t%6.2f\t%6.2f %s"
  109. for _, path in ipairs(notifypaths) do
  110. notifytable[#notifytable+1] = string.format(fmt, path, fs_now[path].percentage, fs_now[path].free, fs_now[path].size, fs_now[path].units)
  111. end
  112. fs.notification_preset.text = tconcat(notifytable)
  113. end
  114. function fs.update(callback)
  115. Gio.Async.start(gears.protected_call.call)(function()
  116. update_synced()
  117. if type(callback) == "function" and callback then
  118. callback()
  119. end
  120. end)
  121. end
  122. if showpopup == "on" then
  123. fs.widget:connect_signal('mouse::enter', function () fs.show(0) end)
  124. fs.widget:connect_signal('mouse::leave', function () fs.hide() end)
  125. end
  126. helpers.newtimer(partition or "fs", timeout, fs.update)
  127. return fs
  128. end
  129. return factory