incith Master

Joined: 23 Apr 2005 Posts: 275 Location: Canada
|
Posted: Sat Mar 08, 2008 5:09 am Post subject: Help me clean up my line_wrap/parse_output procs |
|
|
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! |
|