ai-chat 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 177299f56fc875c484f3b0552e93858c3f8da072ded5b81a67d891d9b7f7df3e
4
- data.tar.gz: c31a180346f098d9c70fc135bdd6556ae12d23f4a85a5618a1907a8f660c0996
3
+ metadata.gz: b938b2b70b76afcd57c639ccc5ed37db9fb49bf42edc422801264d980a7b1d69
4
+ data.tar.gz: a9fa2ad8737b7e3fcd6279b55569302be657272050a766e23729cd2af0eb504d
5
5
  SHA512:
6
- metadata.gz: a4adf79c5e126ee642511bd8a8d2fdbab92fef5d9a596cae6a416bed82861764c61ed80f17f5762e879dae4e8c604475e9c7f5bf82c70224b9bdc60552b936ad
7
- data.tar.gz: ca02e9d88b13569899ce3787159862b8f55d2c822e54d41851f43b7a94d22a35bf25794e6dbff25e71d04dc6cdf0d4da6b74c53b9b40476d68d42d8645a97268
6
+ metadata.gz: d97939df71ccd5093a4b2d7b972eb4cbcdf6f71d1aab4e585ee738f4f005b55123f44aa2a3275d5d35f7693671072cf88a98afedfd8b7d6a38b81529e0142bab
7
+ data.tar.gz: c1655ee6eae4657c0e7cf727719a236fbea11a593993c4876b91d2e611d6757ce740bae7c77ad7bcdcd016071862be0744ba7fa3aff7ce750d13f62769cf0bb7
data/ai-chat.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "ai-chat"
5
- spec.version = "0.8.0"
5
+ spec.version = "0.8.2"
6
6
  spec.authors = ["Raghu Betina", "Jelani Woods"]
7
7
  spec.email = ["raghu@firstdraft.com", "jelani@firstdraft.com"]
8
8
  spec.homepage = "https://github.com/firstdraft/ai-chat"
data/lib/ai/chat.rb CHANGED
@@ -262,7 +262,13 @@ module AI
262
262
  end
263
263
 
264
264
  def inspect
265
- AI.amazing_print(self, plain: !$stdout.tty?, multiline: true)
265
+ AI.amazing_print(self, plain: true, multiline: true)
266
+ end
267
+
268
+ # Same direct write as AI::Message#pretty_print, for the same reasons.
269
+ def pretty_print(q)
270
+ q.flush
271
+ q.output << AI.amazing_print(self, plain: !AI.color_printer?(q), multiline: true)
266
272
  end
267
273
 
268
274
  def to_html
data/lib/ai/items.rb CHANGED
@@ -28,15 +28,17 @@ module AI
28
28
  end
29
29
 
30
30
  def inspect
31
- build_output(html: false, plain: !$stdout.tty?)
31
+ build_output(html: false, plain: true)
32
32
  end
33
33
 
34
34
  def pretty_inspect
35
35
  "#{inspect}\n"
36
36
  end
37
37
 
38
+ # Same direct write as AI::Message#pretty_print, for the same reasons.
38
39
  def pretty_print(q)
39
- q.output << inspect
40
+ q.flush
41
+ q.output << build_output(html: false, plain: !AI.color_printer?(q))
40
42
  end
41
43
 
42
44
  def method_missing(method_name, *args, &block)
data/lib/ai/message.rb CHANGED
@@ -3,17 +3,21 @@
3
3
  module AI
4
4
  class Message < Hash
5
5
  def inspect
6
- AI.amazing_print(display_hash, plain: !$stdout.tty?, index: false)
6
+ formatted(plain: true)
7
7
  end
8
8
 
9
9
  def pretty_inspect
10
10
  "#{inspect}\n"
11
11
  end
12
12
 
13
- # IRB's ColorPrinter calls pretty_print and re-colorizes text,
14
- # which escapes our ANSI codes. Write directly to output to bypass.
13
+ # The console's path, and so the only place color is added (see
14
+ # AI.color_printer?). IRB's ColorPrinter re-colorizes text, which
15
+ # escapes our ANSI codes, so write directly to output to bypass it,
16
+ # but flush first: inside an Array, PP is still buffering the "[" and
17
+ # earlier separators, so an unflushed write lands ahead of them.
15
18
  def pretty_print(q)
16
- q.output << inspect
19
+ q.flush
20
+ q.output << formatted(plain: !AI.color_printer?(q))
17
21
  end
18
22
 
19
23
  def to_html
@@ -22,6 +26,10 @@ module AI
22
26
 
23
27
  private
24
28
 
29
+ def formatted(plain:)
30
+ AI.amazing_print(display_hash, plain: plain, index: false)
31
+ end
32
+
25
33
  def display_hash
26
34
  AI.truncate_data_uris(to_h)
27
35
  end
data/lib/ai-chat.rb CHANGED
@@ -14,6 +14,25 @@ module AI
14
14
  AmazingPrint::Inspector.new(**options).awesome(object)
15
15
  end
16
16
 
17
+ # Whether pretty_print is drawing into a console that shows color: IRB
18
+ # (and so rails console) or Pry, with that console's color setting on.
19
+ # IRB always hands its printer colorize: true and leaves the real decision
20
+ # (a color terminal, and no --nocolorize) to IRB::Color.colorable?.
21
+ #
22
+ # inspect never adds color, because its String also ends up in places that
23
+ # can't read terminal escape codes: the Better Errors console in the
24
+ # browser, log files, ERB. Asking $stdout.tty? doesn't tell those apart: a
25
+ # Rails server started from a terminal has a TTY on $stdout, too.
26
+ def self.color_printer?(q)
27
+ if defined?(IRB::ColorPrinter) && q.is_a?(IRB::ColorPrinter)
28
+ q.instance_variable_get(:@colorize) != false && IRB::Color.colorable?
29
+ elsif defined?(Pry::ColorPrinter) && q.is_a?(Pry::ColorPrinter)
30
+ Pry.config.color
31
+ else
32
+ false
33
+ end
34
+ end
35
+
17
36
  # Recursively truncate base64 data URIs in nested structures for cleaner output
18
37
  def self.truncate_data_uris(obj)
19
38
  case obj
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai-chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raghu Betina
@@ -156,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  - !ruby/object:Gem::Version
157
157
  version: '0'
158
158
  requirements: []
159
- rubygems_version: 4.0.3
159
+ rubygems_version: 4.0.10
160
160
  specification_version: 4
161
161
  summary: A beginner-friendly Ruby interface for OpenAI's API
162
162
  test_files: []