Coder Social home page Coder Social logo

piotrmurach / tty-editor Goto Github PK

View Code? Open in Web Editor NEW
27.0 3.0 3.0 149 KB

Opens a file or text in the user's preferred editor

Home Page: https://ttytoolkit.org

License: MIT License

Ruby 99.63% Shell 0.37%
ruby-gem tty edittext tty-components cli terminal console ruby

tty-editor's Introduction

TTY Toolkit logo

TTY::Editor

Gem Version Actions CI Build status Maintainability Coverage Status

Open a file or text in a preferred terminal text editor.

TTY::Editor provides independent component for TTY toolkit.

Installation

Add this line to your application's Gemfile:

gem "tty-editor"

And then execute:

$ bundle

Or install it yourself as:

$ gem install tty-editor

Contents

1. Usage

To edit a file in a default text editor do:

TTY::Editor.open("/path/to/file")

To edit text in a default editor:

TTY::Editor.open(text: "Some text")

You can also open multiple existing and/or new files:

TTY::Editor.open("file_1", "file_2", "new_file_3")

Note that the VISUAL or EDITOR shell environment variables take precedence when auto detecting available editors.

You can also set your preferred editor command(s) and ignore VISUAL and EDITOR as well as other user preferences:

TTY::Editor.open("/path/to/file", command: "vim -f")

When VISUAL or EDITOR are not specified, a selection menu will be presented to the user.

For example, if an user has code, emacs and vim editors available on their system, they will see the following menu:

Select an editor?
  1) code
  2) emacs
  3) vim
  Choose 1-3 [1]:

You can further customise this behaviour with :prompt, :hide_menu, :enable_color and :menu_interrupt.

2. API

2.1 new

Instantiation of an editor will trigger automatic search for available command-line editors:

editor = TTY::Editor.new

You can change default search with the :command keyword argument.

2.1.1 :command

You can force to always use a specific editor by passing :command option:

editor = TTY::Editor.new(command: "vim")

Or you can specify multiple commands and give a user a choice:

editor = TTY::Editor.new(command: ["vim", "emacs"])

The class-level open method accepts the same parameters:

TTY::Editor.open("/path/to/file", command: "vim")

2.1.2 :env

Use :env key to forward environment variables to the text editor launch command:

TTY::Editor.new(env: {"FOO" => "bar"})

The class-level open method accepts the same parameters:

TTY::Editor.open("/path/to/file", env: {"FOO" => "bar"})

2.1.3 :raise_on_failure

By default when editor fails to open a false status is returned:

TTY::Editor.open("/path/to/unknown/file") # => false

Alternatively, you can use :raise_on_failure to raise an error on failure to open a file.

The TTY::Editor::CommandInvocationError will be raised anytime an editor fails to open a file:

editor = TTY::Editor.new(raise_on_failure: true)
editor.open("/path/to/unknown/file")
# => raises TTY::Editor::ComandInvocationError

2.1.4 :prompt

When more than one editor is available and user hasn't specified their preferred choice via VISUAL or EDITOR variables, a selection menu is presented.

For example, when code, emacs and vim executable exists on the system, the following menu will be displayed:

Select an editor?
  1) code
  2) emacs
  3) vim
  Choose 1-3 [1]:

If you would like to change the menu prompt use :prompt keyword:

editor = TTY::Editor.new(prompt: "Which one do you fancy?")
editor.open("/path/to/file")

This may produce the following in the terminal:

Which one do you fancy?
  1) code
  2) emacs
  3) vim
  Choose 1-3 [1]:

2.1.5 :hide_menu

When more than one editor is available from the default list, a selection menu will be displayed in the console:

Select an editor?
  1) code
  2) emacs
  3) vim
  Choose 1-3 [1]:

To hide the menu and automatically choose the first available editor use the :hide_menu keyword option:

editor = TTY::Editor.new(hide_menu: true)

2.1.6 :enable_color

An editor selection menu will display the first choice in colour on terminals that support colours. However, you can turn off colouring with the :enable_color keyword option:

editor = TTY::Editor.new(enable_color: false)

Equally, you can enforce the current menu choice to be always coloured:

editor = TTY::Editor.new(enable_color: true)

2.1.7 :menu_interrupt

When an editor selection menu gets interrupted by the Ctrl+C key, an InputInterrupt error is raised. To change this, provide the :menu_interrupt option with one of the following:

  • :error - raises InputInterrupt error
  • :exit - exits with 130 status code
  • :noop - skips handler
  • :signal - sends interrupt signal
  • proc - custom proc handler

For example, to immediately exit the menu and program do:

editor = TTY::Editor.new(menu_interrupt: :exit)

2.2 open

There is a class-level and instance-level open method. These are equivalent:

editor = TTY::Editor.new
editor.open(...)
# or
TTY::Editor.open(...)

Creating TTY::Editor instance means that the search for a command editor will be performed only once. Then the editor command will be shared between invocations of open call.

Conversely, the class-level open method will search for an editor each time it is invoked.

The following examples of using the open method apply to both the instance and class level invocations.

If you wish to open an editor without giving a file or content do:

TTY::Editor.open

To open a file, pass a path as an argument to open:

TTY::Editor.open("../README.md")
# => true

When editor successfully opens a file or content then true is returned, false otherwise.

You can change this with :raise_on_failure keyword to raise a TTY::Editor::CommandInvocation error when an editor cannot be opened.

