Sha256: f2e39d13cb5f84edff7ac2a18f492ddd97a98bb970c62257a14dfb9c6ef58655

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

#!/usr/bin/env ruby

require 'tins/go'
include Tins::GO
require 'tins/secure_write'
include Tins::SecureWrite
require 'tins/find'
include Tins::Find
require 'utils'

def usage
  puts <<-EOT
Usage: #{File.basename($0)} [OPTS] [PATHS]

PATHS are the directory and file paths that are search for files to be
stripped.

Options are

  -t COLUMNS  turn tabs into COLUMNS spaces
  -I SUFFIXES list of suffixes
  -h          display this help

Version is #{File.basename($0)} #{Utils::VERSION}.
  EOT
  exit 1
end

args = go 'I:t:h'
args[?h] and usage

unless ARGV.empty?
  paths = ARGV.map { |p| File.expand_path(p) }
end

suffix = Array(args[?I])

config = Utils::ConfigFile.new
config.configure_from_paths

if paths
  find(*(paths + [ { :suffix => suffix} ])) do |filename|
    bn, s = File.basename(filename), File.stat(filename)
    s.symlink? and next
    if s.directory?
      config.strip_spaces.prune?(bn) and prune
      next
    end
    s.file? or next
    config.strip_spaces.skip?(bn) and next
    File.ascii?(filename) or next
    STDOUT.puts "Stripping spaces/tabs from #{filename.inspect}."
    secure_write(filename) do |output|
      File.open(filename) do |file|
        old_mode = file.stat.mode
        file.each do |line|
          line.gsub!(/[ \t\v]+$/, '')
          if tabs = args[?t]
            line.gsub!(/\t/, ' ' * Integer(tabs))
          end
          output.write line
        end
        File.chmod old_mode, output.path
      end
    end
  end
else
  for line in STDIN
    line.gsub!(/[ \t\v]+$/, '')
    STDOUT.write line
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
utils-0.63.0 bin/strip_spaces
utils-0.62.0 bin/strip_spaces