Class: Zashoku::Viewer

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/viewer.rb

Overview

Main class for the zashoku application

Constant Summary collapse

CMD_HISTORY =
File.join(Zashoku::Config::CONF_DIR, 'cmd_history.yml')
SRCH_HISTORY =
File.join(Zashoku::Config::CONF_DIR, 'srch_history.yml')
COMMANDS =
Zashoku::CConf[:core][:commands].merge(Zashoku::CConf[:app][:commands])

Instance Method Summary collapse

Methods included from Util

alert, decode_object, deep_merge, encode_object, get_yaml, put_yaml

Constructor Details

#initializeViewer

Returns a new instance of Viewer.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/viewer.rb', line 54

def initialize
  @semaphore = Mutex.new

  Zashoku.logger = Logger.new(
    File.join(Zashoku::CConf[:app][:root], "#{Zashoku::CConf[:app][:name]}.log")
  )
  Zashoku.logger.level = Zashoku::CConf[:app][:log_level][:viewer]

  Zashoku.conf = ViewConfig.new

  Zashoku.modules = Zashoku::Module.load(Zashoku::CConf[:app][:modules][:viewer])
  Zashoku.modules.merge!('conf' => Conf)
  Zashoku.conf.reload

  # note: start daemon if not running
  Zashoku.client = Net::Client.new(Zashoku::CConf[:app][:net][:port])
  Zashoku.client.callbacks << method(:handle_event)

  @server_modules = Zashoku.client.command('modules')

  @views = Zashoku.modules.keys.zip(Zashoku.modules.map do |_k, m|
    m.view
  end).to_h
  @view = @views.keys.first

  @statusline = Statusline.new
  @statusline.add_observer(self, :draw_statusline)

  @cmdline = Util::Readline.new(
    ':',
    ac_tree:
        @views.map { |k, v| [k, v.methods - Object.methods] }.to_h
        .merge(COMMANDS.map { |k, _v| [k, nil] }.to_h)
        .merge({'conf' => Zashoku.conf.methods - Object.methods })
        .merge(@server_modules.map { |m| ['remote::' + m, nil] }.to_h),
    history: Util.get_yaml(CMD_HISTORY, [])
  )
  @searchline = Util::Readline.new('/', history: Util.get_yaml(SRCH_HISTORY, []))

  init_screen
  lay_traps!
  Thread.abort_on_exception = true
  Thread.new { scan_keyboard! }

  init_view(@view) if @view
  @statusline.draw
  sleep
end

Instance Method Details

#builtin_command(cmd, *args) ⇒ Object



209
210
211
212
# File 'lib/viewer.rb', line 209

def builtin_command(cmd, *args)
  Zashoku.logger.debug("executing builtin #{cmd}(#{args})")
  send(COMMANDS[cmd], *args)
end

#change_view(new_view) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/viewer.rb', line 115

def change_view(new_view)
  Zashoku.logger.debug("Changing view to #{new_view}")
  @views[@view].delete_observers
  #@views[new_view].change_screen_size(redraw: false)
  #@views[new_view].expand(-1)
  @view = new_view
  init_view(@view)
end

#cleanupObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/viewer.rb', line 147

def cleanup
  Zashoku.client.disconnect
  @key_thread&.exit
  Curses.close_screen

  unless Zashoku::CConf[:app][:persistent_daemon]
    Zashoku.command_server('stop')
  end

  if Zashoku::Conf.save?
    Util.put_yaml(CMD_HISTORY, @cmdline.history)
    Util.put_yaml(SRCH_HISTORY, @searchline.history)
  end
ensure
  Term.show_cursor
  exit!
end

#command(mod, meth, *args) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/viewer.rb', line 172

def command(mod, meth, *args)
  if /^remote::.*/.match?(mod)
    Zashoku.command(mod: mod, meth: meth, args: args)
  elsif @views.key?(mod) && @views[mod].respond_to?(meth)
    @views[mod].send(meth, *args)
  elsif @views.key?(mod)
    raise "#{mod} has no method '#{meth}'"
  else
    raise "module '#{mod}' not loaded"
  end
