0

I'm trying to create a status line and get the color scheme correctly. I have the function

function GetDiagnosticErrorCount()
  local errorCount = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR })
  if (errorCount > 0) then
    return '  ' .. errorCount .. ''
  end
  return ''
end

and would like it to have a colorscheme. My current solution today is to add colorscheme before in the definition of the statusline.

%#StatusLineError#%{luaeval('GetDiagnosticErrorCount()')}%

However, this becomes bad because I don't want the space before the error count to get the highlight, and I don't want the space to be there unless it's needed. The ideal solution would be to be able to add the colorscheme directly in the returned string:

function GetDiagnosticErrorCount()
  local errorCount = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR })
  if (errorCount > 0) then
    return ' %#StatusLineError#% ' .. errorCount .. '%'
  end
  return ''
end

But this doesn't work.

1
  • 1
    Try using %{% as in :help statusline to re-evaluate the result as a statusline string? (If that works, feel free to self-answer.)
    – D. Ben Knoble
    Jun 12, 2023 at 14:01

1 Answer 1

1

As D. Ben Knoble suggested, you can wrap it in %{% ... %} and then it will be evaluated as a statusline string!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.