Permalink
Browse files

improve style printing, add word wrapping

  • Loading branch information...
1 parent 91f8725 commit 95907f36ac95bbb42e26a21e0d58bb53c0a7a25a @ziz committed Apr 23, 2012
Showing with 76 additions and 25 deletions.
  1. +76 −25 sleepmask.rb
View
@@ -16,7 +16,7 @@
$interpreters = {
:glulxe => File.join(execpath, '..', 'glulxe-047-rem', 'glulxe'),
:nitfol => File.join(execpath, '..', 'nitfol-0.5-rem', 'remnitfol'),
- :cheapglulxe => File.join(execpath, '..', 'glulxe-rem', 'glulxe'),
+ :cheapglulxe => File.join(execpath, '..', 'glulxe-047-rem', 'glulxe'),
:olddebugcheapnitfol => File.join(execpath, '..', 'nitfol-0.5-rem', 'remnitfol') + " -i -no-spell",
:fizmo => File.join(execpath, '..', 'fizmo-rem', 'fizmo-glktermw', 'fizmo-glktermw'),
:debugcheapnitfol => File.join(execpath, '..', 'fizmo-rem', 'fizmo-glktermw', 'fizmo-glktermw'),
@@ -95,6 +95,13 @@
exit
end
+def word_wrap(text, line_width = nil)
+ width = line_width.nil? ? $options[:width] : line_width
+ text.split("\n").collect do |line|
+ line.length > width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip : line
+ end * "\n"
+end
+
class RemHandler < EM::Connection
attr_reader :queue
@@ -184,33 +191,72 @@ def post_init
send_data(init.to_json)
end
- def run_to_s(run)
+ def run_to_s(run, opts = {})
+ options = {
+ :solo => false
+ }
+
+ options.update(opts)
+
close = false
- s = ""
- if run[:style] == "emphasized"
+ line = run[:text]
+ sopen = ""
+ sclose = ""
+
+ case run[:style]
+ when "emphasized"
run[:style] = "em"
+ when "user1"
+ run[:style] = "u1"
+ when "user2"
+ run[:style] = "u2"
end
- if run[:style] == "header"
- s += "# "
- elsif run[:style] == "subheader"
- s += "## "
- elsif run[:style] == "alert"
- s += "** "
- elsif run[:style] == "input"
- s += "\n> "
- run[:text] = run[:text].upcase
- elsif ["normal", "preformatted"].index(run[:style]).nil?
- close = true
- s += "<#{run[:style]}>"
- end
- s += "#{run[:text]}"
- if run.has_key? :hyperlink and !run[:hyperlink].empty?
- s += "< #{run[:hyperlink]} >"
- end
- if close
- s += "</#{run[:style]}>"
+ case run[:style]
+ when "header"
+ if options[:solo]
+ sopen = "# "
+ sclose = ""
+ else
+ sopen = "<b>"
+ sclose = "</b>"
+ end
+ when "subheader"
+ if options[:solo]
+ sopen = "## "
+ sclose = ""
+ else
+ sopen = "<i>"
+ sclose = "</i>"
+ end
+ when "alert"
+ if options[:solo]
+ sopen = "** "
+ sclose = ""
+ else
+ sopen = "<strong>"
+ sopen = "</strong>"
+ end
+ when "input"
+ sopen = "\n> "
+ sclose = "\n"
+ line = line.upcase
+ when "normal"
+ sopen = ""
+ sclose = ""
+ when "preformatted"
+ sopen = ""
+ sclose = ""
+ else
+ sopen = "<#{run[:style]}>"
+ sclose = "</#{run[:style]}>"
end
+
+ s = "#{sopen}#{line}#{sclose}"
+ #s = word_wrap(run[:text]).split("\n").collect do |l|
+ #"#{sopen}#{l}#{sclose}"
+ #end * "\n"
+
return s
end
@@ -272,8 +318,12 @@ def object_parsed(obj)
content[:text].each do |text|
s = ""
if text.has_key? :content and !text[:content].empty?
- text[:content].each do |run|
- s += run_to_s(run)
+ if text[:content].length == 1
+ s = run_to_s(text[:content][0], {:solo => true})
+ else
+ text[:content].each do |run|
+ s += run_to_s(run)
+ end
end
end
@@ -285,6 +335,7 @@ def object_parsed(obj)
end
end
end
+ window[:buffer] = word_wrap(window[:buffer].join("\n")).split("\n")
puts window[:buffer].join("\n")
window[:buffer] = [window[:buffer].last]
end

0 comments on commit 95907f3

Please sign in to comment.