Skip to content

Instantly share code, notes, and snippets.

@aelobdog
Created August 5, 2021 12:47
Show Gist options
  • Save aelobdog/a2b1c5a3c47bdcad84afd0c7bf06f577 to your computer and use it in GitHub Desktop.
Save aelobdog/a2b1c5a3c47bdcad84afd0c7bf06f577 to your computer and use it in GitHub Desktop.
my first plugin for lite-xl : displays the text "hello, world!" on the top right of the screen
-- mod-version:1 -- lite-xl 1.16
-- NAME : helloworld
-- GOAL : to render some text to the top right corner of the editor.
-- import the lite-xl "core" package
local core = require "core"
local command = require "core.command"
local style = require "core.style"
local config = require "core.config"
-- colors are nothing but rgb values put inside of '{ }'
-- you can add the the configuration options to the
-- config 'module' (I'm not a lua dev so my terminology
-- may not be correct, but I hope you get the point)
config.mycolor = {200, 140, 220}
-- let's create a function to return the coordinates of our text message.
-- while we're at it, let's add the function to the `config` module
-- we'll take the message we want to display as the argument to the
-- function to determine the x and y coordinates of the text.
function config.mycoords(message)
local x = core.root_view.size.x - style.code_font:get_width(message.." ")
local y = style.code_font:get_size() / 2;
return x, y
end
-- Let's now get to actually drawing the text.
-- The first thing we'll do is override the `parent` draw function.
-- So we get the parent draw function as so
local draw = core.root_view.draw
-- Now we overload it by redefining the function in the core module.
core.root_view.draw = function(...)
-- we call the parent's function so as to keep the editor
-- working ... obviously we can't not draw the rest of the stuff!
-- To call the parent draw function, we call the `draw` function \
-- that we stored locally before.
draw(...)
-- for now, we'll hardcode our message as "hello, world!"
local message = "hello, world!"
-- we'll add an option to toggle the message on and off. let's use a boolean
-- variable to keep track of whether we want to display the message or not.
if config.show_my_message then
-- let's get the coordinates for our text
local x, y = config.mycoords(message)
-- let's finally draw the text to the window !
renderer.draw_text(style.code_font, message, x, y, config.mycolor)
end
end
-- To allow the user to turn the message on and off,
-- we'll write a function to flip our "show" boolean variable.
local function toggle_helloworld()
config.show_my_message = not config.show_my_message
end
-- finally, we need to add the toggle function to the command
-- list so that we can call it from the C-S-p command panel
command.add(nil, {
["helloworld:toggle"] = toggle_helloworld
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment