Git Product home page Git Product logo

textio's Introduction

textio CircleCI Go Report Card GoDoc

Go package providing tools for advanced text manipulations

Motivation

This package aims to provide a sutie of tools to deal with text parsing and formatting. It is intended to extend what the standard library already offers, and make it easy to integrate with it.

Examples

This sections presents a couple of examples about how to use this package.

Indenting

Indentation is often a complex problem to solve when dealing with stream of text that may be composed of multiple lines. To address this problem, this package provides the textio.PrefixWriter type, which implements the io.Writer interface and automatically prepends every line of output with a predefined prefix.

Here is an example:

func copyIndent(w io.Writer, r io.Reader) error {
    p := textio.NewPrefixWriter(w, "\t")

    // Copy data from an input stream into the PrefixWriter, all lines will
    // be prefixed with a '\t' character.
    if _, err := io.Copy(p, r); err != nil {
        return err
    }

    // Flushes any data buffered in the PrefixWriter, this is important in
    // case the last line was not terminated by a '\n' character.
    return p.Flush()
}

Tree Formatting

A common way to represent tree-like structures is the formatting used by the tree(1) unix command. The textio.TreeWriter type is an implementation of an io.Writer which supports this kind of output. It works in a recursive fashion where nodes created from a parent tree writer are formatted as part of that tree structure.

Here is an example:

func ls(w io.Writer, path string) {
	tree := NewTreeWriter(w)
	tree.WriteString(filepath.Base(path))
	defer tree.Close()

	files, _ := ioutil.ReadDir(path)

	for _, f := range files {
		if f.Mode().IsDir() {
			ls(tree, filepath.Join(path, f.Name()))
		}
	}

	for _, f := range files {
		if !f.Mode().IsDir() {
			io.WriteString(NewTreeWriter(tree), f.Name())
		}
	}
}

...

ls(os.Stdout, "examples")

Which gives this output:

examples
├── A
│   ├── 1
│   └── 2
└── message

textio's People

Contributors

achille-roussel avatar

Watchers

James Cloos avatar

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.