Module: Zashoku

Defined in:
lib/daemon.rb,
lib/viewer.rb,
lib/zashoku.rb,
lib/constants.rb,
lib/core/item.rb,
lib/core/view.rb,
lib/generator.rb,
lib/core/module.rb,
lib/core/options.rb,
lib/core/formatter.rb,
lib/core/util/term.rb,
lib/core/util/util.rb,
lib/core/controller.rb,
lib/core/net/client.rb,
lib/core/net/server.rb,
lib/core/config/config.rb,
lib/core/util/readline.rb,
lib/core/net/client_san.rb,
lib/core/statusline/widget.rb,
lib/core/config/view_config.rb,
lib/generator/app_generator.rb,
lib/core/config/daemon_config.rb,
lib/core/modules/conf/lib/conf.rb,
lib/core/statusline/statusline.rb,
lib/generator/module_generator.rb,
lib/core/modules/conf/lib/conf/config_view.rb

Defined Under Namespace

Modules: Conf, Formatter, Generator, Module, Net, Statusline, Util Classes: Config, Controller, Daemon, DaemonConfig, Item, Options, View, ViewConfig, Viewer

Constant Summary collapse

Version =
[1, 7, 1].freeze
Root =
File.expand_path('../', __dir__)
DefConf =
{
  app: {
    root: Root,
    name: 'zashoku',
    commands: {},
    modules: {
      viewer: [],
      daemon: [],
    },
    log_level: {
      viewer: :warn,
      daemon: :info
    },
    save_config: false,
    save_history: false,
    persistent_daemon: false,
    net: {
      host: 'localhost',
      port: 26_119
    },
    msg: {
      quit: 'Type :quit<enter> to quit'
    }
  },
  core: {
    commands: {
      'quit'          => :cleanup,
      'q'             => :cleanup,
      'enter_command' => :get_user_command,
      'change_view'   => :change_view,
      'map_key'       => :map_key,
      'search'        => :search,
      'zv'            => :print_version
    },
    net: {
      bsize: 4096
    }
  }
}.freeze
CConf =
const_defined?(:AppConf) ? Zashoku::Util.deep_merge(DefConf, AppConf) : DefConf
LoadPaths =
[
  File.join(Zashoku::CConf[:app][:root], 'modules')
].freeze
EOF =
'ZASHOKU_EOF'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject

Returns the value of attribute client.



29
30
31
# File 'lib/viewer.rb', line 29

def client
  @client
end

.confObject

Returns the value of attribute conf.



12
13
14
# File 'lib/zashoku.rb', line 12

def conf
  @conf
end

.controllersObject

Returns the value of attribute controllers.



20
21
22
# File 'lib/daemon.rb', line 20

def controllers
  @controllers
end

.loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/zashoku.rb', line 12

def logger
  @logger
end

.modulesObject

Returns the value of attribute modules.



12
13
14
# File 'lib/zashoku.rb', line 12

def modules
  @modules
end

.serverObject

Returns the value of attribute server.



20
21
22
# File 'lib/daemon.rb', line 20

def server
  @server
end

Class Method Details

.command(message) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/daemon.rb', line 21

def command(message)
  Zashoku.logger.debug("command got #{message}")
  message = message.keys.map(&:to_sym).zip(message.values).to_h

  obj =
    if controllers.key?(message[:mod]) && controllers[message[:mod]].respond_to?(message[:meth])
      controllers[message[:mod]].send(message[:meth], *message[:args])
    else
      nil
    end
  if message[:raw]
    Zashoku.logger.info('responding with unencoded stream')
    if obj.respond_to?(:gets)
      obj
    else
      StringIO.new(obj.to_s)
    end
  else
    Zashoku::Util.encode_object(obj)
  end
end

.command_server(command, host, port) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zashoku.rb', line 56

def self.command_server(command, host, port)
  case command
  when 'start'
    start_daemon(port)
  when 'restart'
    command_server('stop')
    start_daemon
  else
    require_relative 'core/net/client'
    Net::Client.command(host, port, command)
  end
end

.main(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zashoku.rb', line 22

def self.main(args)
  @options = Options.parse(args)
  host = @options[:host].key?(:host) ? @options[:host][:host] : Zashoku::CConf[:app][:net][:host]
  port = @options[:host].key?(:port) ? @options[:host][:port] : Zashoku::CConf[:app][:net][:port]

  Zashoku.modules = []

  if @options[:generate]
    Generator.generate(@options[:template], @options[:generate_name])
  elsif @options[:server_command]
    print command_server(@options[:server_command], host, port)[:payload]
  else
    unless command_server('up?', host, port)[:status]
      @options[:daemon] = true
      command_server('start', host, port)
      sleep(0.2) until command_server('up?', host, port)[:status]
    end
    Viewer.new(host: host, port: port)
  end
end

.start_daemon(port) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zashoku.rb', line 43

def self.start_daemon(port)
  if @options[:daemon]
    fork {
      Process.daemon()
      d = Daemon.new(port: port)
      d.listen
    }
  else
    d = Daemon.new(port: port)
    d.listen
  end
end