Class: AM::Tail

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Tail

Returns a new instance of Tail.



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

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

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



6
7
8
# File 'lib/tail.rb', line 6

def profile
  @profile
end

Instance Method Details

#get_last_commandObject



48
49
50
51
52
53
# File 'lib/tail.rb', line 48

def get_last_command
  r=""
  if c = `tail -#{2-@profile[:margin]} #{@profile[:file]} | head -1`
    sampling(c)
  end
end

#get_last_five_commandObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tail.rb', line 36

def get_last_five_command
  commands = []
  r=""
  last_commands = `tail -#{@profile[:max_line] + 1 - @profile[:margin]} #{@profile[:file]} | head -#{@profile[:max_line]}`.split("\n")
  unless last_commands.empty?
    last_commands.each_with_index do |c,i|
      commands << r if r = sampling(c)
    end
    commands
  end
end

#get_profile(config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tail.rb', line 11

def get_profile(config)
  shell = ENV['SHELL']
  h = case shell
  when /zsh/  then set_hash(0, 5, '~/.zsh_history')
  when /bash/ then set_hash(1, 5, '~/.bash_history')
  else # todo raise
      puts "does not support is #{shell}"
      exit
  end

  h[:file] = f if f =  config.pg['history_file']
  h[:file] = File.expand_path(h[:file])

  unless File.exists?(h[:file])
    #todo raise
    puts "history file not found #{h[:file]}"
    exit
  end
  h
end

#sampling(command) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/tail.rb', line 55

def sampling(command)
  zsh  = '[0-9]+:[0-0];(.*)'
  bash = '(.*)'
  if command =~ /#{zsh}/
    p = zsh
  else
    p = bash
  end
  command.split(/#{p}/)[COMMAND].strip if command.strip !~ /^$/
end

#set_hash(margin, max_line, history_file) ⇒ Object



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

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