Skip to content

Instantly share code, notes, and snippets.

@ploubser
Created May 29, 2012 11:29
Show Gist options
  • Save ploubser/2827931 to your computer and use it in GitHub Desktop.
Save ploubser/2827931 to your computer and use it in GitHub Desktop.
Curses generator
module MCollective
class Generator
attr_accessor :container, :iwindow, :dwindow, :owindow, :plugin
def initialize
@plugin = {:actions => [], :metadata => {}}
@container = Curses::Window.new(Curses.lines, Curses.cols, 0, 0)
@iwindow = Curses::Window.new(Curses.lines - 2, Curses.cols / 2, 1,0)
@dwindow = Curses::Window.new(Curses.lines - 2, Curses.cols / 2, 1, Curses.cols / 2)
@owindow = Curses::Window.new(1, Curses.cols, Curses.lines - 1, 0)
set_box(@iwindow, @dwindow)
Curses.curs_set(2)
display
end
def display_options_window
@owindow.clear
method_type = ""
if @plugin["type"] == "Data"
method_type = "Query"
elsif @plugin["type"] == "Agent"
method_type = "Action"
end
cols = 2
options = ["Re(n)ame Plugin", "(C)hange plugin type", "(A)dd #{method_type}",
"(R)emove #{method_type}", "(E)dit #{method_type}", "(G)enerate Plugin",
"Edit (M)etadata", "E(x)it"]
increment = (Curses.cols - options.join.size) / options.size
@owindow.addstr(options.join(" " * increment))
@owindow.refresh
end
def display
update_display_window
get_plugin_name
get_plugin_type
get_metadata
while(input = @iwindow.getch)
case input
when 99
get_plugin_type
when 110
get_plugin_name
when 97
create_action
when 120
Curses.close_screen
exit!
end
end
end
def get_plugin_type
@iwindow.clear
set_box(@iwindow)
reset_position(@iwindow)
@iwindow << "Choose plugin type"
move_down(@iwindow)
@iwindow << "------------------"
move_down(@iwindow)
@iwindow << "1) Data Plugin"
move_down(@iwindow)
@iwindow << "2) Agent Plugin"
move_down(@iwindow)
@iwindow.refresh
while(input = @iwindow.getch)
if input == 49
@plugin["type"] = "Data"
break
elsif input == 50
@plugin["type"] = "Agent"
break
end
end
reset_window(@iwindow)
display_options_window
update_display_window
end
def get_plugin_name
@iwindow.clear
set_box(@iwindow)
reset_position(@iwindow)
@iwindow << "Enter plugin name : "
@plugin["name"] = @iwindow.getstr
update_display_window
reset_window(@iwindow)
end
def get_metadata
clear_iwindow
[:name, :description, :author, :license, :version, :url, :timeout].each do |item|
@iwindow << "Enter plugin #{item} : "
@plugin[:metadata][item] = @iwindow.getstr
move_down(@iwindow)
end
reset_window(@iwindow)
update_display_window
end
def create_action
action = {:inputs => [], :outputs => []}
@iwindow.clear
set_box(@iwindow)
reset_position(@iwindow)
@iwindow << "Enter Action Name : "
action["name"] = @iwindow.getstr
move_down(@iwindow)
@iwindow << "1) Add input"
move_down(@iwindow)
@iwindow << "2) Add output"
move_down(@iwindow)
@iwindow << "3) Save Action"
move_down(@iwindow)
while(input = @iwindow.getch)
if input == 49
action[:inputs] << create_input
elsif input == 50
action[:outputs] << create_output
elsif input == 51
@plugin[:actions] << action
break
end
end
update_display_window
reset_window(@iwindow)
end
def update_display_window
@dwindow.clear
set_box(@dwindow)
@plugin.keys.each do |value|
unless value.is_a? Symbol
move_down(@dwindow)
@dwindow << "Plugin #{value.capitalize} : #{@plugin[value]}"
end
end
move_down(@dwindow)
move_down(@dwindow)
unless @plugin[:metadata].empty?
@dwindow << "Plugin Meta Data"
@plugin[:metadata].each do |k, v|
move_down(@dwindow)
@dwindow << " #{k.to_s.capitalize} : #{v}"
end
end
@dwindow.refresh
end
def set_box(*window)
window.each do |w|
w.box(?|, ?-)
end
end
def reset_position(window)
window.setpos(2,2)
end
def reset_window(window)
window.clear
set_box(window)
reset_position(window)
window << "Please select an option from the options list"
move_down(window)
window << "---------------------------------------------"
move_down(window)
end
def move_down(window)
window.setpos(window.cury + 1, 2)
end
def clear_iwindow
@iwindow.clear
set_box(@iwindow)
reset_position(@iwindow)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment