以åãnoteã«æ¸ããè¨äºã ãã©ããã¡ãã«ãã
CUIã®ã¢ããªã±ã¼ã·ã§ã³ãä½ãã¨ãã«ããå©ç¨ãããã®ããCursesã¨ããã©ã¤ãã©ãªã
Rubyã®å ´åãæºæ¨æºã¨ããããcursesã©ã¤ãã©ãªãããï¼â»1.9.3ã¾ã§ã¯æ¨æºæ·»ä»ã©ã¤ãã©ãªï¼ãããã使ãã®ãä¸è¬çã ã¨æãã
ãã ãWebä¸ã«è§£èª¬ãã»ã¨ãã©ãªããçããªããªãå¼·ãããããã使ããã¨ããã¨ãããªã骨ãæããã
ããã§ããã®è¨äºã§ã¯cursesã©ã¤ãã©ãªã«ã¤ãã¦ããµã³ãã«ã³ã¼ãã交ããªãã説æãã¦ããããã¨æãã
åæ
ãã®è¨äºã¯ã以ä¸ã®åæã§æ¸ããã¦ããï¼
- Rubyã®åºæ¬çãªææ³ãç解ãã¦ããã
- åºæ¬çãªã³ãã³ãã©ã¤ã³æä½ãåºæ¥ãã
ãªãããµã³ãã«ã³ã¼ãã®åä½ç¢ºèªã¯ãMacã§Ruby 2.0.0ã使ã£ã¦ãã£ã¦ããã
ï¼ãã以å¤ã®ç°å¢ã§ããCursesã使ãããªãåé¡ãªãåãã¨ã¯æãï¼
æºå
Ruby 2.0.0以ä¸ã®å ´åãcursesã©ã¤ãã©ãªã¯æ¨æºã§ã¯æ·»ä»ããã¦ããªãã®ã§ãRubyGemsããã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã
ã³ãã³ãã©ã¤ã³ãã以ä¸ã®ã³ãã³ããå®è¡ãã¦ã¤ã³ã¹ãã¼ã«ã
ï¼$
ã¯ã³ãã³ãããã³ããã以ä¸ãåæ§ï¼
$ gem install curses
cursesã®åä½ç¢ºèª
ã¾ãã¯ãcursesã®åä½ç¢ºèªããã
以ä¸ã®ãµã³ãã«ã³ã¼ãã§cursesã®åä½ç¢ºèªããã¦ã¿ãï¼
#==================== # hello_curses.rb #-------------------- # cursesã®ãµã³ãã«ã³ã¼ã #==================== # cursesã©ã¤ãã©ãªãèªã¿è¾¼ã require "curses" # Cursesã¢ã¸ã¥ã¼ã«ã®ã¢ã¸ã¥ã¼ã«é¢æ°ãç°¡åã«å¼ã³åºããããã«ãã include Curses # cursesã©ã¤ãã©ãªã®åæå init_screen begin # å ¥åã¢ã¼ããcbreakã¢ã¼ãã« crmode # ç»é¢ä¸å¤®ã«"Hit any key"ã¨è¡¨ç¤ºãã¦å ¥åå¾ ã¡ setpos((lines - 5) / 2, (cols - 10) / 2) addstr("Hit any key") refresh getch # ã¦ã£ã³ãã¦ãçæãã¦"Hello, Curses!"ã¨è¡¨ç¤ºãã¦å ¥åå¾ ã¡ message = "Hello, Curses!" width = message.length + 6 win = Window.new(5, width, (lines - 5) / 2, (cols - width) / 2) win.box('|', '-') win.setpos(2, 3) win.addstr(message) win.refresh win.getch # ã¦ã£ã³ãã¦ãéãã win.close refresh ensure # cursesã©ã¤ãã©ãªã®çµäºå¦ç close_screen end
ãã®ãµã³ãã«ã³ã¼ããå®è¡ããã«ã¯ã次ã®ããã«ããï¼
$ ruby hello_curses.rb
Cursesã使ããç°å¢ã§ãcursesã©ã¤ãã©ãªãæ£ããã¤ã³ã¹ãã¼ã«ããã¦ããã°ãåé¡ãªãå®è¡åºæ¥ãã¯ãã
ä¸è¨ã®ãµã³ãã«ã³ã¼ããå®è¡ããã¨ãã¿ã¼ããã«ã®ä¸å¤®ã«"Hit any key"ã¨ããæåã表示ãããã
ãã®ã¡ãã»ã¼ã¸ã«å¾ã£ã¦ãã¼ãã¼ãã®ä»»æã®ãã¼ãå©ãã¨ã"Hit any key"ã¨ããæåã¯æ¶ããåè§ãæ ã®ä¸ã«"Hello, Curses!"ã¨è¡¨ç¤ºãããã
ããã¦ãããã«ãã¼ãã¼ãã®ä»»æã®ãã¼ãå©ãã¨ãããã°ã©ã ã¯çµäºããã
ãµã³ãã«ã³ã¼ãã§ãã£ã¦ããå 容ï¼ãã¦ã£ã³ãã¦ãããå ¥åã¢ã¼ãããªã©ï¼ã®èª¬æã¯ã¾ãå¾ã§ããã¨ãã¦ãã³ã¡ã³ããè¦ã¦ãããã°ãcursesã©ã¤ãã©ãªã使ãã¨ãã®å¤§éæãªæµãã次ã®ããã«ãªã£ã¦ãããã¨ãåããã¨æãï¼
- cursesã©ã¤ãã©ãªã®èªã¿è¾¼ã¿
- cursesã©ã¤ãã©ãªã®åæå
- ã¦ã£ã³ãã¦ãçæããããæåã表示ããããå ¥åãåãåã£ãããã
- cursesã©ã¤ãã©ãªã®çµäºå¦ç
ä»æ¥ã¯ããã¾ã§ï¼