campline 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "tinder"
4
- gem "rb-readline"
5
4
  gem "cli-colorize"
6
5
 
7
6
  # Add dependencies to develop your gem here.
@@ -18,7 +18,6 @@ The client will prompt for your password and that's it - you're in!
18
18
 
19
19
  == Copyright
20
20
 
21
- Partially based on campfire-cli, by Petyo Ivanov
22
-
23
- Copyleft (c) 2012 Herval. See LICENSE.txt for further details.
21
+ Partially based on campfire-cli, by Petyo Ivanov. STDIN/OUT tricks by http://www.rubyquiz.com/quiz45.html (thanks @cfcosta!)
24
22
 
23
+ Copyleft (c) 2012 Herval. See LICENSE.txt for further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -7,9 +7,18 @@ if ARGV.size != 3
7
7
  exit 1
8
8
  end
9
9
 
10
- domain, room, username = *ARGV
10
+ begin
11
+ domain, room, username = *ARGV
11
12
 
12
- puts "Type in your password:"
13
- password = $stdin.gets.strip
13
+ begin
14
+ %x{stty -echo}
15
+ puts "Type in your password:"
16
+ password = $stdin.gets.strip
17
+ ensure
18
+ %x{stty raw -echo}
19
+ end
14
20
 
15
- Campline::Client.new(domain, room, username, password).listen!
21
+ Campline::Client.new(domain, room, username, password).listen!
22
+ ensure
23
+ %x{stty -raw echo}
24
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "campline"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Herval Freire"]
@@ -42,7 +42,6 @@ Gem::Specification.new do |s|
42
42
 
43
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
44
  s.add_runtime_dependency(%q<tinder>, [">= 0"])
45
- s.add_runtime_dependency(%q<rb-readline>, [">= 0"])
46
45
  s.add_runtime_dependency(%q<cli-colorize>, [">= 0"])
47
46
  s.add_development_dependency(%q<pry>, [">= 0"])
48
47
  s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
@@ -50,7 +49,6 @@ Gem::Specification.new do |s|
50
49
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
51
50
  else
52
51
  s.add_dependency(%q<tinder>, [">= 0"])
53
- s.add_dependency(%q<rb-readline>, [">= 0"])
54
52
  s.add_dependency(%q<cli-colorize>, [">= 0"])
55
53
  s.add_dependency(%q<pry>, [">= 0"])
56
54
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
@@ -59,7 +57,6 @@ Gem::Specification.new do |s|
59
57
  end
60
58
  else
61
59
  s.add_dependency(%q<tinder>, [">= 0"])
62
- s.add_dependency(%q<rb-readline>, [">= 0"])
63
60
  s.add_dependency(%q<cli-colorize>, [">= 0"])
64
61
  s.add_dependency(%q<pry>, [">= 0"])
65
62
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
- require 'readline'
2
+ require 'thread'
3
3
  require 'tinder'
4
+ require 'io/wait'
4
5
  require 'yaml'
5
6
  require 'cli-colorize'
6
7
 
@@ -17,30 +18,32 @@ module Campline
17
18
  @password = password
18
19
  @user_id = nil
19
20
  @room_users = []
21
+ @output_buffer = ""
22
+ @input_buffer = Queue.new
20
23
  end
21
24
 
22
25
  def print_message(msg)
23
- next if msg[:user] && msg[:user][:id] == @user_id
26
+ return if (msg[:user] && msg[:user][:id] == @user_id)
24
27
  case msg[:type]
25
28
  when "SoundMessage" then
26
- puts "#{green(msg[:user][:name])} played some annoying sound"
29
+ @input_buffer << "#{green(msg[:user][:name])} played some annoying sound"
27
30
  when "PasteMessage" then
28
- puts "#{green(msg[:user][:name])}: #{msg[:body]}"
31
+ @input_buffer << "#{green(msg[:user][:name])}: #{msg[:body]}"
29
32
  when "TextMessage" then
30
- puts "#{green(msg[:user][:name])}: #{msg[:body]}"
33
+ @input_buffer << "#{green(msg[:user][:name])}: #{msg[:body]}"
31
34
  end
32
35
  end
33
36
 
34
37
  def commands
35
38
  {
36
- "/help" => lambda { puts "Available commands: /users (list users on the room), /exit (quit!)"},
39
+ "/help" => lambda { print "\r\nAvailable commands: /users (list users on the room), /exit (quit!)"},
37
40
  "/exit" => lambda { @campfire_room.leave; exit; },
38
41
  "/users" => lambda { list_users }
39
42
  }
