class Howto::CLI

CLI class is responsible for handling command-line arguments.

Public Instance Methods

run(args = []) click to toggle source

CLI is used when running howto from command line (check bin/howto) file.

# File lib/howto/cli.rb, line 13
def run(args = [])
  if args.first == "note"
    require "fileutils"

    if Dir.exists?("notes")
      note_name = args[1]

      if note_name.nil?
        puts "Provide a name of your note, please"
      else
        note_file = note_file(note_name)

        if File.exists?(note_file)
          puts "This file already exists: #{note_file}! 🙀"
        else
          File.open(note_file, "w") do |file|
            template = File.read(File.join(Dir.pwd, "note_template.md"))
            template.gsub!("# HEADLINE", "# #{note_name}")

            file.puts(template)
          end

          system `code .`
          system `code #{note_file}`
        end
      end
    else
      puts "Hm, I don't see the \"notes\" folder 🤔"
    end

    0
  else
    puts "Hm, I don't know this command 🤔"
  end
end

Private Instance Methods

note_file(name) click to toggle source
# File lib/howto/cli.rb, line 59
def note_file(name)
  slug = name.downcase.split(" ").map { |w| w.gsub(/\W/, "") }.join("-")
  timestamp = Time.now.strftime("%Y%m%d%H%M")

  File.join(Dir.pwd, "notes", "#{timestamp}-#{slug}.md")
end
unindent(str) click to toggle source

This method is needed to unindent [“here document”](en.wikibooks.org/wiki/Ruby_Programming/Here_documents) help description.

# File lib/howto/cli.rb, line 55
def unindent(str)
  str.gsub(/^#{str.scan(/^[ \t]+(?=\S)/).min}/, "")
end