Allow custom separator for line numbers

the number_lines highlighter accepts a -separator <arg> switch
to change the default '│' separator.

Fixes #295
This commit is contained in:
Maxime Coste 2015-09-25 13:47:57 +01:00
parent 4ea89def3b
commit 3262a6902c

View File

@ -707,9 +707,10 @@ HighlighterAndId create_show_whitespaces_highlighter(HighlighterParameters param
return {"show_whitespaces", make_simple_highlighter(show_whitespaces)}; return {"show_whitespaces", make_simple_highlighter(show_whitespaces)};
} }
template<bool relative, bool hl_cursor_line>
void show_line_numbers(const Context& context, HighlightFlags flags, void show_line_numbers(const Context& context, HighlightFlags flags,
DisplayBuffer& display_buffer, BufferRange) DisplayBuffer& display_buffer, BufferRange,
bool relative, bool hl_cursor_line,
StringView separator)
{ {
const Face face = get_face("LineNumbers"); const Face face = get_face("LineNumbers");
const Face face_absolute = get_face("LineNumberCursor"); const Face face_absolute = get_face("LineNumberCursor");
@ -718,8 +719,8 @@ void show_line_numbers(const Context& context, HighlightFlags flags,
for (LineCount c = last_line; c > 0; c /= 10) for (LineCount c = last_line; c > 0; c /= 10)
++digit_count; ++digit_count;
char format[] = "%?d│"; char format[16];
format[1] = '0' + digit_count + (relative ? 1 : 0); format_to(format, "%{}d{}", digit_count + (relative ? 1 : 0), separator);
int main_selection = (int)context.selections().main().cursor().line + 1; int main_selection = (int)context.selections().main().cursor().line + 1;
for (auto& line : display_buffer.lines()) for (auto& line : display_buffer.lines())
{ {
@ -739,24 +740,23 @@ HighlighterAndId number_lines_factory(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "relative", { false, "" } }, { { "relative", { false, "" } },
{ "separator", { true, "" } },
{ "hlcursor", { false, "" } } }, { "hlcursor", { false, "" } } },
ParameterDesc::Flags::None, 0, 0 ParameterDesc::Flags::None, 0, 0
}; };
ParametersParser parser(params, param_desc); ParametersParser parser(params, param_desc);
constexpr struct { StringView separator = parser.get_switch("separator").value_or("");
StringView name; if (separator.length() > 10)
void (*func)(const Context&, HighlightFlags, DisplayBuffer&, BufferRange); throw runtime_error("Separator length is limited to 10 bytes");
} funcs[] = {
{ "number_lines", show_line_numbers<false, false> },
{ "number_lines", show_line_numbers<false, true> },
{ "number_lines_relative", show_line_numbers<true, false> },
{ "number_lines_relative", show_line_numbers<true, true> },
};
const int index = (parser.get_switch("relative") ? 1 : 0) * 2 +
(parser.get_switch("hlcursor") ? 1 : 0);
return {funcs[index].name.str(), make_simple_highlighter(funcs[index].func)}; using namespace std::placeholders;
auto func = std::bind(show_line_numbers, _1, _2, _3, _4,
(bool)parser.get_switch("relative"),
(bool)parser.get_switch("hlcursor"),
separator.str());
return {"number_lines", make_simple_highlighter(std::move(func))};
} }
void show_matching_char(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange) void show_matching_char(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange)
@ -1346,7 +1346,7 @@ void register_highlighters()
"number_lines", "number_lines",
{ number_lines_factory, { number_lines_factory,
"Display line numbers \n" "Display line numbers \n"
"Parameters: -relative, -hlcursor\n" } }); "Parameters: -relative, -hlcursor, -separator <separator text>\n" } });
registry.append({ registry.append({
"show_matching", "show_matching",
{ create_matching_char_highlighter, { create_matching_char_highlighter,