Skip to content

Commit efa092a

Browse files
authored
Merge pull request #27 from koinos/more-polish
Check terminal for unicode support
2 parents a26c7fd + daee8ec commit efa092a

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

cmd/cli-wallet/interactive/interactive.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package interactive
22

33
import (
44
"fmt"
5+
"os"
6+
"strings"
57

68
"github.com/c-bata/go-prompt"
79
"github.com/koinos/koinos-cli-wallet/internal/wallet"
@@ -13,6 +15,12 @@ type KoinosPrompt struct {
1315
execEnv *wallet.ExecutionEnvironment
1416
gPrompt *prompt.Prompt
1517
commandSuggestions []prompt.Suggest
18+
unicodeSupport bool
19+
20+
onlineDisplay string
21+
offlineDisplay string
22+
openDisplay string
23+
closeDisplay string
1624
}
1725

1826
// NewKoinosPrompt creates a new interactive prompt object
@@ -32,20 +40,37 @@ func NewKoinosPrompt(parser *wallet.CommandParser, execEnv *wallet.ExecutionEnvi
3240
kp.commandSuggestions = append(kp.commandSuggestions, prompt.Suggest{Text: cmd.Name, Description: cmd.Description})
3341
}
3442

43+
// Check for terminal unicode support
44+
lang := strings.ToUpper(os.Getenv("LANG"))
45+
kp.unicodeSupport = strings.Contains(lang, "UTF")
46+
47+
// Setup status characters
48+
if kp.unicodeSupport {
49+
kp.onlineDisplay = "🟢"
50+
kp.offlineDisplay = "🔴"
51+
kp.closeDisplay = "🔐"
52+
kp.openDisplay = "🔓"
53+
} else {
54+
kp.onlineDisplay = "(online)"
55+
kp.offlineDisplay = "(offline)"
56+
kp.closeDisplay = "(locked)"
57+
kp.openDisplay = "(unlocked)"
58+
}
59+
3560
return kp
3661
}
3762

3863
func (kp *KoinosPrompt) changeLivePrefix() (string, bool) {
3964
// Calculate online status
40-
onlineStatus := "🔴"
65+
onlineStatus := kp.offlineDisplay
4166
if kp.execEnv.IsOnline() {
42-
onlineStatus = "🟢"
67+
onlineStatus = kp.onlineDisplay
4368
}
4469

4570
// Calculate wallet status
46-
walletStatus := "🔐"
71+
walletStatus := kp.closeDisplay
4772
if kp.execEnv.IsWalletOpen() {
48-
walletStatus = "🔓"
73+
walletStatus = kp.openDisplay
4974
}
5075

5176
return fmt.Sprintf("%s %s > ", onlineStatus, walletStatus), true
@@ -69,7 +94,7 @@ func (kp *KoinosPrompt) executor(input string) {
6994

7095
// Run runs interactive mode
7196
func (kp *KoinosPrompt) Run() {
72-
fmt.Println("Koinos CLI Wallet")
97+
fmt.Println(fmt.Sprintf("Koinos CLI Wallet %s", wallet.Version))
7398
fmt.Println("Type \"list\" for a list of commands, \"help <command>\" for help on a specific command.")
7499
kp.gPrompt.Run()
75100
}

cmd/cli-wallet/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/joho/godotenv"
78
"github.com/koinos/koinos-cli-wallet/cmd/cli-wallet/interactive"
@@ -13,6 +14,7 @@ import (
1314
const (
1415
rpcOption = "rpc"
1516
executeOption = "execute"
17+
versionOption = "version"
1618
)
1719

1820
// Default options
@@ -31,9 +33,15 @@ func main() {
3133
// Setup command line options
3234
rpcAddress := flag.StringP(rpcOption, "r", rpcDefault, "RPC server URL")
3335
executeCmd := flag.StringP(executeOption, "x", executeDefault, "Command to execute")
36+
versionCmd := flag.BoolP(versionOption, "v", false, "Display the version")
3437

3538
flag.Parse()
3639

40+
if *versionCmd {
41+
fmt.Println(wallet.Version)
42+
os.Exit(0)
43+
}
44+
3745
// Setup client
3846
var client *wallet.KoinosRPCClient
3947
if *rpcAddress != "" {

internal/wallet/commands.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func (cs *CommandSet) List(pretty bool) []string {
7676

7777
// If pretty output add descriptions
7878
o := make([]string, 0)
79-
for i, name := range names {
80-
o = append(o, fmt.Sprintf("%*s - %s", -longest, name, cs.Commands[i].Description))
79+
for _, name := range names {
80+
o = append(o, fmt.Sprintf("%*s - %s", -longest, name, cs.Name2Command[name].Description))
8181
}
8282

8383
return o

internal/wallet/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import (
1717
"github.com/ybbus/jsonrpc/v2"
1818
)
1919

20+
const (
21+
// Version number (this should probably not live here)
22+
Version = "v0.1.2"
23+
)
24+
2025
// SignTransaction signs the transaction with the given key
2126
func SignTransaction(key []byte, tx *types.Transaction) error {
2227
privateKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), key)

0 commit comments

Comments
 (0)