github.com/gagliardetto/solana-go@v1.11.0/text/format/format.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package format
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/davecgh/go-spew/spew"
    21  	"github.com/gagliardetto/solana-go"
    22  	"github.com/gagliardetto/solana-go/text"
    23  	. "github.com/gagliardetto/solana-go/text"
    24  )
    25  
    26  func Program(name string, programID solana.PublicKey) string {
    27  	return IndigoBG("Program") + ": " + Bold(name) + " " + text.ColorizeBG(programID.String())
    28  }
    29  
    30  func Instruction(name string) string {
    31  	return Purple(Bold("Instruction")) + ": " + Bold(name)
    32  }
    33  
    34  func Param(name string, value interface{}) string {
    35  	return Sf(
    36  		Shakespeare(name)+": %s",
    37  		strings.TrimSpace(
    38  			prefixEachLineExceptFirst(
    39  				strings.Repeat(" ", len(name)+2),
    40  				strings.TrimSpace(spew.Sdump(value)),
    41  			),
    42  		),
    43  	)
    44  }
    45  
    46  func Account(name string, pubKey solana.PublicKey) string {
    47  	return Shakespeare(name) + ": " + text.ColorizeBG(pubKey.String())
    48  }
    49  
    50  func MetaIfSetByIndex(name string, metaSlice solana.AccountMetaSlice, index int) string {
    51  	if metaSlice == nil {
    52  		return Meta(name, nil)
    53  	}
    54  	if index >= len(metaSlice) {
    55  		return Meta(name, nil)
    56  	}
    57  	return Meta(name, metaSlice[index])
    58  }
    59  
    60  func Meta(name string, meta *solana.AccountMeta) string {
    61  	if meta == nil {
    62  		return Shakespeare(name) + ": " + "<nil>"
    63  	}
    64  	out := Shakespeare(name) + ": " + text.ColorizeBG(meta.PublicKey.String())
    65  	out += " ["
    66  	if meta.IsWritable {
    67  		out += "WRITE"
    68  	}
    69  	if meta.IsSigner {
    70  		if meta.IsWritable {
    71  			out += ", "
    72  		}
    73  		out += "SIGN"
    74  	}
    75  	out += "] "
    76  	return out
    77  }
    78  
    79  func prefixEachLineExceptFirst(prefix string, s string) string {
    80  	return foreachLine(s,
    81  		func(i int, line string) string {
    82  			if i == 0 {
    83  				return Lime(line) + "\n"
    84  			}
    85  			return prefix + Lime(line) + "\n"
    86  		})
    87  }
    88  
    89  type sf func(int, string) string
    90  
    91  func foreachLine(str string, transform sf) (out string) {
    92  	for idx, line := range strings.Split(str, "\n") {
    93  		out += transform(idx, line)
    94  	}
    95  	return
    96  }