Coder Social home page Coder Social logo

text-indent's Introduction

NAME

Text::Indent - simple indentation of text shared among modules

SYNOPSIS

In your main program:

use Text::Indent;
my $indent = Text::Indent->new;
$indent->spaces(2);

In a module to produce indented output:

use Text::Indent;
my $indent = Text::Indent->instance;
$indent->increase;
print $indent->indent("this will be indented two spaces");
$indent->increase(2);
print $indent->indent("this will be indented six spaces");
$indent->decrease(3);

DESCRIPTION

Text::Indent is designed for use in programs which need to produce output with multiple levels of indent when the source of the output comes from different modules that know nothing about each other.

For example take module A, whose output includes the indented output of module B. Module B can also produce output directly, so it falls to module B to know whether it should indent it's output or not depending on it's calling context.

Text::Indent allows programs and modules to cooperate to choose an appropriate indent level that is shared within the program context. In the above example, module A would increase the indent level prior to calling the output routines of module B. Module B would simply use the Text::Indent instance confident that if it were being called directly no indent would be applied but if module A was calling it then it's output would be indented one level.

CONSTRUCTOR

The constructor for Text::Indent should only be called once by the main program using modules that produce indented text. Modules which wish to produce indented text should use the instance accessor described below.

To construct a new Text::Indent object, call the new method, passing one or more of the following parameters as a hash:

  • Spaces

    the number of spaces to used for each level of indentation. Defaults to 2.

  • SpaceChar

    the character to be used for indentation. Defaults to a space (ASCII 32)

  • Level

    The initial indentation level to set. Defaults to 0.

  • AddNewLine

    Whether the indent method should automatically add a newline to the input arguments. Defaults to TRUE.

  • Instance

    Whether the newly constructed Text::Indent object should become the new singleton instance returned by the instance accessor. Defaults to TRUE.

INSTANCE ACCESSOR

The instance accessor is designed to be used by modules wishing to produce indented output. If the instance already exists (as will be the case if the main program using the module constructed a Text::Indent object) then both the program and the module will use the same indentation scheme.

If the instance does not exist yet, the instance accessor dispatches it's arguments to the constructor. As such, any of the parameters that the constructor takes may also be passed to the instance accessor. Be mindful that if the instance does exist, any parameters passed to the instance accessor are ignored.

METHODS

increase($how_many)

This method increases the level of indentation by $how_many levels. If not provided, $how_many defaults to 1.

decrease

This method decreases the level of indentation by $how_many levels. If not provided, $how_many defaults to 1.

reset

This method resets the level of indentation to 0. It is functionally equivalent to $indent->level(0).

indent(@what)

This is the primary workhorse method of Text::Indent. It takes a list of arguments to be indented and returns the indented string.

The string returned is composed of the following list:

  • the 'space' character repeated x times, where x is the number of spaces multiplied by the indent level.
  • the stringification of arguments passed to the method (note that this means that list arguments will have spaces inserted in between them).
  • a newline if the 'add_newline' attribute of the Text::Indent object is set.

If the indent level drops is a negative value, no indent is applied.

ACCESSORS

spaces

Gets or sets the number of spaces used for each indent level.

spacechar

Gets or sets the character used for indentation.

level

Gets or sets the current indent level.

add_newline

Gets or sets the boolean attribute that determines if the indent method tacks a newline onto it's arguments.

EXAMPLES

In the main program producing indented output:

use Text::Indent;
use Bar;
my $bar = Bar->new(...);
my $i = Text::Indent->new( Level => 1 );
print $i->indent("foo");
$i->increase;
print $bar->display;
$i->decrease;
print $i->indent("baz");
$i->reset;
print $i->indent("gzonk");

In Bar.pm:

package Bar;
use Text::Indent;
sub display
{
  my $i = Text::Indent->instance;
  return $i->indent("bar");
}

The output from the preceding example would be (> indicates the left edge of output and is for illustrative purposes only):

>  foo
>    bar
>  baz
>gzonk

AUTHOR

James FitzGibbon, <[email protected]>

COPYRIGHT

Copyright (c) 2003-10 James FitzGibbon. All Rights Reserved.

This module is free software; you may use it under the same terms as Perl itself.

text-indent's People

Contributors

jf647 avatar renderorange avatar

Stargazers

 avatar

Watchers

 avatar  avatar

text-indent's Issues

update issue tracker and switch to ExtUtils::MakeMaker

  • switching to github for the issue tracker, as well as adding links to the repo in the meta.
  • update the Pod for copyright and current maintainer info.
  • add README.md for github display, with exemption in the MANIFEST.skip and links to the testing and coverage platforms; both CPAN testers, kwalitee, travis, and coveralls (will perhaps not display everything). update README.md with a link to the metacpan page, or link at the top.

Remove test snippets from pod

Assuming the previous maintainer was using Test::Pod::Snippets or something similar to generate tests from the Pod in the module.

Remove the test snippets from the Pod, then clean up the tests.

Fix cover output in travis build

cover is reporting the tests and not the module.

---------------------------- ------ ------ ------ ------
File                           stmt    sub   time  total
---------------------------- ------ ------ ------ ------
t/01_use.t                    100.0  100.0    8.3  100.0
t/02_pod.t                    100.0  100.0    7.3  100.0
t/03_pod_coverage.t           100.0  100.0    8.5  100.0
t/04_breakpoints.t            100.0  100.0    7.6  100.0
t/05_construct.t              100.0  100.0    9.0  100.0
t/06_indent_level_increase.t  100.0  100.0    8.3  100.0
t/07_indent_level_decrease.t  100.0  100.0    8.4  100.0
t/08_indent_level_reset.t     100.0  100.0   16.3  100.0
t/09_indent.t                 100.0  100.0    8.5  100.0
t/11_accessors.t              100.0  100.0    8.2  100.0
t/12_instance.t               100.0  100.0    9.2  100.0
Total                         100.0  100.0  100.0  100.0
---------------------------- ------ ------ ------ ------

Negative repeat count does nothing

from https://rt.cpan.org/Public/Bug/Display.html?id=108678

Tue Nov 10 14:55:08 2015 SREZIC [...] cpan.org - Ticket created
Subject: | Negative repeat count does nothing

With recent perls (e.g. 5.22.1-RC1) there's a new warning causing a test failure:

#    Failed test 'no warnings'
#        Negative repeat count does nothing at /tmpfs/.cpan-build/2015111018/Text-Indent-0.02-CTVop8/blib/lib/Text/Indent.pm line 303.
t/09_indent.t ................. 

Tue Nov 10 14:57:38 2015 SREZIC [...] cpan.org - Correspondence added

On 2015-11-10 14:55:08, SREZIC wrote:
See also http://matrix.cpantesters.org/?dist=Text-Indent%200.02 for a fail/pass overview --- tests start failing with perl 5.22.0.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.