Code Monkey home page Code Monkey logo

Comments (7)

janko avatar janko commented on June 7, 2024

I just found out that there is IO.raw, which both turns off the echo and does the "character" input. It also resets it back at the end of the block.

require "io/console"

IO.raw do |stdin|
  p stdin.getc
end

from tty.

janko avatar janko commented on June 7, 2024

I discovered another thing, IO#readpartial, which perfectly solved my problem.

require "io/console"

loop do
  keypress = STDIN.raw { |stdin| stdin.readpartial(3) }

  case keypress
  when "\e[A" then puts "Up"
  when "\e[B" then puts "Down"
  when "\e[C" then puts "Right"
  when "\e[D" then puts "Left"
  when "\x03" # Ctrl-C
    raise Interrupt
  when "\x04" # Ctrl-D
    exit
  end
end

IO#readpartial(n) reads at most n characters from the IO, and sees a "keypress" as a single input, which was exactly what I wanted. Maybe this is what you want to use in your gem, a "keypress" instead of a "character", because it's much more powerful (each "character" is a "keypress" as well).

from tty.

piotrmurach avatar piotrmurach commented on June 7, 2024

Hi Janko,

Great that you are giving this lib a go! Before I talk about this issue, my goal is for this library to become modular so that individual components can become much more robust and basically stand on their own merits. I realised I was short sighted trying to lump everything into one big blob. I have extracted few components such as pastel that I think got significantly better and got major issues fixed.

Regarding terminal queries and user input, I'm gonna extract that to separate component that going forward will allow for asking robust questions such as choose an option from menu, yes/no and basically nice way to gather input. I will work on it this coming weekend.

Usage of 'io/console' is good, however I found that for instance the JRuby doesn't support it. But obviously that's not a show stopper. I'd like to work with you on getting the interface to work intuitively. I'm open to suggestions and of course PRs. Agree that `shell.ask('...').read_keypress would better explain the intention. We could also provide some callback system that would yield on each key.

Any thoughts?

Best

Piotr

from tty.

janko avatar janko commented on June 7, 2024

Yeah, it's great that you started separating, now people can just use gems like pastel standalone (which probably has a frequent use case; I needed once just the coloring, for a custom logger).

That's great, I would like a separate #read_keypress, and then #read_char doesn't need changing, because it actually does what it's supposed to. I don't think that #read_keypress needs a callback (because it's blocking), it will just return when the user presses a key (returning that key). And if people want to wait for more keypresses, they could just put it in a loop, right?

One more thing that I wanted to ask, what if the key pressed is Ctrl-C? Because inside of STDIN.raw Ctrl-C won't terminate the process, it will just be treated as a regular keypress. My intuition says that we should just leave it to the programmer to rescue that case, since maybe he would like to handle Ctrl-C differently? But then again, if we raise an Interrupt error (it's what Ruby raises on Ctrl-C), then the user can also just rescue it. What do you think we should do?

from tty.

piotrmurach avatar piotrmurach commented on June 7, 2024

Agree with your point of separating the two methods. Also, agree that blocking io doesn't need callbacks. I suppose I was thinking of more general approach, for instance, allow users to subscribe to keepress to accomplish things like autocompletion, but that's besides this point.

From user point of view I imagine that Ctrl-C should bum you out of the client, that would be at least my expectation. Thus I wouldn't do anything special but stay consistent and throw an error and let developer handle that. Also, I would probably be tempted and make this behaviour the default for all the other methods. Thoughts?

from tty.

janko avatar janko commented on June 7, 2024

All other methods will already throw an error on Ctrl-C, it's just when you enable the "raw" method with IO#raw that this gets disabled. Ok, enough philosophing, I will work on the PR now 😃

from tty.

piotrmurach avatar piotrmurach commented on June 7, 2024

Hi @janko-m, just to let you know that I've released v0.1.2 version with your code. The release includes necromancer that helps parsing user input and will help with extraction of shell component later on. In future people will be able to use just the shell interface without other dependencies but I need to extract a bit more. I will keep your posted as I would like your input/opinion. Btw, I like your tic-tac-toe curses usage.

from tty.

Related Issues (20)

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.