Aion-Format
view release on metacpan or search on metacpan
lib/Aion/Format.pm view on Meta::CPAN
package Aion::Format;
# ÐиблиоÑека ÑоÑмаÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð¸ вÑвода
use common::sense;
our $VERSION = "0.1.3";
require POSIX;
require Term::ANSIColor;
use Guard qw//;
use Exporter qw/import/;
our @EXPORT = our @EXPORT_OK = grep {
*{$Aion::Format::{$_}}{CODE} && !/^(_|(NaN|import)\z)/n
} keys %Aion::Format::;
#@category ÐÑвод ÑÑÑÑкÑÑÑ
use DDP qw//;
sub _extends_ddp_properties {
my ($properties) = @_;
+{
colored => 1,
deparse => 1,
show_unicode => 1,
show_readonly => 1,
print_escapes => 1,
show_refcount => 1,
show_memsize => eval { require Devel::Size; 1 },
caller_info => 1,
#output => 'stdout',
unicode_charnames => 1,
%$properties,
class => {
expand => "all",
inherited => "all",
show_reftype => 1,
%{$properties->{class}},
},
}
}
# p Ñ Ð¿ÑедÑÑÑановленнÑми опÑиÑми
sub p($;%) {
my ($arg, %properties) = @_;
%properties = %{_extends_ddp_properties(\%properties)};
DDP::p $arg, %properties
}
# np Ñ Ð¿ÑедÑÑÑановленнÑми опÑиÑми
sub np($;%) {
my ($arg, %properties) = @_;
%properties = %{_extends_ddp_properties(\%properties)};
DDP::np $arg, %properties
}
#@category ÐовÑÑки
# ÐовÑÑка Ð´Ð»Ñ STDERR
sub trapperr(&) {
my $sub = shift;
local *STDERR;
open STDERR, '>:utf8', \my $f; my $guard = Guard::guard { close STDERR };
$sub->();
undef $guard;
utf8::decode($f) unless utf8::is_utf8($f);
$f
}
# ÐовÑÑка Ð´Ð»Ñ STDOUT
sub trappout(&) {
my $sub = shift;
local *STDOUT;
open STDOUT, '>:utf8', \my $f; my $guard = Guard::guard { close STDOUT };
$sub->();
undef $guard;
utf8::decode($f) unless utf8::is_utf8($f);
$f
}
#@category ЦвеÑ
# ÐолоÑизиÑÑÐµÑ ÑекÑÑ escape-поÑледоваÑелÑноÑÑÑми: coloring("#{BOLD RED}ya#{}100!#RESET"), а заÑем - заменÑÐµÑ ÑоÑÐ¼Ð°Ñ sprintf-ом
sub coloring(@) {
my $s = shift;
$s =~ s!#\{(?<x>[\w \t]*)\}|#(?<x>\w+)!
my $x = $+{x};
$x = "RESET" if $x ~~ [qw/r R/];
Term::ANSIColor::color($x)
!nge;
sprintf $s, @_
}
# ÐеÑаÑÐ°ÐµÑ Ð² STDOUT вÑвод coloring
sub printcolor(@) {
print coloring @_
}
# ÐеÑаÑÐ°ÐµÑ Ð² STDERR вÑвод coloring
sub warncolor(@) {
print STDERR coloring @_
}
# ÐÐ»Ñ ÐºÑона: ÐиÑÐµÑ Ð² STDOUT
sub accesslog(@) {
print "[", POSIX::strftime("%F %T", localtime), "] ", coloring @_;
}
# ÐÐ»Ñ ÐºÑона: ÐиÑÐµÑ Ð² STDIN
sub errorlog(@) {
print STDERR "[", POSIX::strftime("%F %T", localtime), "] ", coloring @_;
}
#@category ÐÑеобÑазованиÑ
# ÐÑÐ¾Ð²Ð¾Ð´Ð¸Ñ ÑооÑвеÑÑÑвиÑ
#
# replace "...", qr/.../ => sub {...}, ...
#
sub matches($@) { goto &replace }
sub replace($@) {
my $s = shift;
my $i = 0;
my $re = join "\n| ", map { $i++ % 2 == 0? "(?<I$i> $_ )": () } @_;
my $arg = \@_;
my $fn = sub {
for my $k (keys %+) {
return $arg->[$k]->() if do { $k =~ /^I(\d+)\z/ and $k = $1 }
}
};
$s =~ s/$re/$fn->()/gex;
$s
}
#@category ТÑанÑлиÑеÑаÑиÑ
# ТÑанÑлиÑеÑÑиÑÑÐµÑ ÑÑÑÑкий ÑекÑÑ (x, w, q)
our %TRANS = qw/
а a и i п p Ñ c Ñ eh
б b й y Ñ r Ñ ch Ñ ju
lib/Aion/Format.pm view on Meta::CPAN
# ÐакÑимÑм в даннÑÑ
MediumText ÐаÑии
sub xxM() { 16*MiB-1 }
# ÐакÑимÑм в даннÑÑ
LongText ÐаÑии
sub xxL() { 4*GiB-1 }
#@category ÐонвеÑÑоÑÑ
# Ðаппинг индекÑа ФлеÑа Ð´Ð»Ñ Ñеловеков
my %FLESCH_INDEX_NAMES = (
100 => "Ð´Ð»Ñ Ð¼Ð»Ð°Ð´ÑеклаÑÑников",
90 => "Ð´Ð»Ñ 11 Ð»ÐµÑ (ÑÑÐ¾Ð²ÐµÐ½Ñ 5-го клаÑÑа)",
80 => "Ð´Ð»Ñ 12 Ð»ÐµÑ (6-й клаÑÑ)",
70 => "Ð´Ð»Ñ 13 Ð»ÐµÑ (7-й клаÑÑ)",
60 => "Ð´Ð»Ñ 8-Ñ
и 9-Ñ
клаÑÑов",
50 => "Ð´Ð»Ñ 10-Ñ
, 12-Ñ
клаÑÑов",
40 => "Ð´Ð»Ñ ÑÑÑденÑов",
30 => "Ð´Ð»Ñ Ð±Ð°ÐºÐ°Ð»Ð°Ð²Ñов",
20 => "Ð´Ð»Ñ Ð¼Ð°Ð³Ð¸ÑÑÑов",
10 => "Ð´Ð»Ñ Ð¿ÑоÑеÑÑионалов",
0 => "Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²",
);
sub flesch_index_human($) {
my ($flesch_index) = @_;
$FLESCH_INDEX_NAMES{int($flesch_index / 10) * 10} // "неÑвÑзнÑй ÑÑÑÑкий ÑекÑÑ"
}
1;
__END__
=encoding utf-8
=head1 NAME
Aion::Format - a Perl extension for formatting numbers, coloring output, etc.
=head1 VERSION
0.1.3
=head1 SYNOPSIS
use Aion::Format;
trappout { print "123\n" } # => 123\n
coloring "#red ⬠#r\n" # => \e[31m ⬠\e[0m\n
trappout { printcolor "#red ⬠#r\n" } # => \e[31m ⬠\e[0m\n
=head1 DESCRIPTION
Utilities for formatting numbers, coloring output, etc.
=head1 SUBROUTINES
=head2 coloring ($format, @params)
Colorizes text using escape sequences and then replaces the format with C<sprintf>. The color names are used from the C<Term::ANSIColor> module. For B<RESET> use C<#r> or C<#R>.
coloring "#{BOLD RED}###r %i", 6 # => \e[1;31m##\e[0m 6
=head2 printcolor ($format, @params)
Like C<coloring>, but prints the formatted string to standard output.
=head2 warncolor ($format, @params)
Like C<coloring>, but prints the formatted string to C<STDERR>.
trapperr { warncolor "#{green}ACCESS#r %i\n", 6 } # => \e[32mACCESS\e[0m 6\n
=head2 accesslog ($format, @params)
Writes to STDOUT using the C<coloring> function for formatting and adds a date-time prefix.
trappout { accesslog "#{green}ACCESS#r %i\n", 6 } # ~> \[\d{4}-\d{2}-\d{2} \d\d:\d\d:\d\d\] \e\[32mACCESS\e\[0m 6\n
=head2 errorlog ($format, @params)
Writes to B<STDERR> using the C<coloring> function for formatting and adds a date-time prefix.
trapperr { errorlog "#{red}ERROR#r %i\n", 6 } # ~> \[\d{4}-\d{2}-\d{2} \d\d:\d\d:\d\d\] \e\[31mERROR\e\[0m 6\n
=head2 p ($target; %properties)
C<p> from Data::Printer with preset settings.
Instead of the inconvenient first parameter, a simple scalar is used.
The optional C<%properties> parameter allows you to override settings.
trapperr { p +{cat => 123} } # ~> cat.+123
=head2 np ($target; %properties)
C<np> from Data::Printer with preset settings.
Instead of the inconvenient first parameter, a simple scalar is used.
The optional C<%properties> parameter allows you to override settings.
np +{cat => 123} # ~> cat.+123
=head2 flesch_index_human ($flesch_index)
Converts the Flesch index to a Russian label using step 10.
flesch_index_human -10 # => неÑвÑзнÑй ÑÑÑÑкий ÑекÑÑ
flesch_index_human -3 # => Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²
flesch_index_human 0 # => Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²
flesch_index_human 1 # => Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²
flesch_index_human 15 # => Ð´Ð»Ñ Ð¿ÑоÑеÑÑионалов
flesch_index_human 99 # => Ð´Ð»Ñ 11 Ð»ÐµÑ (ÑÑÐ¾Ð²ÐµÐ½Ñ 5-го клаÑÑа)
flesch_index_human 100 # => Ð´Ð»Ñ Ð¼Ð»Ð°Ð´ÑеклаÑÑников
flesch_index_human 110 # => неÑвÑзнÑй ÑÑÑÑкий ÑекÑÑ
=head2 from_radix ($string, $radix)
( run in 1.888 second using v1.01-cache-2.11-cpan-9581c071862 )