Sha256: 2f85d4f7918b997e82a04aabb88bd8de45763f1ae731b1112465adcfd360979b

Contents?: true

Size: 918 Bytes

Versions: 5

Compression:

Stored size: 918 Bytes

Contents

require 'date'

module Mpx
  ##
  # Manages history for commands.
  class History
    def initialize(root)
      @root = root
    end

    def now
      return DateTime.now.strftime("%d/%m/%Y %H:%M")
    end

    def all_history
      return Dir.entries(@root)
        .select { |f| File.file?(File.join(@root, f)) }
    end

    def get(*commands)
      return (commands.empty? ? all_history : commands)
        .map { |c| history_for(c) }
        .flatten
        .sort_by do |line|
          time, * = line.split('$')
          DateTime.parse(time.strip)
        end
    end

    def history_for(command)
      File.foreach(File.join(@root, command))
        .map { |line| line.strip }
    rescue
      raise "no history for #{command}"
    end

    def write(command, *args)
      File.open(File.join(@root, command), 'a') do |f|
        f.puts("#{now} $ #{command} #{args.join(' ')}")
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mpx-0.5.0 lib/mpx/history.rb
mpx-0.4.1 lib/mpx/history.rb
mpx-0.4.0 lib/mpx/history.rb
mpx-0.3.0 lib/mpx/history.rb
mpx-0.2.0 lib/mpx/history.rb