display

package
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Overview

Package display provides interfaces and functions to format the command line output and print.

Example (RespTxQuery_WithRaw_json)
Print(&RespTxQuery{Msg: getExampleTxQueryResponse(), WithRaw: true}, nil, "json")
Output:

{
  "result": {
    "hash": "31303234",
    "height": 10,
    "tx": {
      "signature": {
        "sig": "yz/tf2/zblkFTASoMbIV5RQFJ1PuNT5v4x1LTvc2rNYVUSfbVV0wBroU/LTHm7rVbI5juBqYljGbsFOp4lNHWAA=",
        "type": "secp256k1_ep"
      },
      "body": {
        "desc": "This is a test transaction for cli",
        "payload": "AAH4Vrg5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyzs3MxoR0ZXh0gMSDZm9v",
        "type": "execute",
        "fee": "100",
        "nonce": 10,
        "chain_id": "asdf"
      },
      "serialization": "concat",
      "sender": ""
    },
    "tx_result": {
      "code": 0,
      "log": "This is log",
      "gas_used": 10,
      "gas_wanted": 10
    },
    "raw": "0001f8eaf850b841cb3fed7f6ff36e59054c04a831b215e514052753ee353e6fe31d4b4ef736acd6155127db555d3006ba14fcb4c79bbad56c8e63b81a9896319bb053a9e2534758008c736563703235366b315f6570f88ea25468697320697320612074657374207472616e73616374696f6e20666f7220636c69b85a0001f856b8397866363137616631636137373465626264366432336538666531326335366434316432356132326438316538386636376336633665653064348b6372656174655f75736572cecdccc6847465787480c483666f6f8765786563757465640a846173646686636f6e63617480",
    "warning": "HASH MISMATCH: requested 31303234; received f866b4251d21552de1bc5b819a4b563a540146954e956e8150163574ce5325ac"
  },
  "error": ""
}
Example (RespTxQuery_json)
Print(&RespTxQuery{Msg: getExampleTxQueryResponse()}, nil, "json")
Output:

{
  "result": {
    "hash": "31303234",
    "height": 10,
    "tx": {
      "signature": {
        "sig": "yz/tf2/zblkFTASoMbIV5RQFJ1PuNT5v4x1LTvc2rNYVUSfbVV0wBroU/LTHm7rVbI5juBqYljGbsFOp4lNHWAA=",
        "type": "secp256k1_ep"
      },
      "body": {
        "desc": "This is a test transaction for cli",
        "payload": "AAH4Vrg5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyzs3MxoR0ZXh0gMSDZm9v",
        "type": "execute",
        "fee": "100",
        "nonce": 10,
        "chain_id": "asdf"
      },
      "serialization": "concat",
      "sender": ""
    },
    "tx_result": {
      "code": 0,
      "log": "This is log",
      "gas_used": 10,
      "gas_wanted": 10
    }
  },
  "error": ""
}
Example (RespTxQuery_text)
Print(&RespTxQuery{Msg: getExampleTxQueryResponse(), WithRaw: true}, nil, "text")
// Transaction ID: 31303234
// Status: success
// Height: 10
// Log: This is log
// Raw: 0001f8eaf850b841cb3fed7f6ff36e59054c04a831b215e514052753ee353e6fe31d4b4ef736acd6155127db555d3006ba14fcb4c79bbad56c8e63b81a9896319bb053a9e2534758008c736563703235366b315f6570f88ea25468697320697320612074657374207472616e73616374696f6e20666f7220636c69b85a0001f856b8397866363137616631636137373465626264366432336538666531326335366434316432356132326438316538386636376336633665653064348b6372656174655f75736572cecdccc6847465787480c483666f6f8765786563757465640a846173646686636f6e63617480
// WARNING! HASH MISMATCH:
// 	Requested 31303234
// 	Received  f866b4251d21552de1bc5b819a4b563a540146954e956e8150163574ce5325ac
Output:

Example (WrappedMsg_json)
msg := wrapMsg(&demoFormat{data: []byte("demo")}, nil)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": {
    "name_to_whatever": "demo_whatever"
  },
  "error": ""
}
Example (WrappedMsg_json_withError)
err := errors.New("an error")
msg := wrapMsg(&demoFormat{data: []byte("demo")}, err)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": null,
  "error": "an error"
}
Example (WrappedMsg_text)
msg := wrapMsg(&demoFormat{data: []byte("demo")}, nil)
prettyPrint(msg, "text", os.Stdout, os.Stderr)
Output:

