module Bosh
  module Cli
    class FileWithProgressBar < ::File

      def progress_bar
        return @progress_bar if @progress_bar
        out = Bosh::Cli::Config.output || StringIO.new
        @progress_bar = ProgressBar.new(file_name, size, out)
        @progress_bar.file_transfer_mode
        @progress_bar
      end

      def file_name
        File.basename(self.path)
      end

      def stop_progress_bar
        progress_bar.halt unless progress_bar.finished?
      end

      def size
        @size || File.size(self.path)
      end

      def size=(size)
        @size=size
      end

      def read(*args)
        result = super(*args)

        if result && result.size > 0
          progress_bar.inc(result.size)
        else
          progress_bar.finish
        end

        result
      end

      def write(*args)
        count = super(*args)
        if count
          progress_bar.inc(count)
        else
          progress_bar.finish
        end
        count
      end
    end
  end
end

module Bosh
  module Cli
    class FileWithProgressBar < ::File

      def progress_bar
        return @progress_bar if @progress_bar
        out = Bosh::Cli::Config.output || StringIO.new
        @progress_bar = ProgressBar.new(file_name, size, out)
        @progress_bar.file_transfer_mode
        @progress_bar
      end

      def file_name
        File.basename(self.path)
      end

      def stop_progress_bar
        progress_bar.halt unless progress_bar.finished?
      end

      def size
        @size || File.size(self.path)
      end

      def size=(size)
        @size=size
      end

      def read(*args)
        result = super(*args)

        if result && result.size > 0
          progress_bar.inc(result.size)
        else
          progress_bar.set(size)
          progress_bar.finish
        end

        result
      end

      def write(*args)
        count = super(*args)
        if count
          progress_bar.inc(count)
        else
          progress_bar.set(size)
          progress_bar.finish
        end
        count
      end
    end
  end
end

Make sure the progress bar goes to 100%
Make sure the progress bar goes to 100%

No one likes it when an operation ends and the progress bar never went all the way to 100%. @zachgersh was showing me some concourseci output and the progress bars always ended at 96%. Rather than sort out exactly why the chunks never match up exactly, just set the progress bar to 100% when we're done.