Class: Cheatorious::CLI
- Inherits:
-
Thor
- Object
- Thor
- Cheatorious::CLI
- Includes:
- Thor::Actions
- Defined in:
- lib/cheatorious/cli.rb
Instance Method Summary collapse
- #alias(name, cheatsheet) ⇒ Object
- #edit(cheatsheet) ⇒ Object
- #import(file) ⇒ Object
- #list ⇒ Object
- #reload(cheatsheet) ⇒ Object
- #remove(cheatsheet) ⇒ Object
- #search(cheatsheet, keyword = "") ⇒ Object
- #view(cheatsheet) ⇒ Object
- #writers ⇒ Object
Instance Method Details
#alias(name, cheatsheet) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/cheatorious/cli.rb', line 88 def alias(name, cheatsheet) ensure_workspace_exist if cheatsheet_list.include?(cheatsheet) puts "alias #{name}='cheatorious search #{cheatsheet}'" else puts "Invalid cheatsheet name: #{cheatsheet}" end end |
#edit(cheatsheet) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/cheatorious/cli.rb', line 98 def edit(cheatsheet) ensure_workspace_exist if cheatsheet_list.include?(cheatsheet) editor = %w( CHEATORIOUS_EDITOR BUNDLER_EDITOR VISUAL EDITOR ).map { |var| ENV[var] }.compact.first || 'vi' exec editor + " " + File.join(workspace_path, "originals", cheatsheet + ".rb") else puts "Invalid cheatsheet name: #{cheatsheet}. Use 'list' command to check the name." end end |
#import(file) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cheatorious/cli.rb', line 23 def import(file) ensure_workspace_exist name = File.basename(file, ".rb") return if cheatsheet_list.include?(name) && !yes?("Do you want to override the existent #{name} cheatsheet? (y/n)") if File.exist?(file) source = File.read(file) bytes = DslExecutor.module_eval("@output = :bytes\n"+source) File.open(File.join(workspace_path, "compiled", name), 'w') {|f| f.write(bytes) } File.open(File.join(workspace_path, "originals", name + ".rb"), 'w') {|f| f.write(source) } puts "Cheatsheet imported successfuly! Try 'cheatorious view #{name}'\nThe original cheatsheet file was copied to #{File.join(workspace_path, "originals", name + ".rb")}" else puts "The specified file doesn't exist: #{file}" end end |
#list ⇒ Object
16 17 18 19 20 |
# File 'lib/cheatorious/cli.rb', line 16 def list ensure_workspace_exist puts (cheatsheet_list.empty? ? "You don't have imported cheatsheets. See 'import' command." : "You have #{cheatsheet_list.size} cheatsheet(s):") puts cheatsheet_list.join("\n") end |
#reload(cheatsheet) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cheatorious/cli.rb', line 109 def reload(cheatsheet) ensure_workspace_exist if cheatsheet_list.include?(cheatsheet) source = File.read(File.join(workspace_path, "originals", cheatsheet + ".rb")) bytes = DslExecutor.module_eval("@output = :bytes\n"+source) File.open(File.join(workspace_path, "compiled", cheatsheet), 'w') {|f| f.write(bytes) } puts "Cheatsheet reloaded successfuly! Try 'cheatorious view #{cheatsheet}'" else puts "Invalid cheatsheet name: #{cheatsheet}. Use 'list' command to check the name." end end |
#remove(cheatsheet) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/cheatorious/cli.rb', line 122 def remove(cheatsheet) ensure_workspace_exist if cheatsheet_list.include?(cheatsheet) FileUtils.rm(File.join(workspace_path, "compiled", cheatsheet)) puts "Cheatsheet removed successfuly! To recover it execute 'import' command for " + File.join(workspace_path, "originals", cheatsheet + ".rb") else puts "Invalid cheatsheet name: #{cheatsheet}. Use 'list' command to check the name." end end |
#search(cheatsheet, keyword = "") ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cheatorious/cli.rb', line 50 def search(cheatsheet, keyword = "") ensure_workspace_exist writer = ["writer"] ? writer_for(["writer"]) : default_writer model = nil if cheatsheet_list.include?(cheatsheet) model = File.read(File.join(workspace_path, "compiled", cheatsheet)) elsif File.exist?(cheatsheet) model = DslExecutor.module_eval("@output = nil\n" + File.read(cheatsheet)) end if model puts Cheatorious::Search.execute(model, keyword, writer, .dup) else puts "Invalid cheatsheet name or file name: #{cheatsheet}" end end |
#view(cheatsheet) ⇒ Object
41 42 43 |
# File 'lib/cheatorious/cli.rb', line 41 def view(cheatsheet) invoke :search end |
#writers ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cheatorious/cli.rb', line 68 def writers ensure_workspace_exist if ["default"] if Cheatorious::Writer.constants.map {|c| c.to_s}.include?(["default"]) config = YAML.load(File.open(File.join(workspace_path, "config"))) config["default_writer"] = ["default"] File.open(File.join(workspace_path, "config"), "w") {|f| f.write(config.to_yaml) } puts "The default writer now is #{["default"]}" else puts "Invalid writer name, use 'cheatorious writers' to choose one from the available." end else puts "The following writers are available:\n" dw = default_writer.to_s puts Cheatorious::Writer.constants.map {|w| (dw.end_with?(w.to_s) ? w.to_s + " (default)" : w.to_s) }.join("\n") puts "\nUse -d option to set a default writer." end end |