Skip to content

Commit

Permalink
you can now enter multiple lines by pressing Alt+Return
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentisambart committed Nov 2, 2008
1 parent c12f47a commit 0ad6013
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
12 changes: 6 additions & 6 deletions config/build.yml
@@ -1,8 +1,8 @@
:name: MacIrb
:load: lib/application.rb
:version: "1.0"
:icon: resources/HotCocoa.icns
:resources:
name: MacIrb
load: lib/application.rb
version: "1.0"
icon: resources/HotCocoa.icns
resources:
- resources/**/*.*
:sources:
sources:
- lib/**/*.rb
74 changes: 49 additions & 25 deletions lib/application.rb
Expand Up @@ -2,10 +2,10 @@
framework 'webkit'

# TODO:
# - autoscroll
# - stdout/stderr, later stdin
# - wraps too long text
# - allows Alt+Return
# - stdin
# - wrap too long text
# - history (up/down or maybe alt+up/alt+down)
# - copy/paste
class Application
include HotCocoa

Expand All @@ -18,7 +18,8 @@ def write(str)
str.length
end
def puts(str)
write("#{str}\n")
write str
write "\n"
nil
end
end
Expand Down Expand Up @@ -48,6 +49,7 @@ def base_html
app.delegate = self

window :frame => [100, 100, 900, 500], :title => "MacIrb" do |win|
@win = win
win.will_close { exit }
win.contentView.margin = 0
@web_view = web_view(:layout => {:expand => [:width, :height]})
Expand All @@ -60,7 +62,9 @@ def base_html
end

def webView view, didFinishLoadForFrame: frame
$stdout = Writer.new(self)
writer = Writer.new(self)
$stdout = writer
#$stderr = writer # for debugging it may be better to disable this (and look in Console.app)
write_prompt
end

Expand All @@ -72,14 +76,14 @@ def command_line
document.getElementById('command_line')
end

def webView webView, shouldInsertText: text, replacingDOMRange: range, givenAction: action
if text == ?\n
perform_action
false
else
true
end
end
# def webView webView, shouldInsertText: text, replacingDOMRange: range, givenAction: action
# if text == ?\n
# perform_action
# false
# else
# true
# end
# end

def add_div(text)
doc = document
Expand All @@ -98,29 +102,50 @@ def write_text(text)
write_element(span)
end

def scroll_to_bottom
body = document.body
body.scrollTop = body.scrollHeight
@web_view.setNeedsDisplay true
end

def write_prompt
if command_line
command_line.setAttribute('contentEditable', value: nil)
command_line.setAttribute('id', value: nil)
end

table = document.createElement('table')
row = table.insertRow(0)
prompt = row.insertCell(-1)
prompt.setAttribute('style', value: 'vertical-align: top;')
prompt.innerText = '>>'
typed_text = row.insertCell(-1)
if command_line
command_line.setAttribute('contentEditable', value: nil)
command_line.setAttribute('id', value: nil)
end
typed_text.setAttribute('contentEditable', value: 'true')
typed_text.setAttribute('id', value: 'command_line')
typed_text.setAttribute('style', value: 'width: 100%;')
write_element(table)

command_line.focus
scroll_to_bottom
responder = @win.firstResponder
# webView:shouldInsertText:replacingDOMRange:givenAction: is not powerful enough for us
# to do all our work so we add our keyDown method on the firstResponder (a child of the WebView)
class << responder
alias :base_key_down :keyDown
attr_writer :action_performer
def keyDown(e)
# if the user presses Return, the entered text is evaluated,
# but if also presses Alt, a carriage return is inserted
if e.characters == ?\r and (e.modifierFlags & NSAlternateKeyMask == 0)
@action_performer.perform_action
else
base_key_down(e)
end
end
end
responder.action_performer = self
end

def scroll_to_bottom
body = document.body
body.scrollTop = body.scrollHeight
@web_view.setNeedsDisplay true
end

def perform_action
@line_num += 1
command = command_line.innerText.tr(' ', ' ') # replace non breakable spaces by normal spaces
Expand All @@ -146,7 +171,6 @@ def perform_action
add_div("#{e.class.name}: #{e.message}" + (backtrace.empty? ? '' : "\n#{backtrace.join("\n")}"))
end
write_prompt
scroll_to_bottom
end

# file/open
Expand Down
4 changes: 4 additions & 0 deletions lib/menu.rb
Expand Up @@ -18,6 +18,10 @@ def application_menu
file.item :new, :key => "n"
file.item :open, :key => "o"
end
main.submenu :edit do |file|
file.item :copy, :key => "c"
file.item :paste, :key => "v"
end
main.submenu :window do |win|
win.item :minimize, :key => "m"
win.item :zoom
Expand Down

0 comments on commit 0ad6013

Please sign in to comment.