Scans an array of arrays using the keyboard in Ruby

I am trying to make a cli application in Ruby that takes a given array and then displays it as a list that I can view with the arrow keys.

It seems to me that I have already seen a library in Ruby that does this already, but I cannot remember its name.

I am trying to reverse engineer the code from soundcloud2000 to do something similar, but its code is closely related to using the Soundcloud API.

I know about the gem curse, I think with something with a larger abstraction .Ad

Has anyone seen a library that does this, or some kind of proof of concept Ruby code that could do this?

+5
source share
2 answers

I don’t know, this is what you are looking for, but maybe you can use my idea.

Since I no longer have information about what you are trying to accomplish, what is your input, etc., this example will be very simple.

Let's say we have a class to work with called PlaylistDemo, which will create a playlist with songs:

class PlaylistDemo attr_accessor :position def initialize songs @playlist = Array.new songs.each { |song| @playlist << song } @position = 0 end def show_playlist @playlist.each_with_index.map do |song, index| position == index ? "[#{song}]" : " #{song} " end end end 

Prepare some songs:

 # From billboard.com songs = [ "Taylor Swift - Blank Space", "Mark Ronson Featuring Bruno Mars - Uptown Funk!", "Hozier - Take Me To Church", "Meghan Trainor - Lips Are Movin", "Meghan Trainor - All About That Bass" ] 

And go ahead and create an object:

 pd = PlaylistDemo.new(songs) 

Now my idea is to use dispel to manipulate the position and see exactly where you are (and update the "user interface" accordingly).

For this, I prepared a function that will make the user interface for your CLI application:

 def show_ui playlist_obj ["\n", playlist_obj.show_playlist, "\nCurrent position: #{playlist_obj.position + 1} "].join("\n") end 

The final piece of code:

 Dispel::Screen.open do |screen| screen.draw show_ui(pd) Dispel::Keyboard.output do |key| case key when :up then pd.position -= 1 when :down then pd.position += 1 when "q" then break end screen.draw show_ui(pd) end end 

You can also use colorize , but for this you need puts .

Please, not that I did not set a limit for the position, as this is just an example.

See my example here:

cAn6hn3.png

Full code: http://paste.debian.net/139651/

+6
source

You are looking for curses ruby stone.

An example of a menu created by @phoet.

 require "curses" include Curses init_screen start_color noecho def draw_menu(menu, active_index=nil) 4.times do |i| menu.setpos(i + 1, 1) menu.attrset(i == active_index ? A_STANDOUT : A_NORMAL) menu.addstr "item_#{i}" end end def draw_info(menu, text) menu.setpos(1, 10) menu.attrset(A_NORMAL) menu.addstr text end position = 0 menu = Window.new(7,40,7,2) menu.box('|', '-') draw_menu(menu, position) while ch = menu.getch case ch when 'w' draw_info menu, 'move up' position -= 1 when 's' draw_info menu, 'move down' position += 1 when 'x' exit end position = 3 if position < 0 position = 0 if position > 3 draw_menu(menu, position) end 
+2
source

Source: https://habr.com/ru/post/1210371/


All Articles