Add wordcount part

This commit is contained in:
Olivier Roques
2020-12-25 14:07:40 +01:00
parent 7d868514cd
commit 184eecba87
4 changed files with 60 additions and 11 deletions

View File

@@ -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},

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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,
}