Module: Mchat::UserConfig

Included in:
Repl
Defined in:
lib/mchat/comps/user_config.rb

Constant Summary collapse

CONFIG_DIR =
Pathname.new(Dir.home).join('.mchat')
CONFIG_PATH =
Pathname.new(Dir.home).join('.mchat').join('mchatrc')

Instance Method Summary collapse

Instance Method Details

#check_server_before_allObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mchat/comps/user_config.rb', line 38

def check_server_before_all
  if !@server
    puts "Mchat about `Server` Tips".style.jade
    puts ""
    puts "your server: `#{@server}`"
    puts ""
    init_config = "~/.mchat".style.warn
    puts "Mchat has help your create config to #{init_config}"
    server_field = "<server> field".style.warn
    puts "Before you run mchat, edit your config file, change #{server_field} to yours:"
    puts "vim #{CONFIG_PATH.to_s}".style.warn
    puts "make sure your server works, before run Mchat."
    puts ""
    puts "If you have server address, you can also run like this:"
    puts "mchat --server='http://<your host>'"
    puts "e.g.  mchat --server='http://localhost:4567'"
    puts "so you will not need to edit mchatrc." + " :)".style.warn
    puts ""
    puts "Confirm? (y/yes)"
    while true
      confirm = gets
      confirm = confirm.strip
      if confirm == 'y' || confirm == 'yes'
        system('screen -X quit')
      end
      sleep 0.1
    end
  end
end

#create_user_configObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mchat/comps/user_config.rb', line 10

def create_user_config
  require 'fileutils'
  FileUtils.mkdir_p(CONFIG_DIR) unless File.exist?(CONFIG_DIR)
  File.open(CONFIG_PATH, 'w') do |f|
init_config = %Q(
# Mchat user config
# use yaml syntax

wait_prefix: ">>"
display_welcome: true
clear_repl_everytime: true
# server: "localhost:4567"

)
  f << init_config
  end
end

#read_user_configObject



28
29
30
31
32
33
34
35
36
# File 'lib/mchat/comps/user_config.rb', line 28

def read_user_config
  if !user_config_exist?
    create_user_config
  end

  require 'yaml'
  opt = YAML.load(File.open(CONFIG_PATH))
  return opt
end

#user_config_exist?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/mchat/comps/user_config.rb', line 7

def user_config_exist?
  File.exist? CONFIG_PATH
end