| View previous topic :: View next topic |
| Author |
Message |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Sun Jan 20, 2019 11:36 am Post subject: Spliting text in two lines |
|
|
Evening, can anyone help me with a code that will split the text in two lines (getting all the text in does 2 lines) if its bigger than a max length ? Thanks _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sun Jan 20, 2019 2:36 pm Post subject: |
|
|
In PHP there's a function called wordwrap that dose this nicely. Here's how I got about the same results in TCL.
| Code: |
proc wordwrap {text max} {
set len [string length $text]
while {$len} {
if {$len > $max} {
set cut [string range $text 0 [expr $max - 1]]
set output [string range $cut 0 [string last " " $cut]]
set text [string range $text [string length $output] end]
incr len -$max
} else {
set output [string range $text 0 $len]
set len 0
}
puts "output: $output"
}
}
|
results:
| Code: |
% set text "Evening, can anyone help me with a code that will split the text in two lines (getting all the text in does 2 lines) if its bigger than a max length ? Thanks"
Evening, can anyone help me with a code that will split the text in two lines (getting all the text in does 2 lines) if its bigger than a max length ? Thanks
% wordwrap $text 80
output: Evening, can anyone help me with a code that will split the text in two lines
output: (getting all the text in does 2 lines) if its bigger than a max length ? Thank
% wordwrap $text 50
output: Evening, can anyone help me with a code that will
output: split the text in two lines (getting all the text
output: in does 2 lines) if its bigger than a max length
output: ? Thanks
% wordwrap $text 100
output: Evening, can anyone help me with a code that will split the text in two lines (getting all the text
output: in does 2 lines) if its bigger than a max length ? Thanks
|
Notice that it doesn't break words so it looks nicely.
PS: The code at the end of the Character hiding and showing topic isn't working or completly forgot about that topic?
PS #2: On Rosetta Code there's wrapParagraph with another approach on this. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Mon Jan 21, 2019 1:10 pm Post subject: |
|
|
| Code: | proc Trivia247:wraptext {chan text characters} {
set len [string length $text]
putlog $len
while {$len} {
if {$len > $characters} {
set cut [string range $text 0 [expr $characters - 1]]
set output [string range $cut 0 [string last " " $cut]]
set text [string range $text [string length $output] end]
incr len -$characters
} else {
set output [string range $text 0 $len]
set len 0
}
putlog "$output"
putlog "[string length $output]"
putserv "PRIVMSG $chan :$output"
}
} |
DCC CHAT:
| Quote: | [18:09:03] 1499
[18:09:03] BONUS +14:
[18:09:03] 44
[18:09:03] 0
[18:09:03] 0
[18:09:03] InmcemanmamavutmlocmprimamparticiparemamRomanieimlamunmturneumfinalmalmCampionatuluimMondialInmc
[18:09:03] 300
|
Channel:
| Quote: | <Androo> BONUS +14:
<Androo> InmcemanmamavutmlocmprimamparticiparemamRomanieimlamunmturneumfinalmalmCampionatuluimMondialInmc
<Androo> 1st Hint: *** +12 Bonus
<Androo> 2nd Hint: d** +10 Bonus
<Androo> 3rd Hint: *aa +8 Bonus |
Don`t why... from your example it should work. _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Tue Jan 22, 2019 1:34 am Post subject: |
|
|
That script tries to split the text in a correct place, at the nearest space.
The string you try is long, but has no spaces.
That is not a valid string, there is no single "word" that long:) _________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
| Back to top |
|
 |
heartbroken Op

Joined: 23 Jun 2011 Posts: 106 Location: somewhere out there
|
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Wed Jan 23, 2019 2:42 pm Post subject: |
|
|
It looks that way because of the ANTI HACK part. _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Wed Jan 23, 2019 3:05 pm Post subject: |
|
|
heartbroken: Works only if i stop the antihack part. So when the questions is "word word word" works, but when its in a single word it brocks.
Anyone with an idea on how to make anything split using the antihack option?
Antihack = it adds a random letter each question between the spaces of the words, so if you try to copy - paste the question it would be "impossible" to read. _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Thu Jan 24, 2019 3:40 am Post subject: |
|
|
Basically the line you are mentioned is most likely from a trivia bot and is "obfuscated" by adding a character between words and a special color so that regular legit users can see the question without the extra colored characters, but anyone else why tries to be cheeky or copy/paste the question would have a hard time "decoding" it back to a normal question.
If you own the bot (based on your claim that you can start/stop the "obfuscation" process) that is spitting the trivia questions why do you need this? What exactly do you want to achieve here? Want to cheat at your own game?
Since the "special" character used to "obfuscate" the code (in the given example is "m") is also found in the other words used in the question reversing the process is kind off an impossible task, unless you know exactly at what positions it was added. For example an extra 'm' is added at 2, 5, 8, and so on. So if you know the pattern then is easy to replace the special character with a space and then recreate the question into a "word word word" that with the code I mentioned would get the result you wanted. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Thu Jan 24, 2019 8:59 am Post subject: |
|
|
Yes its from a trivia script i wrote from scratch.
I was thinking of something to add the letters after the split, but never got it working. _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sun Jan 27, 2019 1:54 pm Post subject: |
|
|
As i was looking on the forums for something I stumbled onto a very old discussion (this) where user and strikelight (two forum users, long time idle sadly) debated over a pice of code user made to wrap some text output. Here is the final code with the changes they discussed:
| Code: |
proc wordwrap {str {len 70} {splitChr { }}} {
set out [set cur {}]; set i 0
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]
}
|
_________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
|