-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4703 from ghubstan/6-format-trade
Refactor CLI output formatting code & add trade formatter
- Loading branch information
Showing
6 changed files
with
189 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.cli; | ||
|
||
import static com.google.common.base.Strings.padEnd; | ||
import static com.google.common.base.Strings.padStart; | ||
|
||
class ColumnHeaderConstants { | ||
|
||
// For inserting 2 spaces between column headers. | ||
static final String COL_HEADER_DELIMITER = " "; | ||
|
||
// Table column header format specs, right padded with two spaces. In some cases | ||
// such as COL_HEADER_CREATION_DATE, COL_HEADER_VOLUME and COL_HEADER_UUID, the | ||
// expected max data string length is accounted for. In others, the column header length | ||
// are expected to be greater than any column value length. | ||
static final String COL_HEADER_ADDRESS = padEnd("Address", 34, ' '); | ||
static final String COL_HEADER_AMOUNT = padEnd("BTC(min - max)", 24, ' '); | ||
static final String COL_HEADER_BALANCE = padStart("Balance", 12, ' '); | ||
static final String COL_HEADER_CONFIRMATIONS = "Confirmations"; | ||
static final String COL_HEADER_CREATION_DATE = padEnd("Creation Date (UTC)", 20, ' '); | ||
static final String COL_HEADER_CURRENCY = "Currency"; | ||
static final String COL_HEADER_DIRECTION = "Buy/Sell"; | ||
static final String COL_HEADER_NAME = "Name"; | ||
static final String COL_HEADER_PAYMENT_METHOD = "Payment Method"; | ||
static final String COL_HEADER_PRICE = "Price in %-3s for 1 BTC"; | ||
static final String COL_HEADER_TRADE_AMOUNT = padStart("Amount(%-3s)", 12, ' '); | ||
static final String COL_HEADER_TRADE_DEPOSIT_CONFIRMED = "Deposit Confirmed"; | ||
static final String COL_HEADER_TRADE_DEPOSIT_PUBLISHED = "Deposit Published"; | ||
static final String COL_HEADER_TRADE_FIAT_SENT = "Fiat Sent"; | ||
static final String COL_HEADER_TRADE_FIAT_RECEIVED = "Fiat Received"; | ||
static final String COL_HEADER_TRADE_PAYOUT_PUBLISHED = "Payout Published"; | ||
static final String COL_HEADER_TRADE_WITHDRAWN = "Withdrawn"; | ||
static final String COL_HEADER_TRADE_ROLE = "My Role"; | ||
static final String COL_HEADER_TRADE_SHORT_ID = "ID"; | ||
static final String COL_HEADER_TRADE_TX_FEE = "Tx Fee(%-3s)"; | ||
static final String COL_HEADER_TRADE_TAKER_FEE = "Taker Fee(%-3s)"; | ||
static final String COL_HEADER_VOLUME = padEnd("%-3s(min - max)", 15, ' '); | ||
static final String COL_HEADER_UUID = padEnd("ID", 52, ' '); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.cli; | ||
|
||
import bisq.proto.grpc.TradeInfo; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static bisq.cli.ColumnHeaderConstants.*; | ||
import static bisq.cli.CurrencyFormat.formatOfferPrice; | ||
import static bisq.cli.CurrencyFormat.formatSatoshis; | ||
import static com.google.common.base.Strings.padEnd; | ||
|
||
@VisibleForTesting | ||
public class TradeFormat { | ||
|
||
@VisibleForTesting | ||
public static String format(TradeInfo tradeInfo) { | ||
// Some column values might be longer than header, so we need to calculated them. | ||
int shortIdColWidth = Math.max(COL_HEADER_TRADE_SHORT_ID.length(), tradeInfo.getShortId().length()); | ||
int roleColWidth = Math.max(COL_HEADER_TRADE_ROLE.length(), tradeInfo.getRole().length()); | ||
|
||
// We only show taker fee under its header when user is the taker. | ||
boolean isTaker = tradeInfo.getRole().toLowerCase().contains("taker"); | ||
Supplier<String> takerFeeHeaderFormat = () -> isTaker ? | ||
padEnd(COL_HEADER_TRADE_TAKER_FEE, 12, ' ') + COL_HEADER_DELIMITER | ||
: ""; | ||
Supplier<String> takerFeeHeader = () -> isTaker ? | ||
"%" + (COL_HEADER_TRADE_TAKER_FEE.length() + 1) + "s" | ||
: ""; | ||
|
||
String headersFormat = padEnd(COL_HEADER_TRADE_SHORT_ID, shortIdColWidth, ' ') + COL_HEADER_DELIMITER | ||
+ padEnd(COL_HEADER_TRADE_ROLE, roleColWidth, ' ') + COL_HEADER_DELIMITER | ||
+ COL_HEADER_PRICE + COL_HEADER_DELIMITER // includes %s -> currencyCode | ||
+ padEnd(COL_HEADER_TRADE_AMOUNT, 12, ' ') + COL_HEADER_DELIMITER | ||
+ padEnd(COL_HEADER_TRADE_TX_FEE, 12, ' ') + COL_HEADER_DELIMITER | ||
+ takerFeeHeaderFormat.get() | ||
+ COL_HEADER_TRADE_DEPOSIT_PUBLISHED + COL_HEADER_DELIMITER | ||
+ COL_HEADER_TRADE_DEPOSIT_CONFIRMED + COL_HEADER_DELIMITER | ||
+ COL_HEADER_TRADE_FIAT_SENT + COL_HEADER_DELIMITER | ||
+ COL_HEADER_TRADE_FIAT_RECEIVED + COL_HEADER_DELIMITER | ||
+ COL_HEADER_TRADE_PAYOUT_PUBLISHED + COL_HEADER_DELIMITER | ||
+ COL_HEADER_TRADE_WITHDRAWN + COL_HEADER_DELIMITER | ||
+ "%n"; | ||
|
||
String counterCurrencyCode = tradeInfo.getOffer().getCounterCurrencyCode(); | ||
String baseCurrencyCode = tradeInfo.getOffer().getBaseCurrencyCode(); | ||
String headerLine = isTaker | ||
? String.format(headersFormat, counterCurrencyCode, baseCurrencyCode, baseCurrencyCode, baseCurrencyCode) | ||
: String.format(headersFormat, counterCurrencyCode, baseCurrencyCode, baseCurrencyCode); | ||
|
||
String colDataFormat = "%-" + shortIdColWidth + "s" // left justify | ||
+ " %-" + (roleColWidth + COL_HEADER_DELIMITER.length()) + "s" // left justify | ||
+ "%" + (COL_HEADER_PRICE.length() - 1) + "s" // right justify | ||
+ "%" + (COL_HEADER_TRADE_AMOUNT.length() + 1) + "s" // right justify | ||
+ "%" + (COL_HEADER_TRADE_TX_FEE.length() + 1) + "s" // right justify | ||
+ takerFeeHeader.get() // right justify | ||
+ " %-" + COL_HEADER_TRADE_DEPOSIT_PUBLISHED.length() + "s" // left justify | ||
+ " %-" + COL_HEADER_TRADE_DEPOSIT_CONFIRMED.length() + "s" // left justify | ||
+ " %-" + COL_HEADER_TRADE_FIAT_SENT.length() + "s" // left justify | ||
+ " %-" + COL_HEADER_TRADE_FIAT_RECEIVED.length() + "s" // left justify | ||
+ " %-" + COL_HEADER_TRADE_PAYOUT_PUBLISHED.length() + "s" // left justify | ||
+ " %-" + COL_HEADER_TRADE_WITHDRAWN.length() + "s"; // left justify | ||
|
||
return headerLine + | ||
(isTaker | ||
? formatTradeForTaker(colDataFormat, tradeInfo) | ||
: formatTradeForMaker(colDataFormat, tradeInfo)); | ||
} | ||
|
||
private static String formatTradeForMaker(String format, TradeInfo tradeInfo) { | ||
return String.format(format, | ||
tradeInfo.getShortId(), | ||
tradeInfo.getRole(), | ||
formatOfferPrice(tradeInfo.getTradePrice()), | ||
formatSatoshis(tradeInfo.getTradeAmountAsLong()), | ||
formatSatoshis(tradeInfo.getTxFeeAsLong()), | ||
tradeInfo.getIsDepositPublished() ? "YES" : "NO", | ||
tradeInfo.getIsDepositConfirmed() ? "YES" : "NO", | ||
tradeInfo.getIsFiatSent() ? "YES" : "NO", | ||
tradeInfo.getIsFiatReceived() ? "YES" : "NO", | ||
tradeInfo.getIsPayoutPublished() ? "YES" : "NO", | ||
tradeInfo.getIsWithdrawn() ? "YES" : "NO"); | ||
} | ||
|
||
private static String formatTradeForTaker(String format, TradeInfo tradeInfo) { | ||
return String.format(format, | ||
tradeInfo.getShortId(), | ||
tradeInfo.getRole(), | ||
formatOfferPrice(tradeInfo.getTradePrice()), | ||
formatSatoshis(tradeInfo.getTradeAmountAsLong()), | ||
formatSatoshis(tradeInfo.getTxFeeAsLong()), | ||
formatSatoshis(tradeInfo.getTakerFeeAsLong()), | ||
tradeInfo.getIsDepositPublished() ? "YES" : "NO", | ||
tradeInfo.getIsDepositConfirmed() ? "YES" : "NO", | ||
tradeInfo.getIsFiatSent() ? "YES" : "NO", | ||
tradeInfo.getIsFiatReceived() ? "YES" : "NO", | ||
tradeInfo.getIsPayoutPublished() ? "YES" : "NO", | ||
tradeInfo.getIsWithdrawn() ? "YES" : "NO"); | ||
} | ||
} |