Class: StackLoop::App

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

Defined Under Namespace

Classes: Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



35
36
37
# File 'lib/stack_loop/app.rb', line 35

def initialize
  @default_args = ""
end

Instance Attribute Details

#collect_fileObject

Returns the value of attribute collect_file.



33
34
35
# File 'lib/stack_loop/app.rb', line 33

def collect_file
  @collect_file
end

#commandObject

Returns the value of attribute command.



33
34
35
# File 'lib/stack_loop/app.rb', line 33

def command
  @command
end

#default_argsObject

Returns the value of attribute default_args.



33
34
35
# File 'lib/stack_loop/app.rb', line 33

def default_args
  @default_args
end

#stack_fileObject

Returns the value of attribute stack_file.



33
34
35
# File 'lib/stack_loop/app.rb', line 33

def stack_file
  @stack_file
end

Instance Method Details

#command_hashObject



151
152
153
# File 'lib/stack_loop/app.rb', line 151

def command_hash
  @command_hash ||= Hash[ commands.map{|cmd| [cmd.name, cmd] } ]
end

#command_line(argset) ⇒ Object



137
138
139
# File 'lib/stack_loop/app.rb', line 137

def command_line(argset)
  command_list + argset
end

#command_listObject



129
130
131
# File 'lib/stack_loop/app.rb', line 129

def command_list
  @command_list ||= command.split(/\s/)
end

#command_namesObject



147
148
149
# File 'lib/stack_loop/app.rb', line 147

def command_names
  @command_names ||= command_hash.keys
end

#commandsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stack_loop/app.rb', line 19

def commands
  [
    Command.new("run"){ run_stack_loop },
    Command.new("try"){ run_top },
    Command.new("collect"){ push_new; run_stack_loop },
    Command.new("list"){ read_stack; @stack.reverse_each{|argset| puts "- #{command_line(argset).join(" ")}"} },
    Command.new("pop"){ pop_argset },
    Command.new("push"){|args| push_argset(args)},
    Command.new("edit"){ edit_stack },
    Command.new("quit"){ throw :quit },
    Command.new("help"){ puts "Don't know that one - try: #{command_names.join(", ")}" }
  ]
end

#current_command_lineObject



133
134
135
# File 'lib/stack_loop/app.rb', line 133

def current_command_line
  command_line(get_argset)
end

#edit_stackObject



93
94
95
96
97
# File 'lib/stack_loop/app.rb', line 93

def edit_stack
  write_stack
  execute_editor_command(stack_file)
  read_stack
end

#execute_editor_command(path) ⇒ Object



99
100
101
102
# File 'lib/stack_loop/app.rb', line 99

def execute_editor_command(path)
  editor = ENV["EDITOR"] || "vim"
  system(editor, path)
end

#get_argsetObject



67
68
69
70
71
72
73
74
# File 'lib/stack_loop/app.rb', line 67

def get_argset
  read_stack
  if @stack.empty?
    [default_args]
  else
    @stack.last
  end
end

#pop_argsetObject



82
83
84
85
86
# File 'lib/stack_loop/app.rb', line 82

def pop_argset
  read_stack
  @stack.pop
  write_stack
end

#promptObject



141
142
143
144
145
# File 'lib/stack_loop/app.rb', line 141

def prompt
  puts
  puts "Next: " + current_command_line.join(" ")
  print "#{stack_depth} > "
end

#push_argset(args) ⇒ Object



76
77
78
79
80
# File 'lib/stack_loop/app.rb', line 76

def push_argset(args)
  read_stack
  @stack.push(args)
  write_stack
end

#push_newObject



44
45
46
47
48
# File 'lib/stack_loop/app.rb', line 44

def push_new
  unless collect_file.nil?
    push_argset File::read(collect_file).split("\n")
  end
end

#read_stackObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/stack_loop/app.rb', line 50

def read_stack
  stack_string = File.read(stack_file)
  @stack = stack_string.split("\n\n").map do |item|
    item.split("\n")
  end.reject do |item|
    item.empty?
  end
rescue Errno::ENOENT
  @stack = []
end

#runObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stack_loop/app.rb', line 104

def run
  validate_options!

  puts "Starting with: " + current_command_line.join(" ")
  puts
  run_stack_loop

  abbreviations = Abbrev.abbrev(command_names)

  catch :quit do
    loop do
      prompt
      command_line = $stdin.gets.chomp.split(/\s+/)
      command_name = command_line.shift
      if command_name.nil?
        command_name = "run"
      end

      command_name = abbreviations[command_name]
      command = command_hash.fetch(command_name, command_hash["help"])
      command.run(command_line)
    end
  end
end

#run_stackObject



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/stack_loop/app.rb', line 165

def run_stack
  return false if @stack.empty?

  success = run_top

  if success.nil?
    raise "#{[command, *args].inspect} couldn't be run"
  end
  pop_argset if success

  return success
end

#run_stack_loopObject



155
156
157
158
159
160
161
162
163
# File 'lib/stack_loop/app.rb', line 155

def run_stack_loop
  while run_stack
    puts
    puts "Success running next..."
    puts
  end

  run_top if @stack.empty?
end

#run_topObject



178
179
180
181
# File 'lib/stack_loop/app.rb', line 178

def run_top
  puts current_command_line.join(" ")
  system(*current_command_line)
end

#stack_depthObject



88
89
90
91
# File 'lib/stack_loop/app.rb', line 88

def stack_depth
  read_stack
  @stack.length
end

#validate_options!Object



39
40
41
42
# File 'lib/stack_loop/app.rb', line 39

def validate_options!
  raise "Need a command!" if command.nil?
  raise "Need a stack file!" if stack_file.nil?
end

#write_stackObject



61
62
63
64
65
# File 'lib/stack_loop/app.rb', line 61

def write_stack
  File::write(stack_file, @stack.map do |argset|
    argset.join("\n")
  end.join("\n\n") + "\n")
end