aboutsummaryrefslogtreecommitdiff
path: root/plugins/typingspeed.lua
diff options
context:
space:
mode:
authorjgmdev <jgmdev@gmail.com>2022-05-24 19:29:50 -0400
committerjgmdev <jgmdev@gmail.com>2022-05-24 19:29:50 -0400
commit35e947d1933613bb0b5a1488bf0fa4587f98ef7d (patch)
tree07d2515f44db03b1e865f6aef6adfd12b118890b /plugins/typingspeed.lua
parentc1f3671e2a8defbc67d1e77c72d5866f2825cdb5 (diff)
downloadlite-xl-plugins-35e947d1933613bb0b5a1488bf0fa4587f98ef7d.tar.gz
lite-xl-plugins-35e947d1933613bb0b5a1488bf0fa4587f98ef7d.zip
added config_spec and other plugin compatibility fixes.
Diffstat (limited to 'plugins/typingspeed.lua')
-rw-r--r--plugins/typingspeed.lua135
1 files changed, 71 insertions, 64 deletions
diff --git a/plugins/typingspeed.lua b/plugins/typingspeed.lua
index 62e44ef..d639252 100644
--- a/plugins/typingspeed.lua
+++ b/plugins/typingspeed.lua
@@ -6,17 +6,38 @@ local common = require "core.common"
local config = require "core.config"
local DocView = require "core.docview"
-if common["merge"] then
- config.plugins.typingspeed = common.merge({
- -- characters that should be counted as word boundary
- word_boundaries = "[%p%s]",
- }, config.plugins.keystats)
-else
- config.plugins.typingspeed = {
- -- characters that should be counted as word boundary
- word_boundaries = "[%p%s]",
- }
-end
+config.plugins.typingspeed = common.merge({
+ enabled = true,
+ -- characters that should be counted as word boundary
+ word_boundaries = "[%p%s]",
+ -- The config specification used by the settings gui
+ config_spec = {
+ name = "Typing Speed",
+ {
+ label = "Enabled",
+ description = "Show or hide the typing speed from the status bar.",
+ path = "enabled",
+ type = "toggle",
+ default = true,
+ on_apply = function(enabled)
+ core.add_thread(function()
+ if enabled then
+ core.status_view:get_item("typing-speed:stats"):show()
+ else
+ core.status_view:get_item("typing-speed:stats"):hide()
+ end
+ end)
+ end
+ },
+ {
+ label = "Word Boundaries",
+ description = "Lua pattern that matches characters to separate words.",
+ path = "word_boundaries",
+ type = "string",
+ default = "[%p%s]"
+ }
+ }
+}, config.plugins.typingspeed)
local chars = 0
local chars_last = 0
@@ -29,65 +50,51 @@ local wpm = 0
core.add_thread(function()
while true do
- local t = os.date("*t")
- if t.sec <= time_last then
- words_last = words
- words = 0
- chars_last = chars
- chars = 0
- time_last = t.sec
- end
- wpm = words_last * (1-(t.sec)/60) + words
- cpm = chars_last * (1-(t.sec)/60) + chars
+ if config.plugins.typingspeed.enabled then
+ local t = os.date("*t")
+ if t.sec <= time_last then
+ words_last = words
+ words = 0
+ chars_last = chars
+ chars = 0
+ time_last = t.sec
+ end
+ wpm = words_last * (1-(t.sec)/60) + words
+ cpm = chars_last * (1-(t.sec)/60) + chars
+ end
coroutine.yield(1)
end
end)
local on_text_input = DocView.on_text_input
function DocView:on_text_input(text, idx)
- chars = chars + 1
- if string.find(text, config.plugins.typingspeed.word_boundaries) then
- if started_word then
- words = words + 1
- started_word = false
- end
- else
- started_word = true
- end
+ if config.plugins.typingspeed.enabled then
+ chars = chars + 1
+ if string.find(text, config.plugins.typingspeed.word_boundaries) then
+ if started_word then
+ words = words + 1
+ started_word = false
+ end
+ else
+ started_word = true
+ end
+ end
on_text_input(self, text, idx)
end
-if core.status_view["add_item"] then
- core.status_view:add_item(
- function()
- return core.active_view and getmetatable(core.active_view) == DocView
- end,
- "keystats:stats",
- core.status_view.Item.RIGHT,
- function()
- return {
- style.text,
- string.format("%.0f CPM / %.0f WPM", cpm, wpm)
- }
- end,
- nil,
- 1,
- "characters / words per minute"
- ).separator = core.status_view.separator2
-else
- local get_items = core.status_view.get_items
- function core.status_view:get_items()
- local left, right = get_items(self)
-
- local t = {
- style.text, string.format("%.0f CPM / %.0f WPM", cpm, wpm),
- style.dim, self.separator2
- }
-
- for i, item in ipairs(t) do
- table.insert(right, i, item)
- end
-
- return left, right
- end
-end
+core.status_view:add_item(
+ function()
+ return core.active_view and getmetatable(core.active_view) == DocView
+ end,
+ "typing-speed:stats",
+ core.status_view.Item.RIGHT,
+ function()
+ return {
+ style.text,
+ string.format("%.0f CPM / %.0f WPM", cpm, wpm)
+ }
+ end,
+ nil,
+ 1,
+ "characters / words per minute"
+).separator = core.status_view.separator2