Skip to content

Commit

Permalink
WIP: Improve help
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Feb 28, 2024
1 parent fdba3f2 commit 8a309dc
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 129 deletions.
16 changes: 8 additions & 8 deletions config/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func GetNodeMultibasePrivKey() string {

func GetNodeIdentity() crypto.PrivKey {

log.Debugf("config.GetNodeIdentity: %s", viper.GetString("libp2p.identity"))
_, privKeyBytes, err := mb.Decode(viper.GetString("libp2p.identity"))
log.Debugf("config.GetNodeIdentity: %s", viper.GetString("p2p.identity"))
_, privKeyBytes, err := mb.Decode(viper.GetString("p2p.identity"))
if err != nil {
log.Debugf("config.GetNodeIdentity: Failed to decode node identity: %v", err)
return nil
Expand Down Expand Up @@ -125,47 +125,47 @@ func GetDiscoveryContext() (context.Context, func()) {
}

func GetDiscoveryTimeout() time.Duration {
return time.Duration(viper.GetDuration("libp2p.discovery-timeout"))
return time.Duration(viper.GetDuration("p2p.discovery-timeout"))
}

func GetDiscoveryTimeoutString() string {
return GetDiscoveryTimeout().String()
}

func GetLowWatermark() int {
return viper.GetInt("libp2p.connmgr.low-watermark")
return viper.GetInt("p2p.connmgr.low-watermark")
}

func GetLowWatermarkString() string {
return fmt.Sprint(GetLowWatermark())
}

func GetHighWatermark() int {
return viper.GetInt("libp2p.connmgr.high-watermark")
return viper.GetInt("p2p.connmgr.high-watermark")
}

func GetHighWatermarkString() string {
return fmt.Sprint(GetHighWatermark())
}

func GetConnMgrGracePeriod() time.Duration {
return viper.GetDuration("libp2p.connmgr.grace-period")
return viper.GetDuration("p2p.connmgr.grace-period")
}

func GetConnMgrGraceString() string {
return GetConnMgrGracePeriod().String()
}

func GetListenPort() int {
return viper.GetInt("libp2p.port")
return viper.GetInt("p2p.port")
}

func GetListenPortString() string {
return strconv.Itoa(GetListenPort())
}

func GetDiscoveryRetryInterval() time.Duration {
return viper.GetDuration("libp2p.discovery-retry")
return viper.GetDuration("p2p.discovery-retry")
}

func GetDiscoveryRetryIntervalString() string {
Expand Down
5 changes: 4 additions & 1 deletion ui/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,8 @@ func (ui *ChatUI) enterEntity(d string, force bool) error {

func (ui *ChatUI) handleHelpEnterCommand(args []string) {
ui.displaySystemMessage("Usage: /enter <DID>")
ui.displaySystemMessage("Enters a chat room with the specified DID")
ui.displaySystemMessage("Enters an entity with the specified DID.")
ui.displaySystemMessage("What this means is that messages will be sent to this entity.")
ui.displaySystemMessage("Everybody 'in' the entity will be able to read the messages.")
ui.displaySystemMessage("NB! use /msg to send encrypted messages to any recipient.")
}
132 changes: 77 additions & 55 deletions ui/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,21 @@ import (
)

func (ui *ChatUI) handleHelpEntityCommands(args []string) {
ui.displaySystemMessage("Usage: /entity remove|show|nick")
ui.displaySystemMessage("Manages entity info")
ui.displayHelpUsage("/entity nick list|remove|show|nick")
ui.displayHelpText("Manages entity info")
ui.displayHelpText("At this point only nick are handled")
}

func (ui *ChatUI) handleEntityCommand(args []string) {

if len(args) >= 2 {
command := args[1]
switch command {
case "list":
ui.handleEntityListCommand(args)
return
case "nick":
ui.handleEntityNickCommand(args)
return
case "remove":
ui.handleEntityRemoveCommand(args)
return
case "show":
ui.handleEntityShowCommand(args)
ui.handleEntityNickShowCommand(args)
return
default:
ui.displaySystemMessage("Unknown alias entity command: " + command)
Expand All @@ -38,10 +33,10 @@ func (ui *ChatUI) handleEntityCommand(args []string) {

}

func (ui *ChatUI) handleEntityListCommand(args []string) {
func (ui *ChatUI) handleEntityNickListCommand(args []string) {

log.Debugf("entity list command: %v", args)
if len(args) == 2 {
if len(args) == 3 {

nicks := entity.ListNicks()
log.Debugf("entities: %v", nicks)
Expand All @@ -60,77 +55,86 @@ func (ui *ChatUI) handleEntityListCommand(args []string) {
}

func (ui *ChatUI) handleHelpEntityListCommand(args []string) {
ui.displaySystemMessage("Usage: /entity list")
ui.displaySystemMessage("List entity DID and nicks")
ui.displayHelpUsage("/entity list")
ui.displayHelpText("List entity DID and nicks")
}

func (ui *ChatUI) handleHelpEntityRemoveCommand() {
ui.displaySystemMessage("/entity remove <id|nick>")
func (ui *ChatUI) handleHelpEntityNickCommand(args []string) {
ui.displayHelpUsage("/entity nick set|remove|show")
ui.displayHelpText("Set a nick for a entity")
ui.handleEntityNickSetCommand(args)
ui.handleEntityNickRemoveCommand(args)
ui.handleEntityNickShowCommand(args)
}

func (ui *ChatUI) handleEntityRemoveCommand(args []string) {
// case "remove":
// ui.handleEntityRemoveCommand(args)
// return

if len(args) == 3 {
err := entity.RemoveNick(args[3])
if err != nil {
ui.displaySystemMessage("Error removing entity: " + err.Error())
func (ui *ChatUI) handleEntityNickCommand(args []string) {

if len(args) >= 3 {
command := args[2]
switch command {
case "list":
ui.handleEntityNickListCommand(args)
return
case "set":
ui.handleEntityNickSetCommand(args)
return
case "remove":
ui.handleEntityNickRemoveCommand(args)
return
case "show":
ui.handleEntityNickShowCommand(args)
return
default:
ui.displaySystemMessage("Unknown alias entity command: " + command)
}
} else {
ui.handleHelpEntityRemoveCommand()
}

ui.handleHelpEntityNickCommand(args)
}

func (ui *ChatUI) handleHelpEntityNickCommand(args []string) {
ui.displaySystemMessage("Usage: /entity nick <id> <nick>")
ui.displaySystemMessage("Set a nick for a entity")
}

func (ui *ChatUI) handleEntityNickCommand(args []string) {

// No nick given, hence just show the existing nick
if len(args) == 3 {
p, err := entity.Lookup(args[2])
if err != nil {
ui.displaySystemMessage("Error fetching alias: " + err.Error())
return
}
log.Debugf("%s: %s", p.DID, p.Nick)
ui.displaySystemMessage(fmt.Sprintf("Alias for %s is set to %s", p.DID, p.Nick))
return
}
// SET
func (ui ChatUI) handleEntityNickSetCommand(args []string) {

if len(args) == 4 {
e, err := entity.Lookup(args[2])
if len(args) == 5 {
id := args[3]
nick := args[4]
e, err := entity.Lookup(id)
if err != nil {
ui.displaySystemMessage("Error fetching alias: " + err.Error())
ui.displaySystemMessage("Error: " + err.Error())
return
}
err = e.SetNick(args[3])
e.SetNick(nick)
if err != nil {
ui.displaySystemMessage("Error setting alias: " + err.Error())
ui.displaySystemMessage("Error setting entity nick: " + err.Error())
return
}
log.Debugf("Setting alias for %s to %s", e.DID, e.Nick)
ui.displaySystemMessage(fmt.Sprintf("Alias for %s set to %s", e.DID, e.Nick))
return
ui.displaySystemMessage(e.DID.Id + " is now known as " + e.Nick)
} else {
ui.handleHelpEntityNickSetCommand(args)
}

ui.handleHelpEntityNickCommand(args)
}

func (ui *ChatUI) handleHelpEntityNickSetCommand(args []string) {
ui.displayHelpUsage("/entity nick set <id|nick> <nick>")
ui.displayHelpText("Sets a nick for an entity")
}

func (ui *ChatUI) handleEntityShowCommand(args []string) {
// SHOW
func (ui *ChatUI) handleEntityNickShowCommand(args []string) {

if len(args) == 3 {
id := args[2]
if len(args) == 4 {
id := args[3]
e, err := entity.Lookup(id)
if err != nil {
ui.displaySystemMessage("Error: " + err.Error())
return
}
entityInfo := fmt.Sprintf("DID: %s\nNick: %s\n", e.DID, e.Nick)
entityInfo := fmt.Sprintf(e.DID.Id + " is also known as " + e.Nick)
ui.displaySystemMessage(entityInfo)
} else {
ui.handleHelpEntityShowCommand(args)
Expand All @@ -139,6 +143,24 @@ func (ui *ChatUI) handleEntityShowCommand(args []string) {
}

func (ui *ChatUI) handleHelpEntityShowCommand(args []string) {
ui.displaySystemMessage("Usage: /entity show <id|nick>")
ui.displaySystemMessage("Shows the entity info")
ui.displayHelpUsage("/entity nick show <id|nick>")
ui.displayHelpText("Shows the entity info")
}

// REMOVE
func (ui *ChatUI) handleEntityNickRemoveCommand(args []string) {

if len(args) == 4 {
id := entity.GetDID(args[3])
entity.RemoveNick(id)
ui.displaySystemMessage("Nick removed for " + id + " if it existed")
} else {
ui.handleHelpEntityNickRemoveCommand(args)
}

}

func (ui *ChatUI) handleHelpEntityNickRemoveCommand(args []string) {
ui.displayHelpUsage("/entity nick remove <id|nick>")
ui.displayHelpText("Removes a nick for an entity")
}
10 changes: 6 additions & 4 deletions ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (

// // displaySelfMessage writes a message from ourself to the message window,
// // with our nick highlighted in yellow.
// func (ui *ChatUI) displaySelfMessage(msg string) {
// prompt := withColor("yellow", fmt.Sprintf("<%s>:", ui.e.Nick))
// fmt.Fprintf(ui.msgW, "%s %s\n", prompt, msg)
// }
//
// func (ui *ChatUI) displaySelfMessage(msg string) {
// prompt := withColor("yellow", fmt.Sprintf("<%s>:", ui.e.Nick))
// fmt.Fprintf(ui.msgW, "%s %s\n", prompt, msg)
// }
const indent = " "

// withColor wraps a string with color tags for display in the messages text box.
func withColor(color, msg string) string {
Expand Down
39 changes: 26 additions & 13 deletions ui/help.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
package ui

import "fmt"

func (ui *ChatUI) displayHelpUsage(msg string) {
out := withColor("purple", msg)
fmt.Fprintf(ui.msgW, "Usage: %s\n", out)
}
func (ui *ChatUI) displayHelpText(msg string) {
out := withColor("purple", msg)
fmt.Fprintf(ui.msgW, indent+"%s\n", out)
}

func (ui *ChatUI) handleHelpCommands(args []string) {

if len(args) == 1 {
ui.displaySystemMessage("Usage: /help [command]")
ui.displaySystemMessage("")
ui.displaySystemMessage("Available commands:")
ui.displaySystemMessage("/help")
ui.displaySystemMessage("/help alias")
ui.displaySystemMessage("/help aliases")
ui.displaySystemMessage("/help broadcast")
ui.displaySystemMessage("/help discover")
ui.displaySystemMessage("/help enter")
ui.displaySystemMessage("/help entity")
ui.displaySystemMessage("/help me # Pun intended")
ui.displaySystemMessage("/help msg")
ui.displaySystemMessage("/help peer")
ui.displaySystemMessage("/help quit")
ui.displaySystemMessage("/help refresh")
ui.displaySystemMessage("/help resolve")
ui.displaySystemMessage("/help status")
ui.displaySystemMessage("/help whereis")
ui.displaySystemMessage("/help")
} else {
switch args[1] {
case "status":
ui.handleHelpStatusCommands(args)
case "msg":
ui.handleHelpMsgCommand(args)
case "broadcast":
ui.handleHelpBroadcastCommand(args)
case "discover":
ui.handleHelpDiscoverCommand(args)
case "enter":
ui.handleHelpEnterCommand(args)
case "resolve":
ui.handleHelpResolveCommand(args)
// case "alias":
// ui.handleHelpAliasCommands(args)
case "whereis":
ui.handleHelpWhereisCommand(args)
case "entity":
ui.handleHelpEntityCommands(args)
case "me":
ui.handleHelpMeCommands(args)
case "msg":
ui.handleHelpMsgCommand(args)
case "peer":
ui.handleHelpPeerCommands(args)
case "refresh":
ui.handleHelpRefreshCommand(args)
case "resolve":
ui.handleHelpResolveCommand(args)
case "status":
ui.handleHelpStatusCommands(args)
case "whereis":
ui.handleHelpWhereisCommand(args)
case "quit":
ui.handleHelpQuitCommand(args)
default:
Expand Down
3 changes: 2 additions & 1 deletion ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func (ui *ChatUI) setupInputField() *tview.InputField {
inputField := tview.NewInputField().
SetLabel(viper.GetString("actor.nick") + ": ").
SetFieldWidth(0).
SetLabelColor(tcell.ColorBlack)
SetLabelColor(tcell.ColorBlack).
SetText("/help")

inputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
Expand Down
Loading

0 comments on commit 8a309dc

Please sign in to comment.