From 184eecba877be1950e1d86f9cbb8d80c169f8c01 Mon Sep 17 00:00:00 2001 From: Olivier Roques Date: Fri, 25 Dec 2020 14:07:40 +0100 Subject: [PATCH] Add wordcount part --- lua/hardline.lua | 3 +- lua/hardline/parts/git.lua | 3 +- lua/hardline/parts/whitespace.lua | 11 ++++--- lua/hardline/parts/wordcount.lua | 54 ++++++++++++++++++++++++++++--- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/lua/hardline.lua b/lua/hardline.lua index b9c8eb7..a0b7f14 100644 --- a/lua/hardline.lua +++ b/lua/hardline.lua @@ -15,7 +15,8 @@ M.options = { {class = 'mode', item = require('hardline.parts.mode').get_item}, {class = 'high', item = require('hardline.parts.git').get_item}, {class = 'med', item = require('hardline.parts.filename').get_item}, - {class = 'med', item ='%='}, + {class = 'med', item = '%='}, + {class = 'med', item = require('hardline.parts.wordcount').get_item}, {class = 'error', item = require('hardline.parts.lsp').get_error}, {class = 'warning', item = require('hardline.parts.lsp').get_warning}, {class = 'warning', item = require('hardline.parts.whitespace').get_item}, diff --git a/lua/hardline/parts/git.lua b/lua/hardline/parts/git.lua index 8cb1dfb..62abcfa 100644 --- a/lua/hardline/parts/git.lua +++ b/lua/hardline/parts/git.lua @@ -17,7 +17,8 @@ local function get_branch() end local function get_item() - return table.concat({' ', get_hunks(), get_branch(), ' '}) + local item = table.concat({' ', get_hunks(), get_branch(), ' '}) + return item == ' ' and '' or item end return { diff --git a/lua/hardline/parts/whitespace.lua b/lua/hardline/parts/whitespace.lua index 1a45cfd..1b2f812 100644 --- a/lua/hardline/parts/whitespace.lua +++ b/lua/hardline/parts/whitespace.lua @@ -1,6 +1,7 @@ local cmd, fn, vim = vim.cmd, vim.fn, vim -local bo = vim.bo +local b, bo = vim.b, vim.bo local enabled = false +local cache = '' local options = { c_langs = {'arduino', 'c', 'cpp', 'cuda', 'go', 'javascript', 'ld', 'php'}, max_lines = 5000, @@ -49,13 +50,14 @@ local function get_item() if not enabled then cmd 'augroup hardline_whitespace' cmd 'autocmd!' - cmd 'autocmd CursorHold, BufWritePost * unlet! b:hardline_whitespace' + cmd 'autocmd CursorHold,BufWritePost * unlet! b:hardline_whitespace' cmd 'augroup end' enabled = true end if bo.readonly or not bo.modifiable then return '' end if fn.line('$') > options.max_lines then return '' end - if fn.exists('b:hardline_whitespace') ~= 0 then return '' end + if fn.exists('b:hardline_whitespace') ~= 0 then return cache end + b.hardline_whitespace = 1 local item = table.concat({ ' ', check_trailing(), @@ -64,7 +66,8 @@ local function get_item() check_conflict(), ' ', }) - return item == ' ' and '' or item + cache = item == ' ' and '' or item + return cache end return { diff --git a/lua/hardline/parts/wordcount.lua b/lua/hardline/parts/wordcount.lua index ce84ef3..7493fac 100644 --- a/lua/hardline/parts/wordcount.lua +++ b/lua/hardline/parts/wordcount.lua @@ -1,8 +1,52 @@ -local M = {} +local cmd, fn, vim = vim.cmd, vim.fn, vim +local b, bo = vim.b, vim.bo +local enabled = false +local cache = '' +local options = { + filetypes = { + 'asciidoc', + 'help', + 'mail', + 'markdown', + 'nroff', + 'org', + 'rst', + 'plaintex', + 'tex', + 'text', + }, + max_lines = 5000, +} -function M.get_item() - return table.concat({ - }) +local function in_visual() + return vim.tbl_contains({'v', 'V', '', 's', 'S', ''}, fn.mode()) end -return M +local function get_wordcount() + local query = in_visual() and 'visual_words' or 'words' + local wordcount = fn.wordcount()[query] + return string.format('%d words', wordcount) +end + +local function get_item() + if not enabled then + cmd 'augroup hardline_wordcount' + cmd 'autocmd!' + cmd 'autocmd CursorHold,BufWritePost * unlet! b:hardline_wordcount' + cmd 'augroup end' + enabled = true + end + if not vim.tbl_contains(options.filetypes, bo.filetype) then return '' end + if fn.line('$') > options.max_lines then return '' end + if fn.exists('b:hardline_wordcount') ~= 0 and not in_visual() then + return cache + end + b.hardline_wordcount = 1 + local item = table.concat({' ', get_wordcount(), ' '}) + cache = item == ' ' and '' or item + return cache +end + +return { + get_item = get_item, +}