10-24-2017, 07:55 +0200
AuthorPost
GM  09-04-2005, 19:01   | [EGG] Splitting long text into smaller parts for IRC output
(Moderator)

Avatar

Member since 09/2004
2 Posts
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
proc longtextout {text} {
    set text [split [string map {\n " "} $text]]
    set curpos 0
    for {set x 1} {$x <= [llength $text]} {incr x} {
        if {[string length [join [lrange $text $curpos $x]]] > 400} {
            lappend curout [join [lrange $text $curpos [expr $x-1]]]
            set curpos $x
        }
    }
    if {$curpos != [expr [llength $text] - 1]} {lappend curout [join [lrange $text $curpos end]]}
    return $curout
}


This proc will take a long text as argument, and will return a list of lines to be sent..... it can be used for example like this:
                                                                                                                              
1
foreach curline [longtextout $mytext] {putmsg $chan $curline}

Where mytext contains the long text.
This post was edited 1 times, last on 08-14-2009, 03:28 by thommey
Gotisch  02-18-2006, 15:13   ICQ
(Moderator)
Member since 01/2006
19 Posts
Location: Europe
This will do the same                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
proc safesay {target text} {
    #there are 510 characters maximum allowed for the command and its parameters.
    #the command is
    #:nick!ident@host PRIVMSG target :text
    set nick $::botnick
    set host [getchanhost $nick]
    set maxlength [expr {510 - [string length $nick] - [string length $host] - [string length $target] - 14}]
    #putlog $maxlength
    #so now comes the trick
    while {[string length $text] > 0} {
        #lets get as much text as fits in one line:
        set output [string range $text 0 $maxlength]
        #putlog $output
        set text [string replace $text 0 $maxlength]
        putchan $target $output
    }
}

This post was edited 1 times, last on 08-14-2009, 03:29 by thommey
thommey  03-05-2006, 13:36   | The same, just a bit more complicated (tries to output complete sentences and adds - if it is necessary to split a word) Jabber
(Administrator)
Member since 03/2004
62 Posts
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
proc safesay {dest text {queue putserv} {action PRIVMSG}} {
    set action [string toupper $action]
    set hardlimit 510
    set prefix ":$::botname PRIVMSG $dest :"
    incr hardlimit -[string length $prefix]
    set softlimit $hardlimit
    incr softlimit -100
    if {[lsearch -exact {putserv puthelp putquick} $queue] == -1} {
        error "bad queue \"$queue\": must be putserv, puthelp or putquick"
    }
    if {[lsearch -exact {PRIVMSG NOTICE} $action] == -1} {
        error "bad action \"$action\": must be PRIVMSG or NOTICE"
    }
    set temp [list]
    set ::text65 $text
    while {[string length [join $::text65]]} {
        $queue "$action $dest :[getoneline ::text65 $softlimit $hardlimit]"
    }
}
proc lowest {list} {
    foreach e $list {
        if {![info exists min]} { set min $e; continue }
        if {$e < $min && $e != -1} { set min $e }
    }
    return $min
}
proc getoneline {varname softlimit hardlimit} {
    set $varname [split [set $varname]]; set textnew [set $varname]
    set temp [list]; set tempnew [list]
    # temp contains the text to send
    if {[string length [lindex [set $varname] 0]] >= $hardlimit} {
        set temp "[string range [set $varname] 0 [expr $hardlimit - 2]]-"
        set $varname [string range [set $varname] [expr $hardlimit - 1] end]
        return $temp
    }
    while {[string length [join $tempnew]] < $softlimit && [string length [set $varname]]} {
        # as long as we're shorter than $softlimit, append
        set $varname $textnew
        set temp $tempnew
        # $tempnew and $textnew are set in the next iteration. they're set delayed 1 cycle to catch the text _before_ we exceed the limit
        # so we clearly stay lower then the limit here
        lappend tempnew [lindex [set $varname] 0]
        set textnew [lrange [set $varname] 1 end]
    }
    # we're longer than $softlimit and are trying to finish the sentence by finding punctuations
    set s1pos [lsearch -glob [set $varname] *.]
    set s2pos [lsearch -glob [set $varname] *\\?]
    set s3pos [lsearch -glob [set $varname] *,]
    set s4pos [lsearch -glob [set $varname] *!]
    set lowest [lowest [list $s1pos $s2pos $s3pos $s4pos]]
    set tempnew $temp; set textnew [set $varname]
    if {$lowest != -1} {
        while {$lowest > -1 && [string length [set $varname]]} {
            set tempnew [concat $temp [lrange [set $varname] 0 $lowest]]; incr lowest
            set textnew [lrange [set $varname] $lowest end]
            set s1pos [lsearch -glob $textnew *.]
            set s2pos [lsearch -glob $textnew *\\?]
            set s3pos [lsearch -glob $textnew *,]
            set s4pos [lsearch -glob $textnew *!]
            set lowest [lowest [list $s1pos $s2pos $s3pos $s4pos]]
            if {[string length [join $tempnew]] < $hardlimit} {
                set temp $tempnew
                set $varname $textnew
            } else { break }
        }
        return [join $temp]
    }
    # no punctuations :(
    set textnew [set $varname]; set tempnew $temp
    while {[string length [join $tempnew]] < $hardlimit  && [string length [set $varname]]} {
        # shorter than $softlimit, append words
        set $varname $textnew
        set temp $tempnew
        # $tempnew and $textnew set delayed as above
        lappend tempnew [lindex [set $varname] 0]
        set textnew [lrange [set $varname] 1 end]
    }
    return [join $temp]
}

This post was edited 4 times, last on 09-12-2010, 21:34 by thommey
schlenk  07-06-2006, 16:56  
Member since 07/2006
3 Posts
The textutil package in Tcllib has some helpers for this kind of things:                                                                                                                               
1
2
3
4
5
6
7
package require textutil

# split a block of text to lines, left adjusted
proc block2lines {text len} {
    split [textutil::adjust $text -full 1 -length $len -strictlength 1] \n
}



Using options you can also add hyphenation and other features.
See:
http://tcllib.sourceforge.net/doc/textutil.html

Michael
This post was edited 1 times, last on 08-14-2009, 03:31 by thommey
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen