I would really like some help with one of my projects. I am a graphic design student and have little to no programming experience. I have created a program for a thermo mini printer that identifies tweets made on twitter based on specific hashtags used and prints them automatically.
However, it's based on a line length of 32 chars and will split words in half instead of moving the entire word to another line. A friend of mine suggested word wrapping but I can't find anything online to help me and most code I've found tends to be for c++ or c#.
The code so far can be found below:
// Build an ArrayList to hold all of the words that
// we get from the imported tweets
ArrayList<String> words = new ArrayList();
Twitter twitter;
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
//Set the size of the stage, and the background to black.
size(550,550);
background(0);
smooth();
//Make the twitter object and prepare the query
twitter = new TwitterFactory(cb.build()).getInstance();
}
void draw() {
Query query = new Query("#R.I.P");
query.setRpp(1);
//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
msg = msg.replace("\n"," ");
myPort.write(msg+"\n");
};
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
println("------------------------------------------------------");
delay(20000);
}
Since you have the length of the line, it's not that hard...
Iterate over the string, one character at a time. If you see a space save the position as e.g.
last_space
. If your iteration goes over the max line length, then go to thelast_space
position and convert the space to a newline, reset the position counter to zero, and start over from that position.Maybe implement it something like this:
Important note: The
wrap
function modifies the buffer passed in as thetext
argument. That means it can not handle literal strings as those are read-only. It works by replacing a space with a newline. If you modify the function to add more than one character, then a better solution would by to (re)allocate a new buffer big enough to hold the modified string.