From 0dd0a23cac1adeaa4dbfc15b4a5cabe44ed59401 Mon Sep 17 00:00:00 2001 From: Shadman Date: Thu, 18 Feb 2021 00:29:50 +0600 Subject: [PATCH] feat: add colors to signify (#61) Co-authored-by: hoob3rt --- README.md | 11 ++++ doc/lualine.txt | 21 +++++- doc/tags | 2 - lua/lualine/components/signify.lua | 100 +++++++++++++++++++++-------- lua/lualine/utils/utils.lua | 6 ++ 5 files changed, 107 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 258f923..3e80b85 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,17 @@ Default options act as default for all components - fileformat - icons_enabled (true)\ Whether to displays icon before component +- signify + - colored (true)\ + Whether to show colors. Colors are automaticaly + extracted from colorscheme . If you want to change + any of those you can use options given below. + - color_added ('#90ee90')\ + Foreground color of added section + - color_modified ('#f0e130')\ + Foreground color of modified section + - color_removed ('#ff0038')\ + Foreground color of removed section **Example:** ```lua diff --git a/doc/lualine.txt b/doc/lualine.txt index c7440ee..1f11d04 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -1,5 +1,4 @@ -*lualine.txt* - *lualine* *lualine.nvim* +lualine.txt *lualine-nvim* *lualine* Author: hoob3rt (https://github.com/hoob3rt) License: MIT License @@ -279,7 +278,23 @@ List of options are given below. • fileformat~ • icons_enabled (true) - Whether to displays icon before component + Whether to displays icon before component. Colors + are automaticaly extracted from colorscheme . If you + want to change any of those you can use options given below. + + + • signify~ + • colored (true) + Whether to show colors + + • color_added ('#90ee90') + Foreground color of added section + + • color_modified ('#f0e130') + Foreground color of modified section + + • color_removed ('#ff0038') + Foreground color of removed section Example:~ > diff --git a/doc/tags b/doc/tags index fd0447d..460e8d5 100644 --- a/doc/tags +++ b/doc/tags @@ -18,6 +18,4 @@ lualine-setting_a_theme lualine.txt /*lualine-setting_a_theme* lualine-starting_lualine lualine.txt /*lualine-starting_lualine* lualine-usage_and_customization lualine.txt /*lualine-usage_and_customization* lualine-vim-plug lualine.txt /*lualine-vim-plug* -lualine.nvim lualine.txt /*lualine.nvim* -lualine.txt lualine.txt /*lualine.txt* lualline-contents lualine.txt /*lualline-contents* diff --git a/lua/lualine/components/signify.lua b/lua/lualine/components/signify.lua index a1d4c8b..070f790 100644 --- a/lua/lualine/components/signify.lua +++ b/lua/lualine/components/signify.lua @@ -1,33 +1,77 @@ --- Copyright (c) 2020-2021 hoob3rt +-- Copyright (c) 2020-2021 shadmansaleh -- MIT license, see LICENSE for more details. -local function signify() - if vim.fn.exists('*sy#repo#get_stats') == 0 then return '' end - local added, modified, removed = unpack(vim.fn['sy#repo#get_stats']()) - if added == -1 then return '' end - local symbols = { - '+', - '-', - '~', - } - local result = {} - local data = { - added, - removed, - modified, - } - for range=1,3 do - if data[range] ~= nil and data[range] > 0 - then table.insert(result,symbols[range]..''..data[range]..' ') - end - end +local utils = require'lualine.utils.utils' +local highlight = require"lualine.highlight" - if result[1] ~= nil then - return table.concat(result, '') - else - return '' - end +local default_color_added = "#f0e130" +local default_color_removed = "#90ee90" +local default_color_modified = "#ff0038" + +local function signify(options) + if options.colored == nil then options.colored = true end + -- apply colors + if not options.color_added then + options.color_added = utils.extract_highlight_colors('diffAdded', 'foreground') or default_color_added + end + if not options.color_modified then + options.color_modified = utils.extract_highlight_colors('diffChanged', 'foreground') or default_color_modified + end + if not options.color_removed then + options.color_removed = utils.extract_highlight_colors('diffRemoved', 'foreground') or default_color_removed + end + + local highlights = {} + + -- create highlights and save highlight_name in highlights table + local function create_highlights() + highlights = { + highlight.create_component_highlight_group({fg = options.color_added}, 'signify_added', options), + highlight.create_component_highlight_group({fg = options.color_modified}, 'signify_modified', options), + highlight.create_component_highlight_group({fg = options.color_removed}, 'signify_removed', options), + } + end + + -- create highlights + if options.colored then + create_highlights() + utils.expand_set_theme(create_highlights) + options.custom_highlight = true + end + + -- Function that runs everytime statusline is updated + return function() + -- check if signify is available + if vim.fn.exists('*sy#repo#get_stats') == 0 then return '' end + local data = vim.fn['sy#repo#get_stats']() + if data[1] == -1 then return '' end + + local symbols = {'+', '~', '-'} + local colors = {} + if options.colored then + -- load the highlights and store them in colors table + for _, highlight_name in ipairs(highlights) do + table.insert(colors, highlight.component_format_highlight(highlight_name)) + end + end + + local result = {} + -- loop though data and load available sections in result table + for range=1,3 do + if data[range] ~= nil and data[range] > 0 then + if options.colored then + table.insert(result,colors[range]..symbols[range]..data[range]) + else + table.insert(result,symbols[range]..data[range]) + end + end + end + if result[1] ~= nil then + return table.concat(result, ' ') + else + return '' + end + end end - -return signify +return { init = function(options) return signify(options) end } diff --git a/lua/lualine/utils/utils.lua b/lua/lualine/utils/utils.lua index 5b984be..610c7b3 100644 --- a/lua/lualine/utils/utils.lua +++ b/lua/lualine/utils/utils.lua @@ -14,4 +14,10 @@ function M.expand_set_theme(func) end end +-- Note for now only works for termguicolors scope can be background or foreground +function M.extract_highlight_colors(color_group, scope) + local color = string.format('#%06x', vim.api.nvim_get_hl_by_name(color_group, true)[scope]) + return color or nil +end + return M