This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Help me clean up my line_wrap/parse_output procs

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Help me clean up my line_wrap/parse_output procs

Post by incith »

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: Select all

    # [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: Select all

    # [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}"
        }
      }
    }
Post Reply