ãããæããããè¨äºãæ¸ãããã ããã©ã
æãã£ã¨ã«ãæ¹é¢ãããã¾ããã®ãã®è¨äºã«é¢ãã質åãæ¥ã¦ãããã«ãå ¥åã渡ãã«ã¯ã©ãããã°ããããã¨ãã追å 質åãããã®ã§ãã£ãã
試ããæãããªãã¨ãç´ ç´ã«åããªãã£ãã®ã§ãã©ãããã°ããããæ¸ãæ®ãã¦ãããã¨æãã
ãã¤ã³ãã¯2ã¤ã§ã
- ttyã®å
¥åãçµäºãããããã«ã
^D
ã«ç¸å½ããEOF
EOT
ã³ã¼ããã¨"\u{04}"
ãéãè¾¼ã - å
¥åããè¡ã¯ãã®ã¾ã¾
pty_out
ããåããã¨ããã¨ãã£ã¼ãããã¯ãã¦åå¾ã§ãã¦ãã¾ãã®ã§ã1è¡æ¨ã¦ã
ããã§å¤§ä½æ³å®ããæåã«ãªãã¯ãããã ãå½ç¶stdout/errãæ··ããã®ã§ãæ¨æºåºåãã¨ã©ã¼åºåã®ãã¹ããªã Open3.capture3
ãªã©ãé©åã ã¨æãã
ãµã³ãã«ã³ã¼ã
require 'pty' status = nil PTY.spawn("tac") do |pty_out, pty_in, pid| 3.times do |i| # æ¸ãããæ¨ã¦ã pty_in.write "Hola" * (i+1) + "\n" pty_out.gets end # ^D ç¸å½ã®å¶å¾¡æå pty_in.puts "\u{04}" pty_out.gets while !(status ||= PTY.check(pid)) && l = pty_out.gets puts "PTY(out): #{l}" end # MacOS ã§ã¯ä¸ã®ãã§ãã¯ã§statusãåã¾ãåã«ã # pty_out.gets ã§nilãè¿ãã¦ã«ã¼ããæããã®ã§æ¹ãã¦åå¾ãã until status ||= PTY.check(pid) end end p status
çµæ
$ ruby pty.rb PTY(out): HolaHolaHola PTY(out): HolaHola PTY(out): Hola #<Process::Status: pid 2217 exit 0>