001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.cli.util; 019 020import java.io.PrintStream; 021 022import com.google.common.base.Strings; 023 024public class CarriageReturnPrinter { 025 private PrintStream underlyingPrintStream; 026 private int lineLength; 027 private boolean lastWasWithCarriageReturn; 028 029 public CarriageReturnPrinter(PrintStream underlyingPrintStream) { 030 this.underlyingPrintStream = underlyingPrintStream; 031 this.lineLength = 0; 032 this.lastWasWithCarriageReturn = false; 033 } 034 035 public void printf(String format, Object... args) { 036 clearLastLine(); 037 038 String line = String.format(format, args); 039 040 underlyingPrintStream.print(line); 041 lineLength += line.length(); 042 } 043 044 public void print(String s) { 045 clearLastLine(); 046 047 underlyingPrintStream.print(s); 048 lineLength += s.length(); 049 } 050 051 public void println() { 052 clearLastLine(); 053 054 underlyingPrintStream.println(); 055 lineLength = 0; 056 } 057 058 public void println(String s) { 059 clearLastLine(); 060 061 underlyingPrintStream.println(s); 062 lineLength = 0; 063 } 064 065 public void printr() { 066 printr(""); 067 } 068 069 public void printr(Object s) { 070 clearLastLine(); 071 072 underlyingPrintStream.print(s); 073 074 lineLength = s.toString().length(); 075 lastWasWithCarriageReturn = true; 076 } 077 078 private void clearLastLine() { 079 if (lastWasWithCarriageReturn) { 080 String spacesStr = "\r" + Strings.repeat(" ", lineLength) + "\r"; 081 underlyingPrintStream.print(spacesStr); 082 083 lastWasWithCarriageReturn = false; 084 } 085 } 086}