Chunk long line on review

This commit is contained in:
Nicolas VINOT 2011-11-05 14:47:49 +01:00
parent 940c6f44a3
commit b1f61e7e86
4 changed files with 59 additions and 13 deletions

View File

@ -0,0 +1,7 @@
#Sun Oct 09 17:20:12 CEST 2011
eclipse.preferences.version=1
encoding//src/main/java=UTF8
encoding//src/main/resources=UTF8
encoding//src/test/java=UTF8
encoding//src/test/resources=UTF8
encoding/<project>=UTF8

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.imirhil.april</groupId>
<artifactId>hebdobot</artifactId>

View File

@ -4,8 +4,6 @@ import java.util.Collection;
import java.util.LinkedList;
import org.jibble.pircbot.PircBot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.imirhil.april.hebdobot.review.CollectiveTopic;
import fr.imirhil.april.hebdobot.review.IndividualTopic;
@ -14,8 +12,6 @@ import fr.imirhil.april.hebdobot.review.Review;
import fr.imirhil.april.hebdobot.review.Topic;
public class Bot extends PircBot {
private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class);
private Review review = null;
private final String channel;
private final Collection<ReviewListener> listeners =

View File

@ -10,7 +10,7 @@ import org.apache.commons.lang.StringUtils;
import fr.imirhil.april.hebdobot.xml.UserAlias;
public class Review {
private static final int LENGTH = 74;
private static final int LENGTH = 80;
private final Set<String> participants = new HashSet<String>();
private final List<IndividualTopic> individualTopics =
@ -67,7 +67,8 @@ public class Review {
addBox(buffer, "Participants", '=');
buffer.append("\n");
for (final String participant : this.participants) {
buffer.append("* " + userService.getRealName(participant) + "\n");
chunkAndAppend(buffer, "* " + userService.getRealName(participant)
+ "\n");
}
buffer.append("\n");
@ -80,11 +81,12 @@ public class Review {
for (final IndividualTopic topic : this.individualTopics) {
if (topic.hasParticipant(participant)) {
buffer.append("\n");
buffer.append(getTopic(topic));
chunkAndAppend(buffer, getTopic(topic));
buffer.append("\n");
for (final Message message : topic
.getMessages(participant)) {
buffer.append("* " + message.getContent() + "\n");
chunkAndAppend(buffer, "* " + message.getContent()
+ "\n");
}
}
}
@ -99,7 +101,7 @@ public class Review {
buffer.append("\n");
addBox(buffer, topic.getTitle(), '-');
for (final Message message : topic.getMessages()) {
buffer.append("* " + message.getAuthor() + " : "
chunkAndAppend(buffer, "* " + message.getAuthor() + " : "
+ message.getContent() + "\n");
}
}
@ -110,8 +112,9 @@ public class Review {
addBox(buffer, "Log complet", '=');
buffer.append("\n");
for (final Message message : this.messages) {
buffer.append("* " + message.getAuthor() + " : "
+ message.getContent() + "\n");
chunkAndAppend(buffer,
"* " + message.getAuthor() + " : " + message.getContent()
+ "\n");
}
return buffer.toString();
@ -135,4 +138,43 @@ public class Review {
buffer.append(getLine(title, ' '));
buffer.append(getLine(c));
}
private static String chunk(final String content, final int length) {
final String[] words = content.split(" ");
final StringBuffer result = new StringBuffer();
StringBuffer current = new StringBuffer();
for (final String word : words) {
if (current.length() + word.length() > length) {
if (result.length() > 0) {
result.append("\n");
}
result.append(current);
current = new StringBuffer(word);
} else {
if (current.length() > 0) {
current.append(" ");
}
current.append(word);
}
}
if (current.length() > 0) {
if (result.length() > 0) {
result.append("\n");
}
result.append(current);
}
return result.toString();
}
private static String chunk(final String content) {
return chunk(content, LENGTH);
}
private static void chunkAndAppend(final StringBuffer buffer,
final String content) {
buffer.append(chunk(content));
}
}