summaryrefslogtreecommitdiff
path: root/.config/micro/plug/wc/wc.lua
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@simplelittledream.com>2022-07-16 18:13:16 -0400
committerJacob McDonnell <jacob@simplelittledream.com>2022-07-16 18:13:16 -0400
commit8fad9a5ecddc88d57a531e4b0084544984f23d25 (patch)
tree84954bc8219942aa56bc899330ccd0007bbe0ef0 /.config/micro/plug/wc/wc.lua
parent2887af7fcfb4d618dd13cf66ec2fbdbd84c7527c (diff)
Added profile and other missing configs
Diffstat (limited to '.config/micro/plug/wc/wc.lua')
-rw-r--r--.config/micro/plug/wc/wc.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/.config/micro/plug/wc/wc.lua b/.config/micro/plug/wc/wc.lua
new file mode 100644
index 0000000..0e13677
--- /dev/null
+++ b/.config/micro/plug/wc/wc.lua
@@ -0,0 +1,37 @@
+VERSION = "1.2.1"
+
+local micro = import("micro")
+local config = import("micro/config")
+local util = import("micro/util")
+local utf8 = import("unicode/utf8")
+
+function init()
+ config.MakeCommand("wc", wordCount, config.NoComplete)
+ config.AddRuntimeFile("wc", config.RTHelp, "help/wc.md")
+ config.TryBindKey("F5", "lua:wc.wordCount", false)
+end
+
+function wordCount(bp)
+ -- Buffer of selection/whole document
+ local buffer
+ --Get active cursor (to get selection)
+ local cursor = bp.Buf:GetActiveCursor()
+ --If cursor exists and there is selection, convert selection byte[] to string
+ if cursor and cursor:HasSelection() then
+ buffer = util.String(cursor:GetSelection())
+ else
+ --no selection, convert whole buffer byte[] to string
+ buffer = util.String(bp.Buf:Bytes())
+ end
+ --length of the buffer/selection (string), utf8 friendly
+ charCount = utf8.RuneCountInString(buffer)
+ --Get word/line count using gsub's number of substitutions
+ -- number of substitutions, pattern: %S+ (more than one non-whitespace characters)
+ local _ , wordCount = buffer:gsub("%S+","")
+ -- number of substitutions, pattern: \n (number of newline characters)
+ local _, lineCount = buffer:gsub("\n", "")
+ --add one to line count (since we're counting separators not lines above)
+ lineCount = lineCount + 1
+ --display the message
+ micro.InfoBar():Message("Lines:" .. lineCount .. " Words:"..wordCount.." Characters:"..charCount)
+end