textbringer-ghost_text 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee11306209a200392bb926c38709e91edb9151b3
4
- data.tar.gz: bbb782882d6590efd34e9f5f58633ccde50eb89b
3
+ metadata.gz: f86902879c3f37dde2c3719208fae58d263eeed1
4
+ data.tar.gz: 7349aea3d2e51a8f87062a3c1f861f4af31a7c18
5
5
  SHA512:
6
- metadata.gz: 85940038dca84d2d28755c0db4431b02eaf6636665cb930ef12e4f7da4167929ae2d32c4b5ee368a176fa17b456bca85554e45984fbcc691de562d5d6702c822
7
- data.tar.gz: ef886af90ffa9e762a1e319b40fb12b0136a8e256505a426ba72d8e96ce09daf7ff4ca6e55261730ade6348e46e8f4f7d1fb741983ceac6ded968a246d837697
6
+ metadata.gz: 59841ec3c62f9e141b8f1c03c56e68956c2aab46ea831c0377248bb060d548ae15808c258a0f145b0f8470758d7db646b264df2e330d9f80b663c2a4986385c6
7
+ data.tar.gz: 6f2c404714da657908fa5233748cdde6f8716423e9769716c9bc6d61a1afc7df5c5ec87bec5e05aacfa78a9062a39ae80566cf0b3a0df0bd71d4dca95086870a
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # textbringer-ghost_text
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/textbringer-ghost_text.svg)](https://badge.fury.io/rb/textbringer-ghost_text)
4
+
3
5
  [GhostText](https://github.com/GhostText/GhostText) plugin for Textbringer.
4
6
 
5
7
  ## Installation
@@ -2,5 +2,5 @@
2
2
 
3
3
  require_relative "ghost_text/version"
4
4
  require_relative "ghost_text/config"
5
- require_relative "ghost_text/web_socket_server"
5
+ require_relative "ghost_text/server"
6
6
  require_relative "ghost_text/commands"
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- define_command(:ghost_text_start) do
3
+ define_command(:ghost_text_start,
4
+ doc: "Start GhostText server") do
5
+ host = CONFIG[:ghost_text_host]
6
+ port = CONFIG[:ghost_text_port]
7
+ message("Start GhostText server: http://#{host}:#{port}")
4
8
  background do
5
9
  thin = Rack::Handler.get("thin")
6
- thin.run(Textbringer::GhostText::WebSocketServer.new,
7
- Host: CONFIG[:ghost_text_host],
8
- Port: CONFIG[:ghost_text_port]) do |server|
10
+ app = Rack::ContentLength.new(Textbringer::GhostText::Server.new)
11
+ thin.run(app, Host: host, Port: port) do |server|
9
12
  server.silent = true
10
13
  end
11
14
  end
@@ -3,4 +3,5 @@
3
3
  module Textbringer
4
4
  CONFIG[:ghost_text_host] = "127.0.0.1"
5
5
  CONFIG[:ghost_text_port] = 4001
6
+ CONFIG[:ghost_text_ping_interval] = 30
6
7
  end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "rack"
5
+ require "thin"
6
+ require "faye/websocket"
7
+
8
+ Faye::WebSocket.load_adapter("thin")
9
+
10
+ module Textbringer
11
+ module GhostText
12
+ class Server
13
+ def call(env)
14
+ if Faye::WebSocket.websocket?(env)
15
+ accept_client(env)
16
+ else
17
+ json = {
18
+ "WebSocketPort" => CONFIG[:ghost_text_port],
19
+ "ProtocolVersion" => 1
20
+ }.to_json
21
+ [200, {'Content-Type' => 'application/json'}, [json]]
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def accept_client(env)
28
+ ws = Faye::WebSocket.new(env, nil,
29
+ ping: CONFIG[:ghost_text_ping_interval])
30
+ next_tick! do
31
+ setup_buffer(ws)
32
+ end
33
+ ws.rack_response
34
+ end
35
+
36
+ def setup_buffer(ws)
37
+ buffer = Buffer.new_buffer("*GhostText*")
38
+ switch_to_buffer(buffer)
39
+
40
+ syncing_from_remote_text = false
41
+
42
+ ws.on :message do |event|
43
+ data = JSON.parse(event.data)
44
+ next_tick do
45
+ syncing_from_remote_text = true
46
+ begin
47
+ buffer.replace(data["text"])
48
+ if pos = data["selections"]&.dig(0, "start")
49
+ byte_pos = data["text"][0, pos].bytesize
50
+ buffer.goto_char(byte_pos)
51
+ end
52
+ ensure
53
+ syncing_from_remote_text = false
54
+ end
55
+ if (title = data['title']) && !title.empty?
56
+ buffer.name = "*GhostText:#{title}*"
57
+ end
58
+ switch_to_buffer(buffer)
59
+ end
60
+ end
61
+
62
+ ws.on :close do |event|
63
+ ws = nil
64
+ next_tick do
65
+ kill_buffer(buffer, force: true)
66
+ end
67
+ end
68
+
69
+ buffer.on :modified do
70
+ unless syncing_from_remote_text
71
+ pos = buffer.substring(0, buffer.point).size
72
+ data = {
73
+ "text" => buffer.to_s,
74
+ "selections" => [{ "start" => pos, "end" => pos }]
75
+ }
76
+ ws&.send(data.to_json)
77
+ end
78
+ end
79
+
80
+ buffer.on :killed do
81
+ ws&.close
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Textbringer
4
4
  module GhostText
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_runtime_dependency "textbringer", ">= 0.2.3"
24
+ spec.add_runtime_dependency "textbringer", ">= 0.2.5"
25
25
  spec.add_runtime_dependency "thin"
26
26
  spec.add_runtime_dependency "faye-websocket"
27
27
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: textbringer-ghost_text
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shugo Maeda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-02 00:00:00.000000000 Z
11
+ date: 2017-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: textbringer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.2.3
19
+ version: 0.2.5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.2.3
26
+ version: 0.2.5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: thin
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -97,8 +97,8 @@ files:
97
97
  - lib/textbringer/ghost_text.rb
98
98
  - lib/textbringer/ghost_text/commands.rb
99
99
  - lib/textbringer/ghost_text/config.rb
100
+ - lib/textbringer/ghost_text/server.rb
100
101
  - lib/textbringer/ghost_text/version.rb
101
- - lib/textbringer/ghost_text/web_socket_server.rb
102
102
  - lib/textbringer_plugin.rb
103
103
  - textbringer-ghost_text.gemspec
104
104
  homepage: https://github.com/shugo/textbringer-ghost_text
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "json"
4
- require "rack"
5
- require "thin"
6
- require "faye/websocket"
7
-
8
- Faye::WebSocket.load_adapter("thin")
9
-
10
- module Textbringer
11
- module GhostText
12
- class WebSocketServer
13
- def call(env)
14
- if Faye::WebSocket.websocket?(env)
15
- ws = Faye::WebSocket.new(env)
16
-
17
- next_tick do
18
- buffer = Buffer.new_buffer("*GhostText*")
19
- switch_to_buffer(buffer)
20
-
21
- remote_text = nil
22
-
23
- buffer.on_modified do
24
- text = buffer.to_s
25
- if text != remote_text
26
- pos = buffer.substring(0, buffer.point).size
27
- data = {
28
- "text" => text,
29
- "selections" => [{ "start" => pos, "end" => pos }]
30
- }
31
- ws.send(data.to_json)
32
- remote_text = text
33
- end
34
- end
35
-
36
- ws.on :message do |event|
37
- data = JSON.parse(event.data)
38
- next_tick do
39
- buffer.composite_edit do
40
- remote_text = data["text"]
41
- buffer.delete_region(buffer.point_min, buffer.point_max)
42
- buffer.insert(remote_text)
43
- pos = data["selections"]&.dig(0, "start")
44
- if pos
45
- byte_pos = remote_text[0, pos].bytesize
46
- buffer.goto_char(byte_pos)
47
- end
48
- end
49
- end
50
- end
51
-
52
- ws.on :close do |event|
53
- next_tick do
54
- kill_buffer(buffer, force: true)
55
- end
56
- ws = nil
57
- end
58
- end
59
- ws.rack_response
60
- else
61
- json = {
62
- "WebSocketPort" => 4001,
63
- "ProtocolVersion" => 1
64
- }.to_json
65
- [200, {'Content-Type' => 'application/json'}, [json]]
66
- end
67
- end
68
- end
69
- end
70
- end