end

#draw_statuslineObject



265
266
267
# File 'lib/viewer.rb', line 265

def draw_statusline
  @statusline.draw
end

#draw_viewObject



261
262
263
# File 'lib/viewer.rb', line 261

def draw_view
  @views[@view].draw
end

#get_user_commandObject



184
185
186
187
188
189
190
191
192
193
# File 'lib/viewer.rb', line 184

def get_user_command
  cmd = @cmdline.read
  return unless cmd
  return builtin_command(*cmd.split) if COMMANDS.key?(cmd.split.first)
  raise "no such command" if cmd.split.length == 1
  command(*cmd.split) unless cmd.empty?
rescue StandardError => e
  Zashoku.logger.error("#{cmd.split} raised an error '#{e}'")
  Util.alert("Error: #{e}")
end

#handle_event(event) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/viewer.rb', line 200

def handle_event(event)
  return @statusline.event(event) if event['statusline']

  case event['event']
  when 'items'
    command(event['sender'], :refresh)
  end
end

#init_screenObject



165
166
167
168
169
170
# File 'lib/viewer.rb', line 165

def init_screen
  Curses.noecho
  Curses.init_screen
  Term.hide_cursor
  Curses.stdscr.keypad(true)
end

#init_view(view) ⇒ Object



124
125
126
127
128
129
# File 'lib/viewer.rb', line 124

def init_view(view)
  @views[view].add_observer(self, :draw_view)
  @views[view].refresh
  @views[view].dirty_all_lines
  draw_view
end

#lay_traps!Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/viewer.rb', line 131

def lay_traps!
  Signal.trap('WINCH') do
    Thread.new do
      @semaphore.synchronize do
        @views[@view].change_screen_size
        @statusline.change_screen_size
      end
    end
  end
  Signal.trap('INT') do
    Thread.new do
      @semaphore.synchronize { Util.alert('Type :quit<enter> to exit.') }
    end
  end
end

#map_key(key, mod, *command) ⇒ Object



214
215
216
# File 'lib/viewer.rb', line 214

def map_key(key, mod, *command)
  Zashoku.conf.set(['keymaps', mod, key], command.join(' '))
end


218
219
220
# File 'lib/viewer.rb', line 218

def print_version
  Util.alert("zashoku v#{Zashoku::Version.join('.')}")
end

#reconnect_clientObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/viewer.rb', line 103

def reconnect_client
    loop do
      @socket = connect(@host, @port, loud_fail: false)
      break if @socket
      sleep 1
    end

    @alive = true
    start_threads
    Util.alert('regained connection!')
end

#scan_keyboard!Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/viewer.rb', line 222

def scan_keyboard!
  loop do
    key  = Curses.getch
    maps = Zashoku.conf.get(%w[keymaps])
    cmd =
      if maps['global'].key?(key)
        maps['global'][key]
      elsif maps[@view]&.key?(key)
        maps[@view][key]
      else
        ''
      end

    if COMMANDS.key?(cmd.split.first)
      builtin_command(*cmd.split)
      next
    end

    mod, meth, *args = cmd.split
    mod = mod == '$current_view' ? @view : mod
    args.map! do |arg|
      if arg.chr == '$'
        arg = arg[1..-1]
        chain = arg.split('.')
        chain[1..-1].reduce(@views[@view].send(chain.first)) do |m, a|
          m.send(a)
        end
      else
        arg
      end
    end
    begin
      command(mod, meth, *args)
    rescue Exception => e
      Zashoku.logger.error("#{[mod, meth, args].join(', ')} raised #{e}")
    end
  end
end

#searchObject



195
196
197
198
# File 'lib/viewer.rb', line 195

def search(*)
  srchterm = @searchline.read
  command(@view, :search, srchterm)
end