Class: Mchat::Repl

Inherits:
Object
  • Object
show all
Includes:
Command, ModApi, TimelineApi, UserConfig, Welcome
Defined in:
lib/mchat/repl.rb

Overview

Core REPL class

Constant Summary

Constants included from Command

Command::CommandComps, Command::CommandConditions

Constants included from UserConfig

UserConfig::CONFIG_DIR, UserConfig::CONFIG_PATH

Instance Method Summary collapse

Methods included from ModApi

#_chat_screen_print, #_cli_screen_print, #_current_channel, #_current_nickname, #_dispatch, #_mchat_action, #_mchat_speak, #_set_current_channel, #_set_current_nickname

Methods included from TimelineApi

#timeline_bossmode, #timeline_clear, #timeline_close_window

Methods included from Welcome

#display_ascii_art, #welcome

Methods included from Command

install, load_command, mount_command

Methods included from UserConfig

#check_server_before_all, #create_user_config, #read_user_config, #user_config_exist?

Constructor Details

#initialize(opts = {}) ⇒ Repl

Instance ########################3



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mchat/repl.rb', line 107

def initialize(opts = {})

  @config = read_user_config

  @server = opts.fetch(:server, nil) || @config.fetch("server", nil) || nil

  check_server_before_all

  @api = ::Mchat::Request.new(@server)

  @wait_prefix = @config.fetch("wait_prefix") || ">>"
  @display_welcome = @config.fetch("display_welcome") || true


  @channel_message_poll_time = 1 # seconds
  @channel_message_poll_running = true # global lock

  @channel_heartbeat_running = true # global lock
  @channel_heartbeat_time = 2 # global lock

  @clear_repl_everytime = @config.fetch("clear_repl_everytime") || false # global lock

  @current_channel = nil
  @current_nickname = nil

  @store = Mchat::Store.new({
    field_name: :messages
  })
end

Instance Method Details

#_apiObject



137
138
139
# File 'lib/mchat/repl.rb', line 137

def _api
  @api
end

#before_loop_setupObject



221
222
223
224
225
226
227
228
229
# File 'lib/mchat/repl.rb', line 221

def before_loop_setup
  # cli
  _puts welcome(@display_welcome)

  init_help_message
  # chat printer
  _puts2 welcome(@display_welcome)
  # _puts2 conn_server
end

#channel_heartbeat_taskObject



165
166
167
168
169
170
171
172
# File 'lib/mchat/repl.rb', line 165

def channel_heartbeat_task
  Thread.new do
    while _current_channel && _current_nickname && @channel_heartbeat_running
      resp = _api.ping_channel(_current_channel, _current_nickname)
      sleep @channel_heartbeat_time
    end
  end
end

#conn_serverObject



215
216
217
218
219
# File 'lib/mchat/repl.rb', line 215

def conn_server
  resp = _api.conn_server_startup
  startup_msg = resp.fetch("data")
  return Message.new(startup_msg).display
end

#fetch_channel_taskObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mchat/repl.rb', line 141

def fetch_channel_task
  Thread.new do
    last_news_time = 0
    while _current_channel && @channel_message_poll_running
      resp = _api.fetch_channel_message(_current_channel)
      data = resp.fetch("data")
      messages = data["messages"] || []

      news = messages.select { |m| m["timestamp"].to_i > last_news_time.to_i }
      news.sort! { |a, b| a["timestamp"].to_i <=> b["timestamp"].to_i }

      if news.length.positive?
        content = ""
        news.each do |m|
          content << Message.new(m).display
        end
        _puts2 content
        last_news_time = news.last["timestamp"]
      end
      sleep @channel_message_poll_time
    end
  end
end

#init_help_messageObject



209
210
211
212
213
# File 'lib/mchat/repl.rb', line 209

def init_help_message
  _puts "Mchat #{Mchat::VERSION}"
  _puts "/h[elp] for help".style.primary
  _puts ""
end

#parser(raw) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mchat/repl.rb', line 174

def parser(raw)
  words = raw.strip
  catch :halt do
    CommandConditions.each do |command|
      command_condition = command[:command_condition]
      command_condition.each do |cc|
        if cc.match(words)
          content = $1 ? $1 : nil
          _dispatch(command[:command_run], content)
          throw :halt
        end
      end
    end
  end
end

#runObject



231
232
233
234
235
236
# File 'lib/mchat/repl.rb', line 231

def run
  before_loop_setup
  loop do
    tick_work
  end
end

#tick_workObject



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/mchat/repl.rb', line 196

def tick_work
  begin
    user_hint_prefix
    user_type_in = gets

    @clear_repl_everytime && system('clear')
    parser(user_type_in)
    sleep 0.1
  rescue => exception
    p exception
  end
end

#user_hint_prefixObject



190
191
192
# File 'lib/mchat/repl.rb', line 190

def user_hint_prefix
  printf "#{_current_channel ? '['+_current_channel+']' : '' }#{_current_nickname ? '@'+_current_nickname : '' }#{@wait_prefix}"
end