GTK+中文社区(gtk.awaysoft.com)

 找回密码
 马上加入

QQ登录

只需一步,快速开始

查看: 2989|回复: 0

用gtk ruby写的简易irb

[复制链接]

该用户从未签到

发表于 2015-6-22 22:29:07 | 显示全部楼层 |阅读模式
本帖最后由 africwildman 于 2015-6-24 00:27 编辑

就是调用eval执行表达式,参考了一下gnumeric里的py-console.c
代码:
  1. #!/usr/bin/env ruby
  2. require 'gtk3'
  3. require 'base64'
  4. require 'stringio'
  5. #base64格式的图标文件
  6. ICON=<<ICON32_EOF
  7. iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBI
  8. WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3wUBBSERvwQbtQAABfxJREFUWMPtl22IHWcVx3/neZ65
  9. dzfJvuV9m8Q0sSmUQkJKA9KgqE2tbxEbilEQQa1okNaiVSspiGApforf+kX7kmA0WLGhxCZKExGt
  10. 2F2bNJpsG2Oat91kX+/unb33zp2Z5zl+mLuxqYu76TelDwyXGebO/3f+5zznzMC76391aQg3dP+e
  11. rvKs1+WGhVURkbeeb/fVyU80xyZkdP9ToXLkkNbPvakhycJI3tQxMQONXPc9XMunf7C8g++PxO8M
  12. YEZYVWmODi8YPfDswxPPH/h29vpr3ZEGuu76IAt2foWxX/2CiZf/QKVaZVKFKRx1a8dTaz/02Gj8
  13. t++t6uGJwcq157r5iE9fODcj3nXpwN49V5/72RerR39LZKEUOTCWyWPHWPiBeyi9/8M6dPiQKIZU
  14. DB6Dt2ZJ09nfoLrmCbk+5jkBBl88yKK161FV1//Vzx8bPnxwc4iniayACAQFBI2g/o8B1u95StJ4
  15. In/58R9ZiawsQDBeMepXP76s497dcGTeAJVTJ+m5fSPjJ/pWvLRty18qff1rXSQYI+RKS7xYIQfp
  16. 7GLoW5+l7cqQ27hxTTj9+hUSRXIgBCWD25kvgE8a2LZ26oOX1v/+/o88H58ZWGtLQgBUQWd+ZyCM
  17. 4L3n6uG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nB+e5j1dZUJQRqZyMFyr+qwl
  31. motgEFZh6HGWkrWUnGGTK3M2a3A5TfCipEbnlwKBPyqQCTR8oK2SYHzQem5EW7YGwKN4gTaEm0xE
  32. u3WUnVBylpKzlJ1l68J2TtSqXMmajEmIZwO4rhM+s6KDL000BgL8vFmyZCXHkEIc560pqISZzBqD
  33. tZY255iyyoTxVCQwRaCKJ9acWAKbVyxLNizpGb2Sp/U5HfBJWtjr3Jdza97IrXmsGVm3oFkM41yU
  34. zDiCMeMiZlRVx6fQUdUwFrwfD15HQcdKRsbKxlQ6y6XmEkK6srszKbeXL3C+euPv/892Rr3PdLRv
  35. +HFH1HWj/z24uuOdf3j8tLs06/WfLIze/Sr7/1r/Av37KHozwoGeAAAAAElFTkSuQmCC
  36. ICON32_EOF
  37. #把图片文件转换成gtk的pixbuf格式
  38. def base642pixbuf data
  39.   decode = Base64.decode64 data
  40.   loader = Gdk::PixbufLoader.new
  41.   loader.write decode
  42.   loader.close
  43.   return loader.pixbuf
  44. end
  45. #核心代码1 binding=Object.new.send :binding
  46. #没有用irb,看了半天没看明白应该怎么调用,于是自己实现一个最简单的irb
  47. binding=Object.new.send :binding
  48. history,i=[],0
  49. out=StringIO.new
  50. out.sync=true
  51. #把标准输出改到字符流,然后textview读取字符流内容
  52. $stdout=out
  53. Gtk.init
  54. window=Gtk::Window.new
  55. window.resize 400,300
  56. window.title="简陋的gtk-irb"
  57. window.icon=base642pixbuf ICON
  58. window.signal_connect("destroy") do
  59.   Gtk.main_quit
  60. end
  61. box=Gtk::Box.new :vertical
  62. window << box
  63. textview=Gtk::TextView.new
  64. textview.wrap_mode=:char
  65. textview.editable=false
  66. buffer=textview.buffer
  67. box.add Gtk::ScrolledWindow.new.add(textview),expand:true,fill:true
  68. hbox=Gtk::Box.new :horizontal
  69. prompt=Gtk::Label.new "[#{history.count+1}] > "
  70. entry=Gtk::Entry.new
  71. entry.signal_connect("key-press-event") do |widget,event|
  72. #具体键盘信号对应的名称需要在/usr/include/gtk-3.0/gdk/gdkkeysyms.h里面查
  73.   if event.keyval==Gdk::Keyval::KEY_Return or
  74.     event.keyval==Gdk::Keyval::KEY_KP_Enter
  75.     buffer.insert buffer.end_iter,"#{prompt.text}#{entry.text}\n"
  76.     begin
  77. #核心代码2 binding.eval(entry.text)
  78.       buffer.insert buffer.end_iter,
  79.         "=> #{binding.eval(entry.text).inspect}\n#{out.string}"
  80.     rescue Exception
  81.       buffer.insert buffer.end_iter,"#{$!.message}\n#{$!.backtrace.join("\n")}\n"
  82.     rescue SystemExit
  83.       buffer.insert buffer.end_iter,"#{$!.message}\n#{$!.backtrace.join("\n")}\n"
  84.     end
  85.     history.unshift entry.text
  86.     entry.text,i,out.string="",0,""
  87.     prompt.text="[#{history.count+1}] > "
  88. #textview的末尾显示在屏幕上,代码参考的gnumeric的py-console.c
  89.     textview.scroll_mark_onscreen(buffer.create_mark(
  90.       "textend",buffer.end_iter,false))
  91.   elsif event.keyval==Gdk::Keyval::KEY_Up
  92.     entry.text=history[i]
  93.     i+=1 if i.abs<(history.count-1)
  94.     entry.position=entry.text.length
  95.   elsif event.keyval==Gdk::Keyval::KEY_Down
  96.     if i>0
  97.       i-=1
  98.       entry.text=history[i]
  99.       entry.position=entry.text.length
  100.     else
  101.       entry.text=""
  102.     end
  103.   end
  104. end
  105. hbox<< prompt
  106. hbox.add entry,expand:true,fill:true
  107. box<< hbox
  108. entry.set_has_focus true
  109. window.show_all
  110. Gtk.main
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?马上加入

x
*滑块验证:
您需要登录后才可以回帖 登录 | 马上加入

本版积分规则

申请友链|Archiver|小黑屋|手机版|GTK+中文社区 ( 粤ICP备13080851号 )

我要啦免费统计

GMT+8, 2024-12-22 02:35 , Processed in 0.027910 second(s), 8 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表