Whatever format: demo

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindOutputFormatFlag

func BindOutputFormatFlag(cmd *cobra.Command)

BindOutputFormatFlag binds the output format flag to the command. This should be added on the root command.

func BindSilenceFlag

func BindSilenceFlag(cmd *cobra.Command)

BindSilenceFlag binds the silence flag to the passed command. If bound, the command will silence logs. If true, display commands will not print to stdout or stderr. The flag will be bound to all subcommands of the given command.

func Print

func Print(msg MsgFormatter, err error, format OutputFormat) error

Print is a helper function to wrap and print message in given format.

func PrintCmd

func PrintCmd(cmd *cobra.Command, msg MsgFormatter) error

PrintCmd prints output based on the commands output format flag. If not format flag is provided, it will default to text in stdout.

func PrintErr

func PrintErr(cmd *cobra.Command, err error) error

PrintErr prints the error according to the commands output format flag.

func ShouldSilence

func ShouldSilence(cmd *cobra.Command) bool

ShouldSilence returns the value of the silence flag

Types

type MsgFormatter

type MsgFormatter interface {
	json.Marshaler
	encoding.TextMarshaler
}

MsgFormatter is an interface that wraps the MarshalText and MarshalJSON It defines the requirements for something to be printed.

type OutputFormat

type OutputFormat string

OutputFormat is the format for command output It implements the pflag.Value interface

type RespString

type RespString string

RespString is used to represent a string in cli It implements the MsgFormatter interface

func (RespString) MarshalJSON

func (s RespString) MarshalJSON() ([]byte, error)

func (RespString) MarshalText

func (s RespString) MarshalText() ([]byte, error)

type RespTxHash

type RespTxHash []byte

RespTxHash is used to represent a transaction hash in cli NOTE: it's different from transactions.TxHash, this is for display purpose. It implements the MsgFormatter interface

Example (Json)
msg := wrapMsg(RespTxHash("1024"), nil)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": {
    "tx_hash": "31303234"
  },
  "error": ""
}
Example (Json_withError)
err := errors.New("an error")
msg := wrapMsg(RespTxHash("1024"), err)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": null,
  "error": "an error"
}
Example (Text)
msg := wrapMsg(RespTxHash("1024"), nil)
prettyPrint(msg, "text", os.Stdout, os.Stderr)
Output:

TxHash: 31303234

func (RespTxHash) Hex

func (h RespTxHash) Hex() string

func (RespTxHash) MarshalJSON

func (h RespTxHash) MarshalJSON() ([]byte, error)

func (RespTxHash) MarshalText

func (h RespTxHash) MarshalText() ([]byte, error)

type RespTxQuery

type RespTxQuery struct {
	Msg     *transactions.TcTxQueryResponse
	WithRaw bool
}

RespTxQuery is used to represent a transaction response in cli

func (*RespTxQuery) MarshalJSON

func (r *RespTxQuery) MarshalJSON() ([]byte, error)

func (*RespTxQuery) MarshalText

func (r *RespTxQuery) MarshalText() ([]byte, error)

type TxHashAndExecResponse

type TxHashAndExecResponse struct {
	Hash      RespTxHash   // embedding breaks MarshalJSON of composing types
	QueryResp *RespTxQuery `json:"exec_result"`
}

TxHashAndExecResponse is meant to combine the "tx_hash" marshalling of RespTxHash with a RespTxQuery in an "exec_result" field.

func NewTxHashAndExecResponse

func NewTxHashAndExecResponse(resp *transactions.TcTxQueryResponse) *TxHashAndExecResponse

NewTxHashAndExecResponse makes a TxHashAndExecResponse from a TcTxQueryResponse.

func (*TxHashAndExecResponse) MarshalJSON

func (h *TxHashAndExecResponse) MarshalJSON() ([]byte, error)

func (TxHashAndExecResponse) MarshalText

func (h TxHashAndExecResponse) MarshalText() ([]byte, error)

MarshalText deduplicates the tx hash for a compact readable output, unlike the JSON marshalling that is meant to be a composition of both RespTxHash and RespTxQuery.

type TxHashResponse

type TxHashResponse struct {
	TxHash string `json:"tx_hash"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL