diff --git a/README b/README deleted file mode 100644 index a249966..0000000 --- a/README +++ /dev/null @@ -1,55 +0,0 @@ -CLI FRAMEWORK - -CLI Framework is a basic command line interface framework containing methods for layout. - -instant_gets() - Take one key as feedback without pressing Enter - ----- @menu_choice = instant_gets - -indent() - Indent content x number of spaces - Pass in your line of content as the first argument - Optional: pass in number of space to indent by - Change 43 to something else to change default - ----- indent("Main Menu", 20) - -blank_indent() - Indent line without line break - Useful in conjunction with instant_gets (needs to be 1 space more than regular indent for this) - ----- indent("Main Menu", 20) - ----- blanck_indent(21) - ----- instant_gets - -br() - Line break - Optional: pass in number of line breaks - ----- indent("Main Menu", 20) - ----- br(2) - ----- blanck_indent(21) - ----- instant_gets - -hr() - Horizontal rule - Optional: pass in "=" for double line instead of single - ----- hr(=) - ----- indent("Main Menu", 20) - ----- br(2) - ----- blanck_indent(21) - ----- instant_gets - -invalid_command() - Invalid command warning - Uses br and indent to format - Useful for menus in conjunction with case statements/until loops - ----- until menu_choice = "q" - ----- case menu_choice - ----- when "1" - ----- some_action - ----- when "2" - ----- some_other_action - ----- when "q" - ----- menu_choice - ----- else - ----- invalid_command - ----- end - ----- end \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..347007e --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +#CLI FRAMEWORK + +CLI Framework is a basic command line interface framework containing methods for layout. + +###instant_gets() +* Take one key as feedback without pressing Enter +```ruby +@menu_choice = instant_gets +``` + +###indent() +* Indent content x number of spaces +* Pass in your line of content as the first argument and the number of spaces that you would like to indent by as your second argument. Note that the default number of spaces is set to 30 making the second argument optional. +```ruby +indent("Main Menu", 20) +``` + +###blank_indent() +* Indent line without line break +* Useful in conjunction with instant_gets (needs to be 1 space more than regular indent for this) +```ruby +indent("Main Menu", 20) +blank_indent(21) +instant_gets +``` + +###centre() +* Centre content in a line. +* Default screen width is 80 but this is optional +```ruby +centre("Main Menu", 80) +``` + +###br() +* Line break +* Optional: pass in number of line breaks +```ruby +indent("Main Menu", 20) +br(2) +blank_indent(21) +instant_gets +``` + +###hr() +* Horizontal rule +* Optional: pass in "=" for double line instead of single +```ruby +hr(=) +indent("Main Menu", 20) +br(2) +blank_indent(21) +instant_gets +``` + +###invalid_command() +* Invalid command warning +* Uses br and indent to format +* Useful for menus in conjunction with case statements/until loops +```ruby + until menu_choice = "q" + case menu_choice + when "1" + some_action + when "2" + some_other_action + when "q" + menu_choice + else + invalid_command + end + end +``` diff --git a/cli_framework.rb b/cli_framework.rb index 8e66c50..ce8f840 100644 --- a/cli_framework.rb +++ b/cli_framework.rb @@ -7,12 +7,12 @@ def instant_gets end end -def indent(content, spaces = "43") - printf "%#{spaces}s %s\n", " ", "#{content}" +def indent(content, spaces = 30) + print "#{" "*spaces} #{content}" end -def blank_indent(spaces = "44") - printf "%#{spaces}s", " " +def blank_indent(spaces = 44) + print "#{" "*spaces}" end def br(breaks = 1) @@ -21,20 +21,29 @@ def br(breaks = 1) end end +def centre(content, width = 80) + if content.length < width + indent_length = (width / 2.0) - (content.length / 2.0) + else + indent_length = width + end + " "*indent_length + content +end + def hr(type = "-") if type == "=" - indent("============================================================================================== ") + ("="*80)+"\n" else - indent("---------------------------------------------------------------------------------------------- ") + ("-"*80)+"\n" end end def invalid_command br - indent("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - indent("!! !!") - indent("!! INVALID COMMAND! !!") - indent("!! !!") - indent("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + indent("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",10) + indent("!! !!",10) + indent("!! INVALID COMMAND! !!", 10) + indent("!! !!", 10) + indent("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", 10) br(2) -end \ No newline at end of file +end