Skip to content

Commit a6669af

Browse files
committed
add -ruler switch to column highlighter
the -ruler <character> switch on the column highlighter replaces the column (for empty and whitespace cells) with <character>. this is useful for drawing rulers. because rulers typically appear over whitespace regions in other editors, we also do that here. additionally, the color of the ruler should only be applied to the ruler, so when -ruler <character> is provided, we do not apply the <face> parameter to cells which are non-empty, aka where the ruler is inserted.
1 parent 5b67664 commit a6669af

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

doc/pages/highlighters.asciidoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ highlighter is replaced with the new one.
111111
*fill* <face>::
112112
fill using the given *face*, mostly useful with regions highlighters
113113

114-
*column* <number> <face>::
115-
highlight column *number* with face *face*
114+
*column* <number> <face> [options]::
115+
highlight column *number* with face *face*, with the following *options*:
116+
117+
*-ruler* <character>:::
118+
a single character to display instead of empty or whitespace cells at
119+
column *number*. If this option is provided, *face* will also not
120+
apply to non-empty cells, and will only apply to *character*.
116121

117122
*line* <number> <face>::
118123
highlight line *number* with face *face*

src/highlighters.cc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "utf8.hh"
2020
#include "utf8_iterator.hh"
2121
#include "window.hh"
22+
#include "unicode.hh"
2223

2324
#include <cstdio>
2425
#include <limits>
@@ -519,16 +520,26 @@ UniquePtr<Highlighter> create_line_highlighter(HighlighterParameters params, Hig
519520
}
520521

521522
const HighlighterDesc column_desc = {
522-
"Parameters: <value string> <face>\n"
523-
"Highlight the column given by evaluating <value string> with <face>",
524-
{}
523+
"Parameters: [-ruler <character>] <column> <face>\n"
524+
"Highlight the column <column> with <face>",
525+
{
526+
{ { "ruler", { ArgCompleter{}, "replace empty or whitespace cells with the given character. When provided, <face> is not applied to non-empty cells" } } },
527+
ParameterDesc::Flags::SwitchesOnlyAtStart, 2, 2
528+
},
525529
};
526530
UniquePtr<Highlighter> create_column_highlighter(HighlighterParameters params, Highlighter*)
527531
{
528-
if (params.size() != 2)
529-
throw runtime_error("wrong parameter count");
532+
ParametersParser parser{params, column_desc.params};
530533

531-
auto func = [col_expr=params[0], facespec=parse_face(params[1])]
534+
auto positionals = parser.positionals_from(0);
535+
auto ruler = parser.get_switch("ruler");
536+
if (ruler.value_or(" ").char_length() > 1)
537+
throw runtime_error("-ruler expects a single character");
538+
539+
auto ruler_provided = static_cast<bool>(ruler);
540+
auto col_char = ruler.value_or(" ").str();
541+
542+
auto func = [col_expr=positionals[0], facespec=parse_face(positionals[1]), ruler_provided, col_char]
532543
(HighlightContext context, DisplayBuffer& display_buffer, BufferRange)
533544
{
534545
ColumnCount column = -1;
@@ -563,7 +574,12 @@ UniquePtr<Highlighter> create_column_highlighter(HighlighterParameters params, H
563574
atom_it = ++line.split(atom_it, remaining_col);
564575
if (atom_it->length() > 1)
565576
atom_it = line.split(atom_it, 1_col);
566-
atom_it->face = merge_faces(atom_it->face, face);
577+
if (!ruler_provided)
578+
atom_it->face = merge_faces(atom_it->face, face);
579+
if (ruler_provided && is_blank(atom_it->content()[0])) {
580+
atom_it->replace(col_char);
581+
atom_it->face = merge_faces(atom_it->face, face);
582+
}
567583
found = true;
568584
break;
569585
}
@@ -574,7 +590,7 @@ UniquePtr<Highlighter> create_column_highlighter(HighlighterParameters params, H
574590

575591
if (remaining_col > 0)
576592
line.push_back({String{' ', remaining_col}, Face{}});
577-
line.push_back({" ", face});
593+
line.push_back({col_char, face});
578594
}
579595
};
580596

0 commit comments

Comments
 (0)