class AM::Tail

Attributes

profile[RW]

Public Class Methods

new(config) click to toggle source
# File lib/tail.rb, line 10
def initialize(config)
  @profile = get_profile(config)
end

Public Instance Methods

get_last_command() click to toggle source
# File lib/tail.rb, line 37
def get_last_command
  commands = []
  last_commands = `tail -#{TAIL_LINE + 1 - @profile[:margin]} #{@profile[:file]} | head -#{TAIL_LINE}`.split("\n")
  unless last_commands.empty?
    last_commands.each_with_index do |c,i|
      r = sampling(c)
      commands << r if r
    end
    commands
  end
end
get_profile(config) click to toggle source
# File lib/tail.rb, line 14
def get_profile(config)
  shell = ENV['SHELL']
  profile = case shell
  when /zsh/  then set_hash(0, '~/.zsh_history')
  when /bash/ then set_hash(1, '~/.bash_history')
  else
      error(:no_support, shell)
  end
  f = config.pg['history_file']
  profile[:file] = f if f
  profile[:file] = File.expand_path(profile[:file])

  unless File.exists?(profile[:file])
    error(:not_exists_history_file, profile[:file])
    exit
  end
  profile
end
sampling(command) click to toggle source
# File lib/tail.rb, line 49
def sampling(command)
  zsh  = '[0-9]+:[0-0];(.*)'
  bash = '(.*)'
  if command =~ /#{zsh}/
    pattern = zsh
  else
    pattern = bash
  end
  command.split(/#{pattern}/)[COMMAND].strip if command.strip !~ /^$/
end
set_hash(margin, history_file) click to toggle source
# File lib/tail.rb, line 33
def set_hash(margin,  history_file)
  return { margin: margin, file: history_file }
end