~tmpod/memusage-lite

7aa3d9a31dcddfd6a3a54dfe06f5eb98152a7b0f — Tmpod 2 years ago 1ef7020
Simplified the config

Instead of using a table on a seperate module,
configs are now loaded from the main config table
(`core.config`) and have a flat structure, having
each field's name prefixed with `memusage_`
2 files changed, 17 insertions(+), 34 deletions(-)

M README.md
M memusage.lua
M README.md => README.md +7 -7
@@ 29,8 29,9 @@ You can also configure it, as explained in the next section.
## Configurations

This plugin has three configuration fields.
You can set these configurations by creating a `memusage.lua` file in your `user` lite directory.
This file must return a table containing some or all the following fields:
You can set these configurations by editing your `init.lua` file in the `data/user` lite directory and modifying the `core.config` table.
Each field name should be prefixed with `memusage_`, for example, `memusage_color`.
Here's a description of each valid configuration field:

| Name | Type | Description |
| :--- | ---- | ----------- |


@@ 40,13 41,12 @@ This file must return a table containing some or all the following fields:

Example:
```lua
local config = {}
local config = require "core.config"

-- Won't start displaying when lite is opened
config.active = false
config.memusage_active = false
-- Text will be of a purplish-pinkish color
config.color = {191, 90, 236}

return config
config.memusage_color = {191, 90, 236}
```



M memusage.lua => memusage.lua +10 -27
@@ 8,32 8,15 @@
local core = require "core"
local command = require "core.command"
local style = require "core.style"
local config = require "core.config"

-- Configurations --
local function default_coords()
  return core.root_view.size.x - 90, 4
end

local default_config = {}
-- Defaults
-- Table containing RGBA int color values for the text.
default_config.color = { 255, 0, 255 }
config.memusage_color = { 255, 0, 255 }
-- Function taking no arguments that returns two ints for the text's x and y position.
default_config.coords = default_coords
-- Boolean that defines if the memusage should be active on start up.
default_config.active = true

local ok, config = pcall(require, "user.memusage")
if not ok then
  core.log_quiet("Couldn't load memusage config, falling back to default.")
  config = default_config
else
  -- Make sure the config is all properly populated
  config.color  = config.color  or default_config.text_color
  config.coords = config.coords or default_config.coords
  -- Since this is a boolean value we can't just use `or`
  if config.active == nil then
    config.active = default_config.active
  end
function config.memusage_coords()
  return core.root_view.size.x - 90, 4
end

-- Logic --


@@ 44,17 27,17 @@ local draw = core.root_view.draw
core.root_view.draw = function(...)
  draw(...)
  -- Not entirely sure if this if branch is expensive enough that it should be avoided...
  if config.active then
  if config.memusage_active then
    local str = string.format("%.2f MB", collectgarbage("count") / 1024)
    local x, y = config.coords()
    renderer.draw_text(style.font, str, x, y, config.color)
    local x, y = config.memusage_coords()
    renderer.draw_text(style.font, str, x, y, config.memusage_color)
  end
end

-- And add a command for toggling too
local function toggle_memusage()
  config.active = not config.active
  local state = config.active and "ON" or "OFF"
  config.memusage_active = not config.memusage_active
  local state = config.memusage_active and "ON" or "OFF"
  core.log("Toggled memusage display " .. state)
end