mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 02:50:39 +00:00
Refactor ReceiveMessageHandler
Introduce PlainTextWriter to improve indentation handling.
This commit is contained in:
parent
6c33a89f82
commit
03c30519b1
6 changed files with 641 additions and 507 deletions
67
src/main/java/org/asamk/signal/PlainTextWriterImpl.java
Normal file
67
src/main/java/org/asamk/signal/PlainTextWriterImpl.java
Normal file
|
@ -0,0 +1,67 @@
|
|||
package org.asamk.signal;
|
||||
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public final class PlainTextWriterImpl implements PlainTextWriter {
|
||||
|
||||
private final Writer writer;
|
||||
|
||||
private PlainTextWriter indentedWriter;
|
||||
|
||||
public PlainTextWriterImpl(final OutputStream outputStream) {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(String format, Object... args) throws IOException {
|
||||
final String message = MessageFormatter.arrayFormat(format, args).getMessage();
|
||||
|
||||
writer.write(message);
|
||||
writer.write(System.lineSeparator());
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlainTextWriter indentedWriter() {
|
||||
if (indentedWriter == null) {
|
||||
indentedWriter = new IndentedPlainTextWriter(this, writer);
|
||||
}
|
||||
return indentedWriter;
|
||||
}
|
||||
|
||||
private static final class IndentedPlainTextWriter implements PlainTextWriter {
|
||||
|
||||
private final static int INDENTATION = 2;
|
||||
|
||||
private final String spaces = " ".repeat(INDENTATION);
|
||||
private final PlainTextWriter plainTextWriter;
|
||||
private final Writer writer;
|
||||
|
||||
private PlainTextWriter indentedWriter;
|
||||
|
||||
private IndentedPlainTextWriter(final PlainTextWriter plainTextWriter, final Writer writer) {
|
||||
this.plainTextWriter = plainTextWriter;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(final String format, final Object... args) throws IOException {
|
||||
writer.write(spaces);
|
||||
plainTextWriter.println(format, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlainTextWriter indentedWriter() {
|
||||
if (indentedWriter == null) {
|
||||
indentedWriter = new IndentedPlainTextWriter(this, writer);
|
||||
}
|
||||
return indentedWriter;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue