egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help me clean up my line_wrap/parse_output procs

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
incith
Master


Joined: 23 Apr 2005
Posts: 275
Location: Canada

PostPosted: Sat Mar 08, 2008 5:09 am    Post subject: Help me clean up my line_wrap/parse_output procs Reply with quote

Well, I'd like to get this into 1 single proc hopefully. I use these for splitting long lines, and detecting what the seperator is, if it's \n then we will want to split on \n and push out 1 line per \n.

If the seperator is not \n, then we want to just split on the $split_length.

line_wrap itself works great, but I was never able/could be bothered to merge parse_output into it, and it became a little messy. Here are the procs:
Code:
    # [parse_output] : prepares output for sending to a channel/user, calls line_wrap
    #
    proc parse_output {input} {
      set parsed_output [set parsed_current {}]
      if {[string match "\n" $incith::layout::seperator] == 1} {
        regsub {\n\s*$} $input "" input
        foreach newline [split $input "\n"] {
          foreach line [incith::layout::line_wrap $newline] {
            lappend parsed_output $line
          }
        }
      } else {
        regsub "(?:${incith::layout::seperator}|\\|)\\s*$" $input {} input
        foreach line [incith::layout::line_wrap $input] {
          lappend parsed_output $line
        }
      }
      return $parsed_output
    }

    # [line_wrap] : takes a long line in, and chops it before the specified length
    # http://forum.egghelp.org/viewtopic.php?t=6690
    #
    proc line_wrap {str {splitChr { }}} {
      set out [set cur {}]
      set i 0
      set len $incith::layout::split_length
      foreach word [split [set str][set str ""] $splitChr] {
        if {[incr i [string len $word]] > $len} {
          lappend out [join $cur $splitChr]
          set cur [list $word]
          set i [string len $word]
        } else {
          lappend cur $word
        }
        incr i
      }
      lappend out [join $cur $splitChr]
    }


And then I'd call it with this:
Code:
    # [send_output] : sends $data appropriately out to $where
    #
    proc send_output {where data} {
      if {${incith::layout::notices} >= 1 && ![string match {#*} $where]} {
        foreach line [incith::layout::parse_output $data] {
          putquick "NOTICE $where :${line}"
        }
      } else {
        foreach line [incith::layout::parse_output $data] {
          putquick "PRIVMSG $where :${line}"
        }
      }
    }

_________________
; Answer a few unanswered posts!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber