{"id":25714027,"url":"https://github.com/piotrmurach/tty-editor","last_synced_at":"2025-05-01T08:42:56.446Z","repository":{"id":59158170,"uuid":"60019335","full_name":"piotrmurach/tty-editor","owner":"piotrmurach","description":"Opens a file or text in the user's preferred editor","archived":false,"fork":false,"pushed_at":"2025-04-28T19:15:51.000Z","size":155,"stargazers_count":28,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-28T20:26:43.932Z","etag":null,"topics":["cli","console","edittext","ruby","ruby-gem","terminal","tty","tty-components"],"latest_commit_sha":null,"homepage":"https://ttytoolkit.org","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piotrmurach.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"piotrmurach"}},"created_at":"2016-05-30T15:12:59.000Z","updated_at":"2025-04-28T19:15:55.000Z","dependencies_parsed_at":"2024-01-30T23:55:32.529Z","dependency_job_id":"cfc9599c-a837-4a2e-a164-4e063ca10c8b","html_url":"https://github.com/piotrmurach/tty-editor","commit_stats":{"total_commits":212,"total_committers":1,"mean_commits":212.0,"dds":0.0,"last_synced_commit":"fa1d9995512dfe4ab64d3a6b42008fcee323aede"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-editor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-editor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-editor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-editor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotrmurach","download_url":"https://codeload.github.com/piotrmurach/tty-editor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251385033,"owners_count":21581153,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","console","edittext","ruby","ruby-gem","terminal","tty","tty-components"],"created_at":"2025-02-25T12:27:52.630Z","updated_at":"2025-05-01T08:42:56.412Z","avatar_url":"https://github.com/piotrmurach.png","language":"Ruby","readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://ttytoolkit.org\"\u003e\u003cimg width=\"130\" src=\"https://github.com/piotrmurach/tty/raw/master/images/tty.png\" alt=\"TTY Toolkit logo\" /\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n# TTY::Editor\n\n[![Gem Version](https://badge.fury.io/rb/tty-editor.svg)][gem]\n[![Actions CI](https://github.com/piotrmurach/tty-editor/actions/workflows/ci.yml/badge.svg)][gh_actions_ci]\n[![Build status](https://ci.appveyor.com/api/projects/status/yw4guy16meq5wkee?svg=true)][appveyor]\n[![Maintainability](https://api.codeclimate.com/v1/badges/0afb9e75eef4ae4615c6/maintainability)][codeclimate]\n[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-editor/badge.svg)][coverage]\n\n[gem]: https://badge.fury.io/rb/tty-editor\n[gh_actions_ci]: https://github.com/piotrmurach/tty-editor/actions/workflows/ci.yml\n[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-editor\n[codeclimate]:https://codeclimate.com/github/piotrmurach/tty-editor/maintainability\n[coverage]: https://coveralls.io/github/piotrmurach/tty-editor\n\n\u003e Open a file or text in a preferred terminal text editor.\n\n**TTY::Editor** provides independent component for [TTY](https://github.com/piotrmurach/tty) toolkit.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"tty-editor\"\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install tty-editor\n\n## Contents\n\n* [1. Usage](#1-usage)\n* [2. API](#2-api)\n  * [2.1 new](#21-new)\n    * [2.1.1 :command](#211-command)\n    * [2.1.2 :env](#212-env)\n    * [2.1.3 :raise_on_failure](#213-raise_on_failure)\n    * [2.1.4 :prompt](#214-prompt)\n    * [2.1.5 :hide_menu](#215-hide_menu)\n    * [2.1.6 :enable_color](#216-enable_color)\n    * [2.1.7 :menu_interrupt](#217-menu_interrupt)\n  * [2.2 open](#22-open)\n* [3. Default Editors](#3-default-editors)\n\n## 1. Usage\n\nTo edit a file in a default text editor do:\n\n```ruby\nTTY::Editor.open(\"/path/to/file\")\n```\n\nTo edit text in a default editor:\n\n```ruby\nTTY::Editor.open(text: \"Some text\")\n```\n\nYou can also open multiple existing and/or new files:\n\n```ruby\nTTY::Editor.open(\"file_1\", \"file_2\", \"new_file_3\")\n```\n\nNote that the `VISUAL` or `EDITOR` shell environment variables take precedence when auto detecting available editors.\n\nYou can also set your preferred editor command(s) and ignore `VISUAL` and `EDITOR` as well as other user preferences:\n\n```ruby\nTTY::Editor.open(\"/path/to/file\", command: \"vim -f\")\n```\n\nWhen `VISUAL` or `EDITOR` are not specified, a selection menu will be presented to the user.\n\nFor example, if an user has `code`, `emacs` and `vim` editors available on their system, they will see the following menu:\n\n```\nSelect an editor?\n  1) code\n  2) emacs\n  3) vim\n  Choose 1-3 [1]:\n```\n\nYou can further customise this behaviour with [:prompt](#214-prompt), [:hide_menu](#215-hide_menu), [:enable_color](#216-enable_color) and [:menu_interrupt](#217-menu_interrupt).\n\n## 2. API\n\n### 2.1 new\n\nInstantiation of an editor will trigger automatic search for available command-line editors:\n\n```ruby\neditor = TTY::Editor.new\n```\n\nYou can change default search with the `:command` keyword argument.\n\n#### 2.1.1 :command\n\nYou can force to always use a specific editor by passing `:command` option:\n\n```ruby\neditor = TTY::Editor.new(command: \"vim\")\n```\n\nOr you can specify multiple commands and give a user a choice:\n\n```ruby\neditor = TTY::Editor.new(command: [\"vim\", \"emacs\"])\n```\n\nThe class-level `open` method accepts the same parameters:\n\n```ruby\nTTY::Editor.open(\"/path/to/file\", command: \"vim\")\n```\n\n#### 2.1.2 :env\n\nUse `:env` key to forward environment variables to the text editor launch command:\n\n```ruby\nTTY::Editor.new(env: {\"FOO\" =\u003e \"bar\"})\n```\n\nThe class-level `open` method accepts the same parameters:\n\n```ruby\nTTY::Editor.open(\"/path/to/file\", env: {\"FOO\" =\u003e \"bar\"})\n```\n\n#### 2.1.3 :raise_on_failure\n\nBy default when editor fails to open a `false` status is returned:\n\n```ruby\nTTY::Editor.open(\"/path/to/unknown/file\") # =\u003e false\n```\n\nAlternatively, you can use `:raise_on_failure` to raise an error on failure to open a file.\n\nThe `TTY::Editor::CommandInvocationError` will be raised anytime an editor fails to open a file:\n\n```ruby\neditor = TTY::Editor.new(raise_on_failure: true)\neditor.open(\"/path/to/unknown/file\")\n# =\u003e raises TTY::Editor::ComandInvocationError\n```\n\n#### 2.1.4 :prompt\n\nWhen more than one editor is available and user hasn't specified their preferred choice via `VISUAL` or `EDITOR` variables, a selection menu is presented.\n\nFor example, when `code`, `emacs` and `vim` executable exists on the system, the following menu will be displayed:\n\n```\nSelect an editor?\n  1) code\n  2) emacs\n  3) vim\n  Choose 1-3 [1]:\n```\n\nIf you would like to change the menu prompt use `:prompt` keyword:\n\n```ruby\neditor = TTY::Editor.new(prompt: \"Which one do you fancy?\")\neditor.open(\"/path/to/file\")\n```\n\nThis may produce the following in the terminal:\n\n```\nWhich one do you fancy?\n  1) code\n  2) emacs\n  3) vim\n  Choose 1-3 [1]:\n```\n\n#### 2.1.5 :hide_menu\n\nWhen more than one editor is available from the default list, a selection menu will be displayed in the console:\n\n```\nSelect an editor?\n  1) code\n  2) emacs\n  3) vim\n  Choose 1-3 [1]:\n```\n\nTo hide the menu and automatically choose the first available editor use the `:hide_menu` keyword option:\n\n```ruby\neditor = TTY::Editor.new(hide_menu: true)\n```\n\n#### 2.1.6 :enable_color\n\nAn 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:\n\n```ruby\neditor = TTY::Editor.new(enable_color: false)\n```\n\nEqually, you can enforce the current menu choice to be always coloured:\n\n```ruby\neditor = TTY::Editor.new(enable_color: true)\n```\n\n### 2.1.7 :menu_interrupt\n\nWhen 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:\n\n* `:error` - raises `InputInterrupt` error\n* `:exit` - exits with `130` status code\n* `:noop` - skips handler\n* `:signal` - sends interrupt signal\n* `proc` - custom proc handler\n\nFor example, to immediately exit the menu and program do:\n\n```ruby\neditor = TTY::Editor.new(menu_interrupt: :exit)\n```\n\n### 2.2 open\n\nThere is a class-level and instance-level `open` method. These are equivalent:\n\n```ruby\neditor = TTY::Editor.new\neditor.open(...)\n# or\nTTY::Editor.open(...)\n```\n\nCreating `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.\n\nConversely, the class-level `open` method will search for an editor each time it is invoked.\n\nThe following examples of using the `open` method apply to both the instance and class level invocations.\n\nIf you wish to open an editor without giving a file or content do:\n\n```ruby\nTTY::Editor.open\n```\n\nTo open a file, pass a path as an argument to `open`:\n\n```ruby\nTTY::Editor.open(\"../README.md\")\n# =\u003e true\n```\n\nWhen editor successfully opens a file or content then `true` is returned, `false` otherwise.\n\nYou can change this with `:raise_on_failure` keyword to raise a `TTY::Editor::CommandInvocation` error when an editor cannot be opened.\n\nIn order to open text content inside an editor use `:text` keyword like so:\n\n```ruby\nTTY::Editor.open(text: \"Some text\")\n```\n\nYou can also provide filename that will be created with specified content before editor is opened:\n\n```ruby\nTTY::Editor.open(\"/path/to/new-file\", text: \"Some text\")\n```\n\nIf you open a filename with already existing content then the new content will be appended at the end of the file.\n\nYou can also open multiple existing and non-existing files providing them as consecutive arguments:\n\n```ruby\nTTY::Editor.open(\"file_1\", \"file_2\", \"new_file_3\")\n```\n\n## 3. Default Editors\n\nWhen 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:\n\n* `Atom`\n* `Emacs`\n* `gedit`\n* `JED`\n* `Kate`\n* `Mg`\n* `Nano`\n* `Notepad`\n* `Pico`\n* `Sublime Text`\n* `TextMate`\n* `Vi`\n* `Vim`\n* `Visual Studio Code`\n\n## Development\n\nAfter 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.\n\nTo 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](https://rubygems.org).\n\n## Contributing\n\nBug 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](https://github.com/piotrmurach/tty-editor/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the TTY::Editor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/tty-editor/blob/master/CODE_OF_CONDUCT.md).\n\n## Copyright\n\nCopyright (c) 2017 Piotr Murach. See LICENSE for further details.\n","funding_links":["https://github.com/sponsors/piotrmurach"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Ftty-editor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotrmurach%2Ftty-editor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Ftty-editor/lists"}