Sha256: a8511b4eaca90658cb7f6d85bdea7ff16c72b86b9d9923b7563f66ce71ccb857

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require "drb"

module Textbringer
  module Commands
    define_command(:server_start,
                   doc: "Start Textbringer server.") do
      uri = CONFIG[:server_uri] ||
        "drbunix:" + File.expand_path("server.sock", "~/.textbringer")
      options = CONFIG[:server_options] || { UNIXFileMode: 0600 }
      DRb.start_service(uri, Server.new, options)
    end

    define_command(:server_kill,
                   doc: "Kill Textbringer server.") do
      DRb.stop_service
    end

    define_command(:server_edit_done,
                   doc: "Finish server edit.") do
      queue = Buffer.current[:client_wait_queue]
      if queue.nil?
        raise EditorError, "No waiting clients"
      end
      if Buffer.current.modified? &&
          y_or_n?("Save file #{Buffer.current.file_name}?")
        save_buffer
      end
      kill_buffer(Buffer.current, force: true)
      queue.push(:done)
    end
  end

  class Server
    def eval(s)
      with_redisplay do
        Controller.current.instance_eval(s).inspect
      end
    end

    def visit_file(filename, wait: true)
      queue = Queue.new if wait
      with_redisplay do
        find_file(filename)
        Buffer.current[:client_wait_queue] = queue if wait
      end
      queue.deq if wait
    end

    private

    def with_redisplay
      foreground! do
        begin
          yield
        ensure
          Window.redisplay
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
textbringer-1.0.0 lib/textbringer/commands/server.rb
textbringer-0.3.2 lib/textbringer/commands/server.rb