Module: Xi::REPL

Extended by:
REPL
Included in:
REPL
Defined in:
lib/xi/repl.rb

Constant Summary collapse

CONFIG_PATH =
File.expand_path("~/.config/xi")
HISTORY_FILE =
"history"
INIT_SCRIPT_FILE =
"init.rb"
DEFAULT_INIT_SCRIPT =
"# Here you can customize or define functions that will be available in\n" \
"# Xi, e.g. new streams or a custom clock."

Instance Method Summary collapse

Instance Method Details

#configureObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xi/repl.rb', line 24

def configure
  prepare_config_dir

  if ENV["INSIDE_EMACS"]
    Pry.config.correct_indent = false
    Pry.config.pager = false
    Pry.config.prompt = [ proc { "" }, proc { "" }]
  else
    Pry.config.prompt = [ proc { "xi> " }, proc { "..> " }]
  end

  Pry.config.history.file = history_path

  Pry.hooks.add_hook(:after_eval, "check_for_errors") do |result, pry|
    more_errors = ErrorLog.instance.more_errors?
    ErrorLog.instance.each do |msg|
      puts red("Error: #{msg}")
    end
    puts red("There were more errors...") if more_errors
  end
end

#history_pathObject



62
63
64
# File 'lib/xi/repl.rb', line 62

def history_path
  File.join(CONFIG_PATH, HISTORY_FILE)
end

#init_script_pathObject



66
67
68
# File 'lib/xi/repl.rb', line 66

def init_script_path
  File.join(CONFIG_PATH, INIT_SCRIPT_FILE)
end

#load_init_scriptObject



50
51
52
# File 'lib/xi/repl.rb', line 50

def load_init_script
  require(init_script_path)
end

#prepare_config_dirObject



54
55
56
57
58
59
60
# File 'lib/xi/repl.rb', line 54

def prepare_config_dir
  FileUtils.mkdir_p(CONFIG_PATH)

  unless File.exists?(init_script_path)
    File.write(init_script_path, DEFAULT_INIT_SCRIPT)
  end
end

#red(string) ⇒ Object



46
47
48
# File 'lib/xi/repl.rb', line 46

def red(string)
  "\e[31m\e[1m#{string}\e[22m\e[0m"
end

#start(irb: false) ⇒ Object



17
18
19
20
21
22
# File 'lib/xi/repl.rb', line 17

def start(irb: false)
  configure
  load_init_script

  irb ? IRB.start : Pry.start
end