Last active
August 29, 2015 14:01
-
-
Save jeanlescure/277d02948e3cb58400e5 to your computer and use it in GitHub Desktop.
Ruby Interactive Command Line Thread Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Interactive Command Line Thread Manager | |
# | |
# The following code allows you to manage multiple threads | |
# from a single command-line instance with fully | |
# customizeable commands | |
# | |
# @author = Jean M. Lescure | |
# @date = 2014/5/24 | |
COMMANDS = ['help','new','list','kill','describe'] | |
THREADS = [] | |
def help(*arg) | |
puts '' | |
puts 'Welcome to CLI Thread Manager' | |
puts '' | |
puts 'The following is a list of available commands:' | |
puts '' | |
COMMANDS.each do |command| | |
puts " #{command}" | |
end | |
end | |
def new(*arg) | |
args = arg[0] | |
THREADS << Thread.new{ | |
loop do; end | |
} | |
THREADS.last()[:description] = "Started at #{Time.now}" | |
puts 'Started new thread successfully!' | |
puts "\nid\t\tdescription\n#{THREADS.length-1}\t\t#{THREADS.last()[:description]}" | |
puts "\n(tip: to change a thread's description simply run 'describe [id] [your description]')" | |
end | |
def list(*arg) | |
id = 0 | |
puts "#{THREADS.length} threads in memory\n\nid\t\tstatus\t\tdescription" | |
THREADS.each do |thread| | |
puts "#{id}\t\t#{thread.status}\t\t#{thread[:description]}" | |
id += 1 | |
end | |
end | |
def kill(*arg) | |
args = arg[0] | |
index = args[0].to_i | |
if !THREADS[index].nil? | |
Thread.kill(THREADS.delete(THREADS[index])) | |
else | |
puts "ERROR! Unable to kill thread with id: #{index}" | |
puts '(Run "list" command to view running threads and their respective id numbers)' | |
end | |
end | |
def describe(*arg) | |
args = arg[0] | |
index = args.shift().to_i | |
if !THREADS[index].nil? | |
THREADS[index][:description] = args.join(' ') | |
else | |
puts "ERROR! No thread with id: #{index}" | |
puts '(Run "list" command to view running threads and their respective id numbers)' | |
end | |
end | |
print 'th-man> ' | |
while ARGF.gets | |
input = $_.split(' ') | |
command = input.shift | |
exit if command == 'exit' | |
if COMMANDS.include? command | |
method(command).call(input) | |
else | |
puts "Unknown command: #{command}" | |
end | |
print 'th-man> ' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment