File: with_term.rb

package info (click to toggle)
ruby-unicode-plot 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,492 kB
  • sloc: ruby: 2,871; makefile: 4
file content (29 lines) | stat: -rw-r--r-- 566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'stringio'

module Helper
  module WithTerm
    def with_sio(tty: true)
      sio = StringIO.new
      def sio.tty?; true; end if tty

      result = yield(sio)
      sio.close

      [result, sio.string]
    end

    def with_term(tty=true)
      with_sio(tty: tty) do |sio|
        begin
          orig_stdout, $stdout = $stdout, sio
          orig_env = ENV.to_h.dup
          ENV['TERM'] = 'xterm-256color'
          yield
        ensure
          $stdout = orig_stdout
          ENV.replace(orig_env) if orig_env
        end
      end
    end
  end
end