Skip to content

Commit

Permalink
border_style rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
doekman committed Mar 7, 2022
1 parent caf3182 commit 01a6ff3
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 52 deletions.
4 changes: 2 additions & 2 deletions assets/border/border.awkcss
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ BEGIN {
}
/^>/ {
border("ascii");
#border_style_left("unset");
}
border_style_left("none");
}
6 changes: 3 additions & 3 deletions assets/border/border.expected
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Heading
Paragraph.
+------------------+
|> Quote |
+------------------+
-------------------+
> Quote |
-------------------+
Another paragraph.
17 changes: 15 additions & 2 deletions library.awk
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ BEGIN {
_ENUM["color", bright_magenta="bright_magenta"] = 65;
_ENUM["color", bright_cyan="bright_cyan" ] = 66;
_ENUM["color", bright_white="bright_white" ] = 67; # bright_white
# border_style
_ENUM["border_style", ascii="ascii"] = 1;
# border_style (value is comma-splittable array, first value is encoding-type)
# 1: telephone number pad, 1..9 (5 is empty) defines edge characters
_ENUM["border_style", none="none"] = "0";
_ENUM["border_style", ascii="ascii"] = "1,+,-,+,|,,|,+,-,+";
_ENUM["border_style", ascii_round="ascii_rounded"] = "1,/,-,\\,|,,|,\\,_,/";
# display
_ENUM["display", block="block"] = block;
_ENUM["display", none="none" ] = none;
Expand All @@ -41,6 +44,7 @@ BEGIN {
select();
_STATE["last_vertical_margin"] = 0; # for use with margin collapse, and block continuation
_STATE["last_block_name"] = ""; # determine if a line is a block continuation
_STATE["border_row"] = 0; # 0 undefined, 1 border-top, 2 inside border, 3 border-bottom
}

# section is gemodelleerd naar console.group()
Expand Down Expand Up @@ -110,6 +114,15 @@ function _set_property(property_name, property_value) {
_set_property_bare(NR, _QUERY, property_name, property_value)
}

function get_border_glyph(style, part , items, glyph_index) {
split(_ENUM["border_style", style], items, ",");
if (items[0] == "0" || _STATE["border_row"] == 0) {
return "";
}
glyph_index = (_STATE["border_row"] - 1) * 3 + (part + 1);
#printf("(%s,%s,%s,%s,%s)", style, _STATE["border_row"], part, glyph_index, items[glyph_index])
return items[glyph_index];
}
# -=[ Public functions ]=-
function select(query) {
if (query) {
Expand Down
53 changes: 29 additions & 24 deletions reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You can use any awk-condition to select a line:
# Will select every odd line
NR % 2 == 1 { color(green); }

To apply a setting to every line, you can either omit the awk-condition, or use the `BEGIN` template. The latter works a bit different, since it only introduces one entry in the CSS DOM. So this is the prefered approach:
To apply a setting to every line, you can either omit the awk-condition, or use the `BEGIN` template. The latter works a bit different, since it only introduces one entry in the CSS DOM. So this is the preferred approach:

BEGIN { background(gray); }

Expand All @@ -43,39 +43,26 @@ So stylize text, you can use the following properties:
- `text_decoration_line( underline | blink [, ... ] )`: make the text _underline_ and/or _blink_.
* `font_weight( normal | bold )`: make the text appear _bold_ or _normal_.

`named_color` is defined one of:

* `black` and `bright_black` (or `gray`)
* `red` and `bright_red`
* `green` and `bright_green`
* `yellow` and `bright_yellow`
* `blue` and `bright_blue`
* `magenta` and `bright_magenta`
* `cyan` and `bright_cyan`
* `white` and `bright_white`

Always be aware of the capabilities of your system. You can inspect the supported number of colors by the `COLORS` variable (see also below). Black and white always work, but for the normal colors, you need at least 8 colors. To use the bright-variations, 16 colors is the minimum.

To control the box model (i.e. lines of text), use the following properties:

* `display( block | none )`:
- __block__ (default): a paragraph (one or more lines) is created.
- __none__: the line is not rendered (use this instead of `next`).
* `block_name( "name" )`:
- For setting up _block continuation_.
- Normally, every input line results in a block-item in the box-model. To combine adjecent input lines into one block-item, give these lines the same name.
- Normally, every input line results in a block-item in the box-model. To combine adjacent input lines into one block-item, give these lines the same name.
- When not set, the block-name is regarded the same as the `NR` variable.
* `width( [nr_columns] )`: sets the width, including margins, of a line.
- __nr\_columns__: set the maximum width of a line. Handy when using background colors.
- _no arguments_: when omitting `nr_columns`, the default value is used, which is the width of the terminal (see variable `COLS` below).
* `tab_size( nr_characters )`:
- __nr\_characters__: set the width of a tab-character. Must be a positive integer value. Defaults to `8`.
* `margin( a [, b [, c [, d ]]] )`: Margin collapse for top/bottom margins is supported.
- `margin_top( length )`: nr of lines
- `margin_right( length )`: nr of columns
- `margin_bottom( length )`: nr of lines
- `margin_left( length )`: nr of columns
* `border( named_style )`: for border styles, see `named_style` below. <mark>Rendering not implemented yet</mark>
- `margin_top( length )`: number of lines
- `margin_right( length )`: number of columns
- `margin_bottom( length )`: number of lines
- `margin_left( length )`: number of columns
* `border( named_style )`: for border styles, see `named_style` below.
- `border_style( named_style )`
+ `border_style_top( named_style )`
+ `border_style_right( named_style )`
Expand All @@ -90,13 +77,31 @@ To control the box model (i.e. lines of text), use the following properties:
- __"string"__ (experimental): to specify a different character or characters, use a awk string. For example `text_overflow("8<")`.
- Note: because of `UTF-8`, when using a comma (`,`) or non-ASCII characters, prefix the clipping indicator with character length and a comma. For example: `text_overflow("1,❗️")`.

`named_style` is used for border styles, and is defined one of:
Other properties:

* `ascii` for simple borders (left/right: `|`, top/bottom: `-`, corners: `+`)
* `content( "any string" )`: display text for use with the `::before` and `::after` selectors. All lines are replaceable by the way.

Other properties:

* `content( "any string" )`: display text for use with the `::before` and `::after` selectors. All lines are replaceble by the way.
## Enumerations

`named_color` is defined one of:

* `black` and `bright_black` (or `gray`)
* `red` and `bright_red`
* `green` and `bright_green`
* `yellow` and `bright_yellow`
* `blue` and `bright_blue`
* `magenta` and `bright_magenta`
* `cyan` and `bright_cyan`
* `white` and `bright_white`

Always be aware of the capabilities of your system. You can inspect the supported number of colors by the `COLORS` variable (see also below). Black and white always work, but for the normal colors, you need at least 8 colors. To use the bright-variations, 16 colors is the minimum.

`named_style` is used for border styles, and is defined one of:

* `none` to unset borders
* `ascii` for simple borders (left/right: `|`, top/bottom: `-`, corners: `+`)
* `ascii_rounded` same (left/right: `|`, top: `-`, bottom: `_`, corners: `/` and `\`)


## Variables
Expand Down
103 changes: 82 additions & 21 deletions render.awk
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,56 @@ function _print_inline_block(text, from_index, width , terminal_line, pos, tab_
# return the number of characters of the original "text" variable where consumed
return width - ( (tab_size - 1) * nr_tabs_expanded)
}
function _print_margin_line(text, from_index , current_width, margin_part, chars_consumed) {
function _print_border_line(text, from_index, current_width , border_part, border_glyph, chars_consumed) {
border_part = _get_property("border_style_left");
if (border_part) {
if (current_width <= 2) {
_warning("border_style_left", border_part, "Ignored, because width is too small.")
}
else {
border_glyph = get_border_glyph(border_part, 1);
printf("%s", border_glyph);
current_width -= length(border_glyph);
}
}
border_part = _get_property("border_style_right");
if (border_part) {
if (current_width <= 2) {
_warning("border_style_right", border_part, "Ignored, because width is too small.")
}
else {
border_glyph = get_border_glyph(border_part, 3);
current_width -= length(border_glyph);
}
}
chars_consumed = _print_inline_block(text, from_index, current_width)
if (border_part) {
printf("%s", border_glyph);
}
return chars_consumed;
}
function _print_margin_line(text, from_index , current_width, left_margin, right_margin, chars_consumed) {
current_width = _get_property("width");
margin_part = _get_property("margin_left");
if (margin_part >= current_width) {
_warning("margin_left", margin_part, "Ignored, because width is too small.")
margin_part = 0;
left_margin = _get_property("margin_left");
if (left_margin >= current_width) {
_warning("margin_left", left_margin, "Ignored, because width is too small.")
left_margin = 0;
}
else if (margin_part > 0) {
#printf("%d>%" margin_part "s", margin_part, " ");
printf("%" margin_part "s", " ");
current_width -= margin_part
else if (left_margin > 0) {
printf("%" left_margin "s", " ");
current_width -= left_margin
}
margin_part = _get_property("margin_right");
if (margin_part >= current_width) {
_warning("margin_right", margin_part, "Ignored, because width is too small.")
margin_part = 0;
right_margin = _get_property("margin_right");
if (right_margin >= current_width) {
_warning("margin_right", right_margin, "Ignored, because width is too small.")
right_margin = 0;
}
else if (margin_part > 0) {
current_width -= margin_part
else if (right_margin > 0) {
current_width -= right_margin
}
chars_consumed = _print_inline_block(text, from_index, current_width);
if (margin_part > 0) {
#printf("%" margin_part "s%<d", " ", margin_part);
printf("%" margin_part "s", " ");
chars_consumed = _print_border_line(text, from_index, current_width)
if (right_margin > 0) {
printf("%" right_margin "s", " ");
}
printf "\n";
return chars_consumed;
Expand All @@ -96,6 +122,37 @@ function _print_vertical_margin(margin_property_name , value) {
NR += 1; # restore
}
}
function _print_vertical_border(border_side , border_style, border_width, border_glyph, border_corner_width) {
if (border_side == "bottom") {
NR -= 1;
}
border_width = _get_property("width") - _get_property("margin_left") - _get_property("margin_right");
if (border_side == "bottom") {
border_style = _get_property("border_style_bottom");
_STATE["border_row"] = 3;
border_glyph = get_border_glyph(border_style, 2);
if (border_glyph) {
border_corner_width = length(get_border_glyph(_get_property("border_style_left"), 1) get_border_glyph(_get_property("border_style_right"), 3))
_print_margin_line(str_mul(border_glyph, border_width - border_corner_width), 0);
}
}
else { # border_side == "top"
border_style = _get_property("border_style_top");
_STATE["border_row"] = 1;
border_glyph = get_border_glyph(border_style, 2);
if (border_glyph) {
border_corner_width = length(get_border_glyph(_get_property("border_style_left"), 1) get_border_glyph(_get_property("border_style_right"), 3))
_print_margin_line(str_mul(border_glyph, border_width - border_corner_width), 0);
}
}
if (border_side == "bottom") {
NR += 1;
}
}
function _close_box() {
_print_vertical_border("bottom");
_print_vertical_margin("margin_bottom");
}
function _print_margin_box( text, last_block_name, current_block_name) {
if (_get_property("display") == block) {
if (_has_property("content")) {
Expand All @@ -109,12 +166,14 @@ function _print_margin_box( text, last_block_name, current_block_name) {
# We only know whether to print a bottom_margin, when the next line DOESN'T belong
# to the next block, so we need to buffer this.
if (NR > 1 && last_block_name != current_block_name) {
_print_vertical_margin("margin_bottom");
_close_box();
}

if (last_block_name != current_block_name) {
_print_vertical_margin("margin_top");
_print_vertical_border("top");
}
_STATE["border_row"] = 2;
if (_get_property("white_space") == pre_wrap) {
_index = 0;
while (_index == 0 || _index < length(text)) {
Expand Down Expand Up @@ -145,7 +204,9 @@ function _print_margin_box( text, last_block_name, current_block_name) {

END {
# We need to close the last box
_print_vertical_margin("margin_bottom");
NR += 1;
_close_box();
NR -= 1;
if (_DUMP != "") {
_bat_debug(_DUMP);
}
Expand Down

0 comments on commit 01a6ff3

Please sign in to comment.