GithubHelp home page GithubHelp logo

robotdana / tty_string Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 76 KB

render strings like a tty

License: MIT License

Ruby 99.41% Shell 0.59%
tty ansi-colors ansi-escape-codes ansi-escape-sequences ansi-escape ansi-terminal ruby

tty_string's Introduction

TTYString

Build Status Gem Version

Render to a string like your terminal does by (narrowly) parsing ANSI TTY codes. Intended for use in tests of command line interfaces.

Features

  • supports ruby 2.4 - 3.0.0.preview1, and jruby
  • has no dependencies outside ruby stdlib

Supported codes

Code Description Default
\a bell: suppressed
\b backspace: clear the character to the left of the cursor and move the cursor back one column
\n newline: move the cursor to the start of the next line
\r return: move the cursor to the start of the current line
\t tab: move the cursor to the next multiple-of-8 column
\e[nA move the cursor up n lines n=1
\e[nB move the cursor down n lines n=1
\e[nC move the cursor right n columns n=1
\e[nD move the cursor left n columns n=1
\e[nE move the cursor down n lines, and to the start of the line n=1
\e[nF move the cursor up n lines, and to the start of the line n=1
\e[nG move the cursor to column n. 1 is left-most column n=1
\e[n;mH
\e[n;mf
move the cursor to row n, column m. 1;1 is top left corner n=1 m=1
\e[nJ n=0: clear the screen from the cursor forward
n=1: clear the screen from the cursor backward
n=2 or n=3: clear the screen
n=0
\e[nK n=0: clear the line from the cursor forward
n=1: clear the line from the cursor backward
n=2: clear the line
n=0
\e[nS scroll up n rows n=1
\e[nT scroll down n rows n=1
\e[m styling codes: optionally suppressed with clear_style: false
\e[?2004h enabled bracketed paste: suppressed
\e[?2004l disable bracketed paste: suppressed
\e[200~ bracketed paste start: suppressed
\e[201~ bracketed paste end: suppressed

Installation

Add this line to your application's Gemfile:

gem 'tty_string', '~> 1.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tty_string

Usage

TTYString.parse("th\ta string\e[3Gis is")
=> "this is a string"

Styling information is suppressed by default:

TTYString.parse("th\ta \e[31mstring\e[0m\e[3Gis is")
=> "this is a string"

But can be passed through:

TTYString.parse("th\ta \e[31mstring\e[0m\e[3Gis is", clear_style: false)
=> "this is a \e[31mstring\e[0m"

Just for fun TTYString.to_proc provides the parse method as a lambda, so:

["th\ta string\e[3Gis is"].each(&TTYString)
=> ["this is a string"]

Limitations

  • Various terminals are wildly variously permissive with what they accept, so this doesn't even try to cover all possible cases, instead it covers the narrowest possible case, and leaves the codes in place when unrecognized

  • clear_style: false treats the style codes as regular text which may work differently when rendering codes that move the cursor.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake to run the tests and linters. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/robotdana/tty_string.

License

The gem is available as open source under the terms of the MIT License.

tty_string's People

Contributors

robotdana avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

tty_string's Issues

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.