Skip to content

Commit c14f2be

Browse files
committed
Add and test horizontal-ruler sub
1 parent 798b80b commit c14f2be

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/Text/MiscUtils/Layout.rakumod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ unit module Text::MiscUtils::Layout;
55
use Terminal::ANSIColor;
66

77

8+
#| Format a simple glanceable Unicode 1.1 horizontal ruler
9+
sub horizontal-ruler(UInt:D $width = 80) is export {
10+
# 16:9 - 64x18, 80x22.5, 96x27, 112x31.5, 128x36
11+
# 16:10 - 64x20, 80x25, 96x30, 112x35, 128x40
12+
13+
constant $nine = '¹²³⁴⁵⁶⁷⁸⁹';
14+
constant @markers = flat (1..9).map({ chr($_ + 0x2775) }), '';
15+
constant $hundred = @markers.map({ $nine ~ $_ }).join;
16+
17+
my $ruler = $hundred x (ceiling $width / 100);
18+
substr($ruler, 0, $width)
19+
}
20+
21+
822
#| Calculate monospaced width of a single line of text, ignoring ANSI colors
923
# XXXX: Does not handle cursor-movement control characters such as TAB
1024
sub duospace-width(Str:D $text, Bool :$wide-context = False) is export {

t/02-layout.rakutest

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ use Test;
22
use Text::MiscUtils::Layout;
33

44

5-
plan 75;
5+
plan 80;
6+
7+
8+
# horizontal-ruler()
9+
10+
is horizontal-ruler(0), '', 'empty horizontal ruler';
11+
is horizontal-ruler(5), '¹²³⁴⁵', 'short horizontal ruler';
12+
is horizontal-ruler(25), '¹²³⁴⁵⁶⁷⁸⁹❶¹²³⁴⁵⁶⁷⁸⁹❷¹²³⁴⁵', 'mid-length horizontal ruler';
13+
is horizontal-ruler, '¹²³⁴⁵⁶⁷⁸⁹❶¹²³⁴⁵⁶⁷⁸⁹❷¹²³⁴⁵⁶⁷⁸⁹❸¹²³⁴⁵⁶⁷⁸⁹❹¹²³⁴⁵⁶⁷⁸⁹❺¹²³⁴⁵⁶⁷⁸⁹❻¹²³⁴⁵⁶⁷⁸⁹❼¹²³⁴⁵⁶⁷⁸⁹❽', 'default horizontal ruler';
14+
is horizontal-ruler(128), '¹²³⁴⁵⁶⁷⁸⁹❶¹²³⁴⁵⁶⁷⁸⁹❷¹²³⁴⁵⁶⁷⁸⁹❸¹²³⁴⁵⁶⁷⁸⁹❹¹²³⁴⁵⁶⁷⁸⁹❺¹²³⁴⁵⁶⁷⁸⁹❻¹²³⁴⁵⁶⁷⁸⁹❼¹²³⁴⁵⁶⁷⁸⁹❽¹²³⁴⁵⁶⁷⁸⁹❾¹²³⁴⁵⁶⁷⁸⁹│¹²³⁴⁵⁶⁷⁸⁹❶¹²³⁴⁵⁶⁷⁸⁹❷¹²³⁴⁵⁶⁷⁸', 'long horizontal ruler';
615

716

817
# duospace-width() -- XXXX: NEEDS TESTS

0 commit comments

Comments
 (0)