Class: SetupBashrc

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

Class Method Summary collapse

Class Method Details

.apply_change(file, force = false) ⇒ Object

*file <~String>: bashrc file to detect and optionally apply changes to.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/history_commander/setup_bashrc.rb', line 44

def self.apply_change(file, force = false)
  say("Thanks for installing History Commander!\nThe following modifications will be applied to your ~/.bashrc file:")
  say("+++\n#{self.goodies}")
  self.do_warnings(IO.read(file))
  unless force
    confirm = ask("+++ Apply changes (y/n)?", lambda { |ans| true if (ans =~ /^[y,Y]{1}/) })
    unless confirm
      say "Aborting."
      exit
    end
  end
  backup = "#{file}_hc_backup#{rand(10000)}"
  say "Creating backup of .bashrc in #{backup}"
  FileUtils.cp(file, backup)
  File.open(file, "a") { |f| f.puts(self.goodies)}
  say "done installing history customization."
  say "***"
  say "Now you MUST load your new bash history config:"
  say "Open a new terminal -or-" 
  say "'source #{file}'"
end

.do_warnings(file_contents) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/history_commander/setup_bashrc.rb', line 34

def self.do_warnings(file_contents)
  if file_contents =~ /#{self.goodies}/
    say "*** WARNING: modifications already installed!"
  end
  if file_contents =~ /PROMPT_COMMAND/
    say "*** WARNING: your .bashrc already has a PROMPT_COMMAND.  This setting will be overridden!"
  end
end

.goodiesObject



24
25
26
27
28
29
30
31
32
# File 'lib/history_commander/setup_bashrc.rb', line 24

def self.goodies
  <<eoh
shopt -s histappend
export HIST_CONTROL=ignoreboth
export PROMPT_COMMAND="history -a; history -n"
export HISTSIZE=1000000 
export HISTFILESIZE=1000000
eoh
end

.install(force = false) ⇒ Object

Install necessary .bashrc customization for optimal history commander usage.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/history_commander/setup_bashrc.rb', line 8

def self.install(force = false)
  my_home = File.expand_path('~')
  my_bashrc = File.join(my_home, ".bashrc")
  unless File.exists?(my_bashrc)
    if File.exists?("/etc/bashrc")
      FileUtils.cp("/etc/bashrc", my_bashrc)
    elsif File.exists?("/usr/etc/bashrc")
      FileUtils.cp("/usr/etc/bashrc", my_bashrc)
    else
      `touch #{my_bashrc}` 
    end
  end
  say "using .bashrc in #{my_bashrc}"
  self.apply_change(my_bashrc, force)
end