Skip to content

Commit

Permalink
- adds default limit for table output to 50
Browse files Browse the repository at this point in the history
- prints message at end of table if limit is reached
- updates commands to handle limit
  • Loading branch information
jowparks committed Nov 1, 2024
1 parent 29ed1d0 commit 65b52df
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 147 deletions.
15 changes: 6 additions & 9 deletions ironfish-cli/src/commands/mempool/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ export class TransactionsCommand extends IronfishCommand {

const transactions: GetMempoolTransactionResponse[] = []
for await (const transaction of response.contentStream()) {
if (transactions.length >= flags.limit) {
break
}
transactions.push(transaction)
}

Expand Down Expand Up @@ -208,22 +211,16 @@ function renderTable(

let result = ''

const limit = flags.csv ? 0 : flags.show
table(getRows(response, limit), columns, {
table(getRows(response), columns, {
printLine: (line) => (result += `${String(line)}\n`),
...flags,
})

if (limit > 0 && response.length > limit) {
result += ` ... ${response.length - limit} more rows\n`
}

return result
}

function getRows(response: GetMempoolTransactionResponse[], limit: number): TransactionRow[] {
const transactions = limit > 0 ? response.slice(0, limit) : response
return transactions.map(({ serializedTransaction, position, expiresIn }) => {
function getRows(response: GetMempoolTransactionResponse[]): TransactionRow[] {
return response.map(({ serializedTransaction, position, expiresIn }) => {
const transaction = new Transaction(Buffer.from(serializedTransaction, 'hex'))

return {
Expand Down
7 changes: 4 additions & 3 deletions ironfish-cli/src/commands/peers/banned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class BannedCommand extends IronfishCommand {
if (!flags.follow) {
await this.sdk.client.connect()
const response = await this.sdk.client.peer.getBannedPeers()
this.log(renderTable(response.content))
this.log(renderTable(response.content, flags.limit))
this.exit(0)
}

Expand All @@ -59,14 +59,14 @@ export class BannedCommand extends IronfishCommand {

for await (const value of response.contentStream()) {
text.clearBaseLine(0)
text.setContent(renderTable(value))
text.setContent(renderTable(value, flags.limit))
screen.render()
}
}
}
}

function renderTable(content: GetBannedPeersResponse): string {
function renderTable(content: GetBannedPeersResponse, limit: number): string {
const columns: TableColumns<BannedPeerResponse> = {
identity: {
minWidth: 45,
Expand All @@ -87,6 +87,7 @@ function renderTable(content: GetBannedPeersResponse): string {
let result = ''

table(content.peers, columns, {
limit,
printLine: (line) => (result += `${String(line)}\n`),
})

Expand Down
120 changes: 60 additions & 60 deletions ironfish-cli/src/commands/wallet/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,68 +52,68 @@ export class AssetsCommand extends IronfishCommand {
const assetNameWidth = flags.extended
? MAX_ASSET_NAME_COLUMN_WIDTH
: MIN_ASSET_NAME_COLUMN_WIDTH
let showHeader = !flags['no-header']

const assets = []
for await (const asset of response.contentStream()) {
table(
[asset],
{
name: TableCols.fixedWidth({
header: 'Name',
width: assetNameWidth,
get: (row) =>
renderAssetWithVerificationStatus(
BufferUtils.toHuman(Buffer.from(row.name, 'hex')),
{
verification: row.verification,
outputType: flags.output,
},
),
}),
id: {
header: 'ID',
minWidth: ASSET_ID_LENGTH + 1,
get: (row) => row.id,
},
metadata: TableCols.fixedWidth({
header: 'Metadata',
width: assetMetadataWidth,
get: (row) => BufferUtils.toHuman(Buffer.from(row.metadata, 'hex')),
}),
createdTransactionHash: {
header: 'Created Transaction Hash',
get: (row) => row.createdTransactionHash,
},
supply: {
header: 'Supply',
minWidth: 16,
get: (row) => row.supply ?? 'NULL',
},
creator: {
header: 'Creator',
minWidth: PUBLIC_ADDRESS_LENGTH + 1,
get: (row) =>
row.id === Asset.nativeId().toString('hex')
? BufferUtils.toHuman(Buffer.from(row.creator, 'hex'))
: row.creator,
},
owner: {
header: 'Owner',
minWidth: PUBLIC_ADDRESS_LENGTH + 1,
get: (row) =>
row.id === Asset.nativeId().toString('hex')
? BufferUtils.toHuman(Buffer.from(row.owner, 'hex'))
: row.owner,
},
assets.push(asset)
if (assets.length >= flags.limit) {
break
}
}
table(
assets,
{
name: TableCols.fixedWidth({
header: 'Name',
width: assetNameWidth,
get: (row) =>
renderAssetWithVerificationStatus(
BufferUtils.toHuman(Buffer.from(row.name, 'hex')),
{
verification: row.verification,
outputType: flags.output,
},
),
}),
id: {
header: 'ID',
minWidth: ASSET_ID_LENGTH + 1,
get: (row) => row.id,
},
{
printLine: this.log.bind(this),
...flags,
'no-header': !showHeader,
metadata: TableCols.fixedWidth({
header: 'Metadata',
width: assetMetadataWidth,
get: (row) => BufferUtils.toHuman(Buffer.from(row.metadata, 'hex')),
}),
createdTransactionHash: {
header: 'Created Transaction Hash',
get: (row) => row.createdTransactionHash,
},
)

showHeader = false
}
supply: {
header: 'Supply',
minWidth: 16,
get: (row) => row.supply ?? 'NULL',
},
creator: {
header: 'Creator',
minWidth: PUBLIC_ADDRESS_LENGTH + 1,
get: (row) =>
row.id === Asset.nativeId().toString('hex')
? BufferUtils.toHuman(Buffer.from(row.creator, 'hex'))
: row.creator,
},
owner: {
header: 'Owner',
minWidth: PUBLIC_ADDRESS_LENGTH + 1,
get: (row) =>
row.id === Asset.nativeId().toString('hex')
? BufferUtils.toHuman(Buffer.from(row.owner, 'hex'))
: row.owner,
},
},
{
printLine: this.log.bind(this),
...flags,
},
)
}
}
120 changes: 60 additions & 60 deletions ironfish-cli/src/commands/wallet/notes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { CurrencyUtils, RpcAsset } from '@ironfish/sdk'
import { CurrencyUtils, RpcAsset, RpcWalletNote } from '@ironfish/sdk'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../../command'
import { RemoteFlags } from '../../../flags'
Expand Down Expand Up @@ -33,74 +33,74 @@ export class NotesCommand extends IronfishCommand {
const account = await useAccount(client, flags.account)

const response = client.wallet.getAccountNotesStream({ account })

let showHeader = !flags['no-header']

const notes: RpcWalletNote[] = []
for await (const note of response.contentStream()) {
if (notes.length >= flags.limit) {
break
}
if (!assetLookup.has(note.assetId)) {
assetLookup.set(
note.assetId,
(await client.wallet.getAsset({ id: note.assetId, account })).content,
)
}

ui.table(
[note],
{
memo: {
header: 'Memo',
// Maximum memo length is 32 bytes
minWidth: 33,
get: (row) => row.memo,
},
sender: {
header: 'Sender',
get: (row) => row.sender,
},
transactionHash: {
header: 'From Transaction',
get: (row) => row.transactionHash,
},
isSpent: {
header: 'Spent',
get: (row) => {
if (row.spent === undefined) {
return '-'
} else {
return row.spent ? `✔` : `x`
}
},
},
...TableCols.asset({ extended: flags.extended }),
value: {
header: 'Amount',
get: (row) =>
CurrencyUtils.render(
row.value,
false,
row.assetId,
assetLookup.get(row.assetId)?.verification,
),
minWidth: 16,
},
noteHash: {
header: 'Note Hash',
get: (row) => row.noteHash,
notes.push(note)
}
ui.table(
notes,
{
memo: {
header: 'Memo',
// Maximum memo length is 32 bytes
minWidth: 33,
get: (row) => row.memo,
},
sender: {
header: 'Sender',
get: (row) => row.sender,
},
transactionHash: {
header: 'From Transaction',
get: (row) => row.transactionHash,
},
isSpent: {
header: 'Spent',
get: (row) => {
if (row.spent === undefined) {
return '-'
} else {
return row.spent ? `✔` : `x`
}
},
nullifier: {
header: 'Nullifier',
get: (row) => {
if (row.nullifier === null) {
return '-'
} else {
return row.nullifier
}
},
},
...TableCols.asset({ extended: flags.extended }),
value: {
header: 'Amount',
get: (row) =>
CurrencyUtils.render(
row.value,
false,
row.assetId,
assetLookup.get(row.assetId)?.verification,
),
minWidth: 16,
},
noteHash: {
header: 'Note Hash',
get: (row) => row.noteHash,
},
nullifier: {
header: 'Nullifier',
get: (row) => {
if (row.nullifier === null) {
return '-'
} else {
return row.nullifier
}
},
},
{ ...flags, 'no-header': !showHeader },
)
showHeader = false
}
},
flags,
)
}
}
22 changes: 9 additions & 13 deletions ironfish-cli/src/commands/wallet/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ export class TransactionsCommand extends IronfishCommand {
char: 's',
description: 'Block sequence to get transactions for',
}),
limit: Flags.integer({
description: 'Number of latest transactions to get details for',
}),
offset: Flags.integer({
description: 'Number of latest transactions to skip',
}),
Expand Down Expand Up @@ -82,11 +79,13 @@ export class TransactionsCommand extends IronfishCommand {

const columns = this.getColumns(flags.extended, flags.notes, format)

let showHeader = !flags['no-header']
let hasTransactions = false

let transactionRows: PartialRecursive<TransactionRow>[] = []
for await (const transaction of response.contentStream()) {
let transactionRows: PartialRecursive<TransactionRow>[]
if (transactionRows.length >= flags.limit) {
break
}
if (flags.notes) {
Assert.isNotUndefined(transaction.notes)
const assetLookup = await getAssetsByIDs(
Expand All @@ -113,17 +112,14 @@ export class TransactionsCommand extends IronfishCommand {
)
transactionRows = this.getTransactionRows(assetLookup, transaction, format)
}

ui.table(transactionRows, columns, {
printLine: this.log.bind(this),
...flags,
'no-header': !showHeader,
})

showHeader = false
hasTransactions = true
}

ui.table(transactionRows, columns, {
printLine: this.log.bind(this),
...flags,
})

if (!hasTransactions) {
this.log('No transactions found')
}
Expand Down
Loading

0 comments on commit 65b52df

Please sign in to comment.