Class: SyntaxTree::CLI::Write

Inherits:
Action
  • Object
show all
Defined in:
lib/syntax_tree/cli.rb

Overview

An action of the CLI that formats the input source and writes the formatted output back to the file.

Instance Attribute Summary

Attributes inherited from Action

#options

Instance Method Summary collapse

Methods inherited from Action

#failure, #initialize, #success

Constructor Details

This class inherits a constructor from SyntaxTree::CLI::Action

Instance Method Details

#run(item) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/syntax_tree/cli.rb', line 193

def run(item)
  filepath = item.filepath
  start = Time.now

  source = item.source
  formatted = item.handler.format(source, options.print_width)
  File.write(filepath, formatted) if item.filepath != :script

  color = source == formatted ? Color.gray(filepath) : filepath
  delta = ((Time.now - start) * 1000).round

  puts "#{color} #{delta}ms"
rescue StandardError
  puts filepath
  raise
end