Class: AM::Tail

Inherits:
Object
  • Object
show all
Includes:
MessageControl
Defined in:
lib/tail.rb

Constant Summary

Constants included from MessageControl

MessageControl::ERROR_MESSAGE, MessageControl::NOTICE_MESSAGE, MessageControl::WARNING_MESSAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageControl

#after_sepalate, #before_sepalate, #error, #notice, #print_message, #warning

Constructor Details

#initialize(config) ⇒ Tail

Returns a new instance of Tail.



10
11
12
# File 'lib/tail.rb', line 10

def initialize(config)
  @profile = get_profile(config)
end

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



8
9
10
# File 'lib/tail.rb', line 8

def profile
  @profile
end

Instance Method Details

#get_last_commandObject



37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



33
34
35
# File 'lib/tail.rb', line 33

def set_hash(margin,  history_file)
  return { margin: margin, file: history_file }
end