Skip to content

Commit

Permalink
refactor: shorten & trim cli utils
Browse files Browse the repository at this point in the history
This extracts and refines utility functions for xucli to shorten or trim
values that are too long to display in a terminal.
  • Loading branch information
sangaman committed May 27, 2020
1 parent 1c2e66a commit 4f881cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 3 additions & 10 deletions lib/cli/commands/listpeers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'colors/safe';
import { Arguments } from 'yargs';
import { ListPeersRequest, ListPeersResponse, Peer } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';
import { generateHeaders } from '../utils';
import { generateHeaders, shorten } from '../utils';

const HEADERS = [
'Peer',
Expand All @@ -18,13 +18,6 @@ const createTable = () => {
return table;
};

const trimPubKey = (key: string) => {
if (key.length <= 0) {
return '';
}
return `${key.slice(0, 10)}...${key.slice(key.length - 10)}`;
};

const formatPairList = (pairs: string[]) => {
let pairString = '';
pairs.forEach((pair) => {
Expand All @@ -38,7 +31,7 @@ const formatLndPubKeys = (lndKeys: string[][]) => {
let str = '';
lndKeys.forEach((client) => {
/* eslint disable-next-line */
str = `${str}${str ? '\n' : ''}${client[0]} lnd key: ${trimPubKey(client[1])}`;
str = `${str}${str ? '\n' : ''}${client[0]} lnd key: ${shorten(client[1])}`;
});
return str;
};
Expand All @@ -60,7 +53,7 @@ ${address}`,
\nversion: ${peer.xudVersion}\
\ntime connected: ${peer.secondsConnected.toString()} seconds\
\n${formatLndPubKeys(peer.lndPubKeysMap)}\
${peer.raidenAddress ? `\nraiden address: ${trimPubKey(peer.raidenAddress)}` : ''}`,
${peer.raidenAddress ? `\nraiden address: ${shorten(peer.raidenAddress)}` : ''}`,
];
formattedPeers.push(details);
});
Expand Down
14 changes: 14 additions & 0 deletions lib/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ export function getDefaultCertPath() {
}
}

export function shorten(key: string, length = 10) {
if (key.length <= (length * 2) + 3) {
return key;
}
return `${key.slice(0, length)}...${key.slice(key.length - length)}`;
}

export function trim(key: string, length = 10) {
if (key.length <= length + 3) {
return '';
}
return `${key.slice(0, length)}...`;
}

export const generateHeaders = (headers: string[]) => {
return headers.map((header) => {
return colors.blue(header);
Expand Down

0 comments on commit 4f881cb

Please sign in to comment.