In order to open text content inside an editor use :text keyword like so:

TTY::Editor.open(text: "Some text")

You can also provide filename that will be created with specified content before editor is opened:

TTY::Editor.open("/path/to/new-file", text: "Some text")

If you open a filename with already existing content then the new content will be appended at the end of the file.

You can also open multiple existing and non-existing files providing them as consecutive arguments:

TTY::Editor.open("file_1", "file_2", "new_file_3")

3. Default Editors

When an editor in EDITOR and VISUAL environment variables can't be found or isn't specified, a choice menu is displayed. The menu includes available editors from the default list of text editors:

  • Atom
  • Emacs
  • gedit
  • JED
  • Kate
  • Mg
  • Nano
  • Notepad
  • Pico
  • Sublime Text
  • TextMate
  • Vi
  • Vim
  • Visual Studio Code

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. 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. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-editor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the TTY::Editor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Copyright

Copyright (c) 2017 Piotr Murach. See LICENSE for further details.

tty-editor's People

Contributors

piotrmurach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jstoks dmproger

tty-editor's Issues

TTY::Editor.open(text: "") provides no way to access the editor text

Describe the problem

Using the TTY::Editor.open(text: "") API throws away the text.

Additionally that method is documented as # @return [Object], but actually it returns a boolean.

Steps to reproduce the problem

See description.

Actual behaviour

Text thrown away.

Expected behaviour

Either text returned or the editor instance (with text available via a reader) returned.

Describe your environment

  • OS version: Debian Bullseye
  • Ruby version: 3.2
  • TTY::Editor version: latest in RubyGems

TTY::Editor.open fails when EDITOR has spaces

Describe the problem

I'm on macOS and use MacVim for my editor. To make sure it's used everywhere, I set EDITOR to mvim -f which opens a new macvim window, and stays in the foreground until the editor closes. This is to keep

TTY::Editor.open fails in this case, which makes me think it's because of the space in it.

Steps to reproduce the problem

bundle exec irb -r tty-editor                                                                                                                   5.1s ๎‚ณ Thu Aug  8 20:10:23 2019
irb(main):001:0> TTY::Editor.open
TTY::Editor::CommandInvocationError: ` ''` failed with status: 127
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/tty-editor-0.5.1/lib/tty/editor.rb:205:in `open'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/tty-editor-0.5.1/lib/tty/editor.rb:94:in `open'
	from (irb):1
	from /Users/technicalpickles/.rbenv/versions/2.4.4/bin/irb:11:in `<top (required)>'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/cli/exec.rb:74:in `load'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/cli/exec.rb:74:in `kernel_load'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/cli/exec.rb:28:in `run'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/cli.rb:424:in `exec'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/cli.rb:27:in `dispatch'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/cli.rb:18:in `start'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/exe/bundle:30:in `block in <top (required)>'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bundler-1.16.4/exe/bundle:22:in `<top (required)>'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/bin/bundle:23:in `load'
	from /Users/technicalpickles/.rbenv/versions/2.4.4/bin/bundle:23:in `<main>'```

### Actual behaviour

/Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/tty-editor-0.5.1/lib/tty/editor.rb:205:in open': ''failed with status: 127 (TTY::Editor::CommandInvocationError) from /Users/technicalpickles/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/tty-editor-0.5.1/lib/tty/editor.rb:94:inopen'
from bin/pretend:94:in block in <main>' from bin/pretend:55:in loop'
from bin/pretend:55:in `

'


### Expected behaviour

Expect a new mvim editor to launch.

### Describe your environment

* OS version: 10.14.5
* Ruby version: 2.4.4p296
* TTY::Editor version: 0.5.1

could not find a text editor to use (when not specifying one)

Are you in the right place?

  • For issues or feature requests file a GitHub issue in this repository
  • For general questions or discussion post in Gitter

Describe the problem

TTY::Editor.new() fails with could not find a text editor to use. Please specify $VISUAL or $EDITOR or install one of the following editors: nano, notepad, vim, vi, emacs, code, subl, mate, atom, pico, qe, mg, jed.

Steps to reproduce the problem

      puts ENV["EDITOR"]   # prints /usr/local/bin/bbedit 
      editor = TTY::Editor.new() # causes TTY::Editor::EditorNotFoundError

Actual behaviour

TTY::Editor.new() fails with could not find a text editor to use. Please specify $VISUAL or $EDITOR or install one of the following editors: nano, notepad, vim, vi, emacs, code, subl, mate, atom, pico, qe, mg, jed.

Expected behaviour

I expect the editor instance to be created without error when use with my default text editor.
Note that is I specify an editor specifically, e.g., TTY::Editor.new(command:"bbedit") everything works. However I'm looking for cross-platform, editor-neutral behavior, this using this gem.

Describe your environment

  • OS version: macOS 11.3.1
  • Ruby version: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]
  • TTY::Editor version: 0.6.0

Ability to open editor by folder path (not file)

Describe the problem

I'm a sublime text user, and sublime allows me to specify subl path/to/folder/ and it will open a sublime editor with the folder as the working project.

How would the new feature work?

I would love for it to work as expected, currently, I am getting:

Don't know how to handle `path/to/folder`. Please provide a file path or content (ArgumentError)

Example usage

TTY::Editor.open(source.full_path)

Drawbacks

It is possible that some editors don't have this support. I am not aware of any, however, I don't play around with too many editors.

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.