Sha256: 41ad9bdde30c1d9eda69e11857122e052650bd2712102e5d49a49b9b1c17cc45

Contents?: true

Size: 1.51 KB

Versions: 60

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/string/filters'
require 'eac_ruby_utils/fs/temp'
require 'filesize'

module EacRubyUtils
  module Fs
    class Logs
      TRUNCATE_DEFAULT_LENGTH = 1000
      TRUNCATE_APPEND_TEXT = '(...) '

      def [](label)
        log_set.fetch(sanitize_label(label))
      end

      def add(label)
        log_set[sanitize_label(label)] = ::EacRubyUtils::Fs::Temp.file

        self
      end

      def remove_all
        log_set.each_key { |label| remove(label) }

        self
      end

      def remove(label)
        log_set.fetch(sanitize_label(label)).remove
        log_set.delete(sanitize_label(label))
      end

      def truncate(label, length = TRUNCATE_DEFAULT_LENGTH)
        content = self[label].read.strip
        return content if content.length <= TRUNCATE_DEFAULT_LENGTH

        TRUNCATE_APPEND_TEXT + content[content.length - length + TRUNCATE_APPEND_TEXT.length,
                                       length - TRUNCATE_APPEND_TEXT.length]
      end

      def truncate_all(length = TRUNCATE_DEFAULT_LENGTH)
        s = "Files: #{log_set.length}\n"
        log_set.each do |label, path|
          x = truncate(label, length)
          y = [label, path, ::Filesize.from("#{path.size} B").pretty].join(' / ')
          s += x.blank? ? ">>> #{y} (Blank) <<<" : ">>> #{y}\n#{x}\n<<< #{y}\n"
        end
        s
      end

      private

      def sanitize_label(label)
        label.to_sym
      end

      def log_set
        @log_set ||= {}
      end
    end
  end
end

Version data entries

60 entries across 60 versions & 3 rubygems

Version Path
eac_ruby_utils-0.80.1 lib/eac_ruby_utils/fs/logs.rb
eac_ruby_utils-0.80.0 lib/eac_ruby_utils/fs/logs.rb
eac_ruby_utils-0.79.0 lib/eac_ruby_utils/fs/logs.rb
ehbrs-tools-0.28.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
eac_ruby_utils-0.78.0 lib/eac_ruby_utils/fs/logs.rb
avm-tools-0.110.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
eac_ruby_utils-0.77.1 lib/eac_ruby_utils/fs/logs.rb
avm-tools-0.109.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
eac_ruby_utils-0.77.0 lib/eac_ruby_utils/fs/logs.rb
avm-tools-0.109.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
ehbrs-tools-0.28.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
avm-tools-0.108.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
ehbrs-tools-0.28.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
avm-tools-0.107.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
eac_ruby_utils-0.76.0 lib/eac_ruby_utils/fs/logs.rb
ehbrs-tools-0.28.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
avm-tools-0.106.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
ehbrs-tools-0.27.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb
eac_ruby_utils-0.75.0 lib/eac_ruby_utils/fs/logs.rb
ehbrs-tools-0.26.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb