RubyKaigi 2018

TTY - Ruby alchemist’s secret potion

What if you were told that there is a set of simple and potent gems developed to exponentially increase productivity when building modern terminal
applications such as Bundler, in next to no time? Curious about how you can harness this power and become a command line applications alchemist?

RubyKaigi 2018 https://rubykaigi.org/2018/presentations/piotr_murach

RubyKaigi 2018

00:00:00 Thank you very much for your presence. Arigatou gozaimasu.
00:00:11 I am very excited to be here at RubyKaigi 2018. Today, I would like to share some insights into the world of Ruby and introduce you to a gem I have created, TTY.
00:00:30 As software engineers, we often don't have a standard approach when building terminal applications. Unlike Rails, which has set standards for web applications, terminal development lacks a similar guideline.
00:00:51 The question arises, is there a standard for creating terminal applications with Ruby? We have the POSIX standard and UNIX terminals, but the ease of creating terminal applications is not obvious.
00:01:10 To address this issue, I created the TTY gem. This gem is designed to help simplify the process of building terminal applications because there is no single, easy way to create them, much like Rails does for web applications.
00:01:32 TTY is a meta-gem, meaning you wouldn't use it directly in your application, similar to how you wouldn’t use Rails directly. It's available on GitHub, and I encourage you to check it out and give it a star!
00:01:50 Before diving deeper into TTY, I'd like to discuss the guiding principles behind its development. If you have used Rails, the principle of convention over configuration should be familiar. TTY has similar principles.
00:02:10 The aim is to provide a place for you to document your commands and functionalities, making the development of internal applications intuitive without requiring extensive thought about structure.
00:02:30 There is a sweet spot for TTY in developing terminal applications. While you might not need it for a small script, when managing multiple concepts and dependencies, TTY can help organize your commands efficiently.
00:03:08 For instance, if you look into Bundler’s source code, you'll see much complexity involved in managing various concepts and subcommands.
00:03:30 Subcommands are another great use case for TTY, as terminal applications can grow in complexity, requiring a structure where each command has its own file.
00:03:55 Starting with TTY is straightforward: you simply run 'gem install TTY'. I've released this gem today, and you can install it directly from your command line.
00:04:15 TTY is built on top of Bundler, which is a great way for sharing Ruby code. Even if you do not plan to publish your dependency on RubyGems, it's worthwhile to structure your code as a gem to facilitate sharing with your colleagues.
00:04:35 The TTY gem scaffolds a basic Ruby setup with two main commands: 'new' and 'add'. If you want to start building your applications, type 'tty new' followed by the name of your application.
00:04:55 This command will create the necessary modules and directories for you, providing an output setup that includes a 'commands' directory and a 'CLI.rb' file as the entry point.
00:05:14 In the generated files, 'CLI.rb' functions similarly to a router in Rails; it figures out which commands to execute based on input.
00:05:35 Each command acts as a controller, processing options and managing the input/output cycle, crucial for standard UNIX terminology.
00:05:55 The templates provided in TTY were designed to simplify generating commands. These are actually ERB templates which provide a helpful good start for creating your command line applications.
00:06:15 Let’s explore some files that would be interesting for you to see. First, you'll find a set of dependencies included in your project. You are free to add or remove libraries as needed.
00:06:38 You are not bound to use only TTY; it's flexible and allows integration with any other libraries that suit your needs.
00:07:05 The main CLI entry point only requires Thor, a helpful gem, and provides the 'version' command as the first feature.
00:07:22 This command uses Thor's methods for defining and mapping commands, giving your CLI application a structured output.
00:07:45 If you want to add new commands, TTY makes it simple through the 'help add' command, which displays options for adding features.
00:08:01 You can add required arguments and describe each command for clear and organized output. The goal here is to ensure that the commands created are as intuitive as possible.
00:08:21 As you add commands, TTY facilitates the creation of necessary files for handling those commands, including unit tests, which ensure your commands work as expected.
00:08:41 When looking at the command structure, you'll notice a focus on minimizing complexity while ensuring that your application can grow as needed.
00:09:04 The TTY CLI can support various command options and allows for easy instantiation of commands with required parameters.
00:09:26 Let's move on to reviewing how commands execute and interact with the contextual data you define within your application.
00:09:48 Now I’ll present a few useful libraries TTY offers, like config management, which simplifies the handling of nested configuration, providing a minimal API that feels intuitive for developers.
00:10:13 The configuration file is typically straightforward, supporting various file types as extensions such as JSON or YAML, enhancing usability.
00:10:33 When you run your command, it takes into account existing configurations and can read from them or create new entries as necessary.
00:10:50 Any changes made to the configuration are preserved the next time the command is executed, ensuring persistence in your command line application.
00:11:11 Let's see how you can set up a command using the following configuration settings, showcasing its power in building terminal applications.
00:11:35 Using predefined commands, TTY allows for clear interaction patterns where options are available for the developer without overwhelming them.
00:11:54 With this framework in place, you can design applications like coin pair, which facilitates interactions in the cryptocurrency world smoothly.
00:12:12 Ultimately, I hope you've gotten a taste of the power of TTY libraries and how they can simplify your command line applications.
00:12:28 As we can see, TTY configurations and commands offer a structured way to manage complex workflows within terminal applications.
00:12:43 Now, let’s summarize what we learned and dive into the practical demonstration of building a terminal application using TTY.
00:13:04 After exploring the capabilities of TTY, I'm excited to show you a project I've developed, Coin Pair, which manages cryptocurrency holdings.
00:13:20 When you run Coin Pair, you'll see prompts that request information about currency and exchange preferences, making it user-friendly.
00:13:37 As you input information, Coin Pair will store configurations and manage the interactions automatically, allowing for seamless user experience.
00:13:54 It's impressive how quickly you can set up an application with TTY's help, as it takes care of the boilerplate, letting you focus on logic.
00:14:12 Moving forward, I believe TTY libraries can handle your command needs efficiently while also supporting complex data configurations.
00:14:31 Thank you for your time, and I hope you've enjoyed this overview of TTY. I appreciate the opportunity to engage with all of you at RubyKaigi.
00:14:49 Please feel free to reach out if you have any questions or feedback. Thank you!