Module: Imp::UI

Defined in:
lib/imp/ui.rb

Overview

Module handling user I/O.

Constant Summary collapse

DEFAULT_FILE =

The default file to save encrypted passwords in.

'~/.imp/default.enc'
HISTFILE =

The file of the history of the prompt.

'~/.imp/hist'
PROMPT =

The string precending user input in the prompt.

'imp> '
TIMEOUT =

The time in seconds, after which the program exits if it recieves no input from the user.

300

Class Method Summary collapse

Class Method Details

.close_fileObject

Closes the tree. This generally only happens right before ruby is about to exit so it isn’t that important but hey.



43
44
45
46
# File 'lib/imp/ui.rb', line 43

def self.close_file
  $tree.close
  $tree = nil
end

.load_fileObject

Loads and decrypts a file. The password is asked for interactively.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/imp/ui.rb', line 29

def self.load_file
  until $tree
    begin
      passwd = get_passwd
      return unless passwd
      $tree = EncryptedTree.new(passwd, $file)
    rescue OpenSSL::Cipher::CipherError
      $stderr.puts "Decryption failed. Corrupt file or wrong password."
    end
  end
end

.mainObject

Runs the program.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/imp/ui.rb', line 49

def self.main
  load_options
  if $opts[:file]
    $file = $opts[:file]
  else
    $file = DEFAULT_FILE
  end
  load_file
  # If no password was entered, quit.
  exit unless $tree
  welcome
  init_readline
  begin
    prompt
  ensure
    close_file
  end
end

.timeout(&block) ⇒ Object

Times out execution of a block and exits printing an appropriate message if the block doesn’t time out in time.

The name is due to a conflict with Timeout’s own.



72
73
74
75
76
77
78
79
# File 'lib/imp/ui.rb', line 72

def self.timeout(&block)
  begin
    Timeout::timeout(TIMEOUT, &block)
  rescue Timeout::Error
    $stderr.puts "\nUser input timeout. Closing..."
    exit
  end
end