# # file:: cmd_edit.rb # author:: Jon A. Lambert # version:: 2.10.0 # date:: 06/25/2006 # # Additional Contributor: Craig Smith # # This source code copyright (C) 2005, 2006 by Jon A. Lambert # All rights reserved. # # Released under the terms of the TeensyMUD Public License # See LICENSE file for additional information. # module Cmd # @edits a string field on an object # Syntax: # @edit #<id> <field> # @edit sysmsg <field> # (ex. @edit #1 desc def cmd_edit(args) case args when nil, "" sendto("What??") when /sysmsg\s+(\w+)/ if world.is_admin? id @mode = :edit @editobj = world.msgs @editfield = $1.intern @editstr = world.msgs[$1.intern] || '' sendto(edit_display(@editstr)) else sendto("Only admins can edit sysmsgs") end when /#(\d+)\s+(\w+)/ o = get_object($1.to_i) if world.is_builder? id and not o.owner == id sendto("Builders can only edit objects they have created") return end case o when GameObject, Room, Character, Script, Exit if o.respond_to?($2) && o.respond_to?("#$2=") && o.send($2).class == String @mode = :edit @editobj = o @editfield = $2 @editstr = o.send $2 @editstr += "\n" # add an implicit \n sendto(edit_display(@editstr)) else sendto("Field #$2 not found on object.") end else sendto("No object.") end else sendto("What??") end end def edit_display(str) header =<<EOD ======== Teensy String Editor ======== Type .h on a new line for help Terminate with a @ on a blank line. ====================================== EOD i = 0 header + str.gsub(/^/){"#{i+=1}: "} end # Attempt to fix original word_wrap function # Word wrap needs to end with a \n for some reason :/ def word_wrap(s, len) lines = String.new line = String.new # remove \r\n s.gsub!(/[\r\n]/, " ") s.split(" ").each do |word| if word.size + line.size < len line += word + " " else lines += line + "\n" line = word end end lines += line if line.size > 0 lines += "\n" lines end def word_wrap_orig(s, len) str = s str.gsub!(/\n/,' ');str.squeeze!(' ') str.gsub!(/(\S{#{len}})(?=\S)/,'\1 ') str.scan(/(.{1,#{len}})(?:\s+|$)/).flatten.join("\n") end def edit_parser(args) case args when nil sendto("What??") when /^\.h/ sendto <<EOD @edit help (commands on blank line): .r /old/new/ - replace a substring .h - get help (this info) .s - show string so far .ww [width] - word wrap string (width optional) defaults to 76 .c - clear string so far .ld <num> - delete line <num> .li <num> <txt> - insert <txt> before line <num> .lr <num> <txt> - replace line <num> with <txt> @ - end string EOD when /^\.c/ @editstr = "" when /^\.s/ sendto(edit_display(@editstr)) when /^\.r\s+\/(.+)?\/(.+)?\// @editstr.gsub!($1, $2) when /^\.ww\s+(\d+)/, /^\.ww/ if $1 @editstr = word_wrap(@editstr, $1) else @editstr = word_wrap(@editstr, 76) end when /^\.ld\s+(\d+)/ idx = $1.to_i return if idx < 1 idx -= 1 lines = @editstr.split("\n") lines.delete_at(idx) @editstr = lines.join("\n") when /^\.li\s+(\d+)\s+(.*)?$/ idx = $1.to_i return if idx < 1 idx -= 1 nl = $2 lines = @editstr.split("\n") lines.insert(idx, nl + "\n") @editstr = lines.join("\n") when /^\.lr\s+(\d+)\s+(.*)?$/ idx = $1.to_i return if idx < 1 idx -= 1 nl = $2 lines = @editstr.split("\n") lines[idx] = nl + "\n" @editstr = lines.compact.join("\n") when /^@/ @mode = :playing if @editobj.object_id == world.msgs.object_id # detect sysmsgs @editobj.send("[]=", @editfield, @editstr) else @editobj.send(@editfield+"=", @editstr.chomp!) sendto("Ok.") end when /^\./ sendto "Invalid command." else @editstr << args << "\n" end end end