File Coverage

blib/lib/App/Greple/frame.pm
Criterion Covered Total %
statement 11 35 31.4
branch 0 14 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 1 3 33.3
total 16 62 25.8


line stmt bran cond sub pod time code
1             package App::Greple::frame;
2             our $VERSION = "0.06";
3              
4             =encoding utf-8
5              
6             =head1 NAME
7              
8             App::Greple::frame - Greple frame output module
9              
10             =head1 SYNOPSIS
11              
12             greple -Mframe --frame ...
13              
14             =head1 DESCRIPTION
15              
16             Greple -Mframe module provide a capability to put surrounding frames
17             for each blocks.
18              
19             C, C and C frames are printed for blocks.
20              
21             By default B<--join-blocks> option is enabled to collect consecutive
22             lines into a single block. If you don't like this, override it by
23             B<--no-join-blocks> option.
24              
25             =head1 OPTIONS
26              
27             =over 7
28              
29             =item B<--frame>
30              
31             =for comment
32             =item B<--frame-fold>
33              
34             Set frame and fold long lines with frame-friendly prefix string.
35             Folding width is taken from the terminal. Or you can specify the
36             width by calling B function with module option.
37              
38             =begin comment
39              
40             =item B<--frame-simple>
41              
42             Set frame without folding.
43              
44             =end comment
45              
46             =back
47              
48             Put next line in your F<~/.greplerc> to autoload B module.
49              
50             autoload -Mframe --frame
51              
52             Then you can use B<--frame> option whenever you want.
53              
54             =begin html
55              
56            

57              
58             =end html
59              
60             =head1 FUNCTION
61              
62             =over 7
63              
64             =item B(B=I)
65              
66             Set terminal width to I. Use like this:
67              
68             greple -Mframe::set(width=80) ...
69              
70             greple -Mframe::set=width=80 ...
71              
72             If non-digit character is found in the value part, it is considered as
73             a Reverse Polish Notation, starting terminal width pushed on the
74             stack. RPN C<2/3-> means C.
75              
76             You can use like this:
77              
78             greple -Mframe::set=width=2/3- --frame --uc '(\w+::)+\w+' --git | ansicolumn -PC2
79              
80             =begin html
81              
82            

83              
84             =end html
85              
86             =back
87              
88             =head1 SEE ALSO
89              
90             L
91              
92             =head1 AUTHOR
93              
94             Kazumasa Utashiro
95              
96             =head1 LICENSE
97              
98             Copyright 2022 Kazumasa Utashiro.
99              
100             This library is free software; you can redistribute it and/or modify
101             it under the same terms as Perl itself.
102              
103             =cut
104              
105 1     1   652 use 5.014;
  1         3  
106 1     1   4 use warnings;
  1         2  
  1         19  
107 1     1   481 use utf8;
  1         11  
  1         5  
108              
109             my($mod, $argv);
110             my $width;
111             my($head, $blockend, $file_start, $file_end);
112              
113             my %param = (
114             width => undef,
115             );
116              
117             sub terminal_width {
118 1     1   445 use Term::ReadKey;
  1         1771  
  1         379  
119 0     0 0   my $default = 80;
120 0           my @size;
121 0 0         if (open my $tty, ">", "/dev/tty") {
122             # Term::ReadKey 2.31 on macOS 10.15 has a bug in argument handling
123             # and the latest version 2.38 fails to install.
124             # This code should work on both versions.
125 0           @size = GetTerminalSize $tty, $tty;
126             }
127 0 0         $size[0] or $default;
128             }
129              
130             sub finalize {
131 0     0 0   ($mod, $argv) = @_;
132 0   0       $width = $param{width} || terminal_width;
133 0 0         if ($width =~ /\D/) {
134             require App::Greple::frame::RPN
135 0 0         and App::Greple::frame::RPN->import('rpn_calc');
136 0 0         $width = int(rpn_calc(terminal_width, $width)) or die "$width: format error\n";
137             }
138              
139 0           my $frame_top = ' ┌─' ;
140 0           my $frame_middle = ' ⋮ ├╶' ;
141 0           my $frame_bottom = '──────┴─' ;
142              
143 0           for ($frame_top, $frame_middle, $frame_bottom) {
144 0 0         if ((my $rest = $width - length) > 0) {
145 0           $_ .= (substr($_, -1, 1) x $rest);
146             }
147             }
148              
149 0           $mod->setopt('--show-frame-top', '--frame-top' => "'$frame_top'");
150 0           $mod->setopt('--show-frame-middle', '--frame-middle' => "'$frame_middle'");
151 0           $mod->setopt('--show-frame-bottom', '--frame-bottom' => "'$frame_bottom'");
152 0           $mod->setopt(
153             '--ansifold',
154             '--pf' => "'ansifold -x --width=$width --prefix \" │ \"'",
155             );
156             }
157              
158             sub set {
159 0     0 1   while (my($k, $v) = splice(@_, 0, 2)) {
160 0 0         exists $param{$k} or next;
161 0           $param{$k} = $v;
162             }
163 0           ();
164             }
165              
166             1;
167              
168             __DATA__