Class: Snip::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/snip.rb

Instance Method Summary collapse

Instance Method Details

#argv_ext_check(pos) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/snip.rb', line 8

def argv_ext_check(pos)
  if ARGV[pos]
    (ARGV[pos] == "rb" || ARGV[pos] == "js" || ARGV[pos] == "erb")
  else
    false
  end
end

#display_fileObject



16
17
18
# File 'lib/snip.rb', line 16

def display_file
  DestinationFileWriter.return_display_file
end

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/snip.rb', line 20

def execute
  unless ARGV[0]
    abort(ViewFormatter.no_args_message)
  end

  if ARGV[0] == "--help"
    abort(ViewFormatter.terminal_help_message)
  end

  if ARGV[0] == "-f"
    if !ARGV[1].nil?
      if File.directory?(ARGV[1])
        DestinationFileWriter.save_file_path_to_config_file(ARGV[1])
        abort(ViewFormatter.new_file_path)
      else
        abort(ViewFormatter.need_to_specify_directory)
      end
    else
      abort(ViewFormatter.specify_filename((DestinationFileWriter.snip_filepath)))
    end
  end

  if ARGV[0] == "-l" 
    puts ViewFormatter.show_log(DestinationFileWriter.log_filepath)
    abort
  end

  if ARGV[0] == "-i"
    DestinationFileWriter.reindex_all
    abort(ViewFormatter.reindexed)
  end

  if ARGV[0] == "-w"
    DestinationFileWriter.restore_whitespace(DestinationFileWriter.return_display_file, SearchDisplay.run(DestinationFileWriter.return_display_file))
    abort("Whitespace restored")
  end

  if ARGV[0] == "--delete"
    unless ARGV[1]
      abort("Please specify which snippets you wish to delete")
    end
    if ARGV[1].include?(",")
      ids = ARGV[1].split(",")
    else
      ids = [ARGV[1]]
    end
    DestinationFileWriter.rewrite_file(display_file, SearchDisplay.delete(display_file, ids))
    DestinationFileWriter.reindex_all
    abort("Snips #{ARGV[1]} deleted from my_snips.txt (other files must be modified manually)")
  end

  if ARGV[0] == "-d"
    if argv_ext_check(1)
      if ARGV[2]
        if !ARGV[3].nil?
          abort(ViewFormatter.display_error)
        else
          puts CommandLineController.display_search(ARGV[2], "." + ARGV[1])
        end
      else
        puts CommandLineController.display_search("", "." + ARGV[1])
      end
    elsif ARGV[1] == "."
      puts ViewFormatter.show_snips(DestinationFileWriter.snip_filepath)
    elsif !ARGV[1].nil?
      if !ARGV[2].nil?
        abort(ViewFormatter.display_error)
      else
        puts CommandLineController.display_search(ARGV[1], "")
      end 
    else
      abort(ViewFormatter.display_error)
    end
    abort
  end

  DestinationFileWriter.check_config_file_for_snip_file

  if ARGV[0] == "-c"
    code = Clipboard.paste + "\n"
    
    if !ARGV[2].nil? && (ARGV[1] == "rb" || ARGV[1] == "js" || ARGV[1] == "erb" || ARGV[1] == "misc")
      type = ARGV[1]
      title = ARGV[2]
    elsif ARGV[1].nil?
      puts "Type of code (e.g. rb, js, erb) or press enter for misc:"
      type = $stdin.gets.chomp
      puts "Title:"
      title = $stdin.gets.chomp
    else
      abort(ViewFormatter.clipboard_command)
    end

    if type == "rb" || type == "js" || type == "erb"
      origin = "clipboard (.#{type})"
    else
      origin = "clipboard"
    end

    Snippet.new(code: code, title: title, line: nil, filename: origin)
    CommandLineController.file_writing
    abort("Your code has been snipped:\n\n#{code}")
  end

  if File.directory?(ARGV[0])
    BatchProcessing.process(ARGV[0])
  elsif File.file?(ARGV[0])
    if ARGV[0].end_with?(".rb") || ARGV[0].end_with?(".js") ||ARGV[0].end_with?(".erb") 
      CommandLineController.run(ARGV[0])
    else
      abort(ViewFormatter.invalid_file)
    end
  else
    abort(ViewFormatter.file_not_found)
  end

  puts ViewFormatter.success_message(DestinationFileWriter.full_file_directory)
end