App-Basis

 view release on metacpan or  search on metacpan

lib/App/Basis.pm  view on Meta::CPAN

# ----------------------------------------------------------------------------
# special function to help us test this module, as it flags that we can die
# rather than exiting when doing some operations
# also test mode will not output to STDERR/STDOUT

sub set_test_mode
{
    $_test_mode = shift ;
}

# ----------------------------------------------------------------------------




# saymd function taken and modied from
# echomd -- An md like conversion tool for shell terminals
# https://raw.githubusercontent.com/WebReflection/echomd/master/perl/echomd
# some mod's of my own

#
# Fully inspired by the work of John Gruber
# <http://daringfireball.net/projects/markdown/>
#
# -----------------------------------------------------------------------------
# The MIT License (MIT)
# Copyright (c) 2016 Andrea Giammarchi - @WebReflection
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
# THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -----------------------------------------------------------------------------

# for *bold* _underline_ ~strike~ (strike on Linux only)
# it works with both double **__~~ or just single *_~
sub _bold_underline_strike
{
    my ($txt) = @_ ;
    $txt =~ s/(\*{1,2})(?=\S)(.+?)(?<=\S)\1/\x1B[1m$2\x1B[22m/gs ;
    $txt =~ s/(\_{1,2})(?=\S)(.+?)(?<=\S)\1/\x1B[4m$2\x1B[24m/gs ;
    $txt =~ s/(\~{1,2})(?=\S)(.+?)(?<=\S)\1/\x1B[9m$2\x1B[29m/gs ;
    return $txt ;
}

# for #color(text) or bg#bgcolor(text)
# virtually compatible with #RGBA(text)
# or for background via bg#RGBA(text)
sub _color
{
    my ($txt) = @_ ;
    $txt =~ s{(bg)?#([a-zA-Z0-9]{3,8})\((.+?)\)(?!\))}
           {_get_color($1,$2,$3)}egs ;
    return $txt ;
}

# for very important # Headers
# and for less important ## One
sub _header
{
    my ($txt) = @_ ;
    $txt =~ s{^(\#{1,6})[ \t]+(.+?)[ \t]*\#*([\r\n]+|$)}
           {_get_header($1,$2).$3}egm ;
    return $txt ;
}

# for horizontal lines
# --- or - - - or ___ or * * *
sub _horizontal
{
    my ($txt) = @_ ;
    my $line = "─" x 72 ;
    $txt =~ s{^[ ]{0,2}([ ]?[\*_-][ ]?){3,}[ \t]*$}
           {\x1B[1m$line\x1B[22m}gm ;
    return $txt ;
}

# for lists such:
#   * list 1
#     etc, etc
#   * list 2
#   * list 3
sub _list
{
    my ($txt) = @_ ;
    $txt =~ s/^([ \t]{2,})[*+-]([ \t]{1,})/$1•$2/gm ;
    return $txt ;
}

# for quoted text such:
# > this is quote
# > this is the rest of the quote
sub _quote
{
    my ($txt) = @_ ;
    $txt =~ s/^[ \t]*>([ \t]?)/\x1B[7m$1\x1B[27m$1/gm ;
    return $txt ;
}

# HELPERS

# used to grab colors by name
sub _get_color
{
    my $bg  = $1 ;
    my $rgb = $2 ;
    my $out = "" ;
    # one day, when it won't show experimental warnings

lib/App/Basis.pm  view on Meta::CPAN


    command         - string to execute (arrayrefs aren't supported, for some reason)
    timeout         - timeout (in seconds) before command is killed
    stdout_handler  - see IPC::Cmd docs
    stderr_handler  - see IPC::Cmd docs
    child_stdin     - pass data to STDIN of forked processes
    discard_output  - don't return output in hash
    terminate_on_parent_sudden_death

Output HASHREF

    exit_code       - exit code
    timeout         - time taken to timeout or 0 if timeout not used
    stdout          - text written to STDOUT
    stderr          - text written to STDERR
    merged          - stdout and stderr merged into one stream
    err_msg         - description of any error that occurred.

=item run_cmd

Basic way to run a shell program and get its output, this is not interactive.
For interactiviness see execute_cmd.

By default if you do not pass a full path to the command, then unless the command
is in /bin, /usr/bin, /usr/local/bin then the command will not run.

my ($code, $out, $err) = run_cmd( 'ls') ;
#
($code, $out, $err) = run_cmd( 'ls -R /tmp') ;

B<Parameters>
  string to run in the shell
  timeout (optional) in seconds

=item fix_filename

Simple way to replace ~, ./ and ../ at the start of filenames

B<Parameters>
  file name that needs fixing up

=item saymd

convert markdown text into something that can be output onto the terminal

saymd "# # Bringing MD Like Syntax To Bash Shell
It should be something as ***easy***
and as ___natural___ as writing text.

> Keep It Simple
> With quoted sections

Is the idea

  * behind
  * all this

~~~striking~~~ UX for `shell` users too.
- - -
#green(green text)
bg#red(red background text)
" ;

=back

=head1 AUTHOR

Kevin Mulholland <moodfarm@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Kevin Mulholland.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



( run in 2.842 seconds using v1.01-cache-2.11-cpan-d8267643d1d )