Class: Mchat::Timeline

Inherits:
Object
  • Object
show all
Defined in:
lib/mchat/timeline.rb

Instance Method Summary collapse

Constructor Details

#initializeTimeline

Returns a new instance of Timeline.



17
18
19
20
21
22
23
# File 'lib/mchat/timeline.rb', line 17

def initialize
  @reader = Mchat::Store.new({
    field_name: :messages
  })

  @boss_mode = false
end

Instance Method Details

#api_bossmodeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mchat/timeline.rb', line 30

def api_bossmode
  # toggle api
  @boss_mode = !@boss_mode

  thx = nil
  if @boss_mode
    system('clear')
    boss_will_see_fake_logs
  else
    sleep 2
    system('clear')
    puts "======= Recover last 100 ==========="
    @reader.messages_history(100).each do |m|
      if m.is_a? String || Rainbow::Presenter
        puts m
      end
    end
    puts "======= Recover last 100 ==========="
  end
end

#api_clearObject

约定和cli交互用api_xx方法



26
27
28
# File 'lib/mchat/timeline.rb', line 26

def api_clear
  system('clear')
end

#api_close_windowObject



51
52
53
# File 'lib/mchat/timeline.rb', line 51

def api_close_window
  @reader.store_messages_reader_run = false
end

#boss_will_see_fake_logsObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mchat/timeline.rb', line 73

def boss_will_see_fake_logs
  fake_logs = File.open(Pathname.new(File.join(__dir__, './fake_log.txt'))).readlines
  thx = Thread.new {
    while @boss_mode
      fake_logs.each do |line|
        puts line
        sleep 0.1
      end
    end
  }
end

#dispatch(m) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mchat/timeline.rb', line 61

def dispatch(m)
  cmd_name = m.name
  data = m.data || nil

  api_name = "api_#{cmd_name}".to_sym
  if data
    __send__(api_name, data)
  else
    __send__(api_name)
  end
end

#hook_closeObject



55
56
57
58
59
# File 'lib/mchat/timeline.rb', line 55

def hook_close
  @reader.hook_quit
  system('screen -X quit')
  exit 0
end

#runObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mchat/timeline.rb', line 85

def run
  @reader.message_loop_reader { |messages|
    messages.each do |m|
      if m.is_a? String || Rainbow::Presenter
        if !@boss_mode
          puts m
        end
      elsif m.is_a? Mchat::TimelineCommand
        dispatch(m)
      end
    end
  }
  hook_close
end