Coder Social home page Coder Social logo

roughike / indent Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 3.0 1.02 MB

Change indentation in multiline Dart strings while preserving the existing relative indentation. Kotlin's trimIndent and trimMargin for Dart.

License: BSD 2-Clause "Simplified" License

Dart 90.77% HTML 4.41% Shell 4.82%

indent's Introduction

indent

pub package Build Status Coverage Status

Change indentation in multiline Dart strings while preserving the existing relative indentation.

A GIF speaks more than a thousand words:

A screencast of the example app in action

You can run the example app yourself by running cd example && pub get && webdev serve from the project root.

Usage

For convenience, the library adds the following extension members on Dart's String class.

You can also wrap a string with the Indentation class and call methods on that - this is what the extension methods do under the hood.

unindent()

If you found this library from a Google search, you're probably looking for the unindent() method. It's the use case this library was originally created for.

For example, this:

import 'package:indent/indent.dart';

print('''
          Hello
        there
             World!
'''.unindent());

outputs this:

  Hello
there
     World!

It gets rid of the common indentation while preserving the relative indentation. This is like Kotlin's trimIndent() or Java 12's align().

trimMargin([String marginPrefix = '|'])

Trims the leading whitespace followed by the marginPrefix from each line.

For example, this:

import 'package:indent/indent.dart';

print('''
        |  Hello
        |there
        |    World!
'''.trimMargin());

outputs this:

  Hello
there
    World!

Behaves just like trimMargin in Kotlin.

indent(int indentationLevel)

Indents a string with the desired indentation level while preserving relative indentation.

For example, this:

import 'package:indent/indent.dart';

print('''
   Hello
World
'''.indent(2));

prints:

     Hello
  World

If the starting indentation level is higher than the desired one, the value will be unindented accordingly.

This:

import 'package:indent/indent.dart';

print('''
          Hello
       World
'''.indent(2));

also prints:

     Hello
  World

(calling indent(0) is equal to calling unindent().)

indentBy(int howMuch)

Changes the indentation level in a string by howMuch.

A positive value for howMuch adds indentation.

For example, this:

import 'package:indent/indent.dart';

print('''
   Hello
World
'''.indentBy(4));

prints this:

       Hello
    World

When a negative value for howMuch is given, indentation is removed accordingly.

This:

import 'package:indent/indent.dart';

print('''
       Hello
    World
'''.indentBy(-4));

prints this:

   Hello
World

getIndentationLevel()

Returns the common indentation level in a string.

For example, this:

import 'package:indent/indent.dart';

final int indentationLevel= '''
     Hello
  World
'''.getIndentationLevel();

returns 2 as the two spaces before World is the lowest common indentation in the string.

indent's People

Contributors

dumazy avatar roughike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

masseelch dumazy

indent's Issues

Problem when used with inlined variables

For example:

String test() {
    String t = '123123\n123123';
    return '''
      _FutureBuilderError{
        error: Instance of 'WhatIfGodSmokedCannabisError', 
        stackTrace: 
          $t
      }
    '''.unindent();
}

Returns:

        _FutureBuilderError{
        error: Instance of 'WhatIfGodSmokedCannabisError, 
        stackTrace: 
          123123
123123
      }

Desirable behaviour

To (optionally?) put proper amount of tabs/spaces before each new line of inlined variable.

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.