40
43
  end
41
44
 
42
45
  def list_users
43
- puts white("In the room right now: #{@room_users.join(', ')}")
46
+ print white("\r\nIn the room right now: #{@room_users.join(', ')}")
44
47
  end
45
48
 
46
49
  def update_user_list
@@ -48,13 +51,25 @@ module Campline
48
51
  @room_users = new_list if @room_users.empty?
49
52
 
50
53
  new_guys = new_list - @room_users
51
- new_guys.each { |n| puts "#{n} joined the room" }
54
+ new_guys.each { |n| @input_buffer << "#{n} joined the room" }
52
55
 
53
56
  dead_guys = @room_users - new_list
54
- dead_guys.each { |n| puts "#{n} left the room" }
57
+ dead_guys.each { |n| @input_buffer << "#{n} left the room" }
55
58
  @room_users = new_list
56
59
  end
57
60
 
61
+ def backspace!
62
+ @output_buffer.chop!
63
+ go_back = "\r\e[0K" # return to beginning of line and use the ANSI clear command "\e" or "\003"
64
+ print "#{go_back}> #{@output_buffer}"
65
+ end
66
+
67
+ def show_prompt
68
+ puts "\r\n"
69
+ print "> #{@output_buffer}"
70
+ $stdout.flush
71
+ end
72
+
58
73
  def blue(str)
59
74
  colorize(str, :foreground => :blue)
60
75
  end
@@ -71,18 +86,32 @@ module Campline
71
86
  colorize(str, :background => :red)
72
87
  end
73
88
 
89
+ def send_line!
90
+ if commands[@output_buffer]
91
+ commands[@output_buffer].call
92
+ else
93
+ @campfire_room.speak @output_buffer
94
+ end
95
+ @output_buffer = ""
96
+ end
97
+
74
98
  def listen!
75
- puts "Logging in..."
76
- campfire = Campfire.new @domain, :username => @username, :password => @password, :ssl => true
99
+ puts "Logging in...\r\n"
100
+ begin
101
+ campfire = Campfire.new @domain, :username => @username, :password => @password, :ssl => true
102
+ rescue Tinder::AuthenticationFailed
103
+ raise "There was an authentication error - check your username and password\r\n"
104
+ end
77
105
  @user_id = campfire.me.id
78
106
 
79
- puts "Joining #{@room}..."
107
+ puts "Joining #{@room}...\r\n"
80
108
  @campfire_room = campfire.find_room_by_name @room
109
+ raise "Can't find room named #{@room}!\r\n" if @campfire_room.nil?
110
+
81
111
  @campfire_room.join
82
-
83
112
  update_user_list
84
113
 
85
- puts "You're up! For a list of available commands, type #{highlight('/help')}"
114
+ puts "You're up! For a list of available commands, type #{highlight('/help')}\r\n"
86
115
 
87
116
  Thread.new(@campfire_room) do |listener|
88
117
  while true
@@ -99,14 +128,33 @@ module Campline
99
128
  end
100
129
  end
101
130
 
102
- while msg = Readline.readline('', true)
103
- next if msg.strip.blank?
104
- if commands[msg]
105
- commands[msg].call
106
- else
107
- @campfire_room.speak msg
108
- end
109
- end
131
+ Thread.new do
132
+ while true #msg = Readline.readline('> ', true)
133
+ if $stdin.ready?
134
+ character = $stdin.getc
135
+ case character
136
+ when ?\C-c
137
+ break
138
+ when ?\r, ?\n
139
+ send_line!
140
+ show_prompt
141
+ when ?\u007F, ?\b
142
+ backspace!
143
+ else
144
+ @output_buffer << character
145
+ print character.chr
146
+ $stdout.flush
147
+ end
148
+ end
149
+
150
+ unless @input_buffer.empty? # read from server
151
+ puts "\r\n"
152
+ puts "#{@input_buffer.shift}\r\n" until @input_buffer.empty?
153
+ show_prompt
154
+ end
155
+
156
+ end
157
+ end.join
110
158
  end
111
159
  end
112
160
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: campline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,22 +27,6 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: rb-readline
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :runtime
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
30
  - !ruby/object:Gem::Dependency
47
31
  name: cli-colorize
48
32
  requirement: !ruby/object:Gem::Requirement
@@ -159,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
143
  version: '0'
160
144
  segments:
161
145
  - 0
162
- hash: 813841254707109310
146
+ hash: -3789922764660391293
163
147
  required_rubygems_version: !ruby/object:Gem::Requirement
164
148
  none: false
165
149
  requirements: