github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/cmd/lit-af/autocomplete.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chzyer/readline"
     7  	"github.com/mit-dci/lit/litrpc"
     8  )
     9  
    10  func (lc *litAfClient) completePeers(line string) []string {
    11  	names := make([]string, 0)
    12  	pReply := new(litrpc.ListConnectionsReply)
    13  	err := lc.Call("LitRPC.ListConnections", nil, pReply)
    14  	if err != nil {
    15  		return names
    16  	}
    17  	if len(pReply.Connections) > 0 {
    18  		for _, peer := range pReply.Connections {
    19  			var peerStr = fmt.Sprint(peer.PeerNumber)
    20  			names = append(names, peerStr)
    21  		}
    22  	}
    23  	return names
    24  }
    25  
    26  func (lc *litAfClient) completeClosedPeers(line string) []string {
    27  	channelpeers := make([]string, 0)
    28  	connectedpeers := make([]string, 0)
    29  	pReply := new(litrpc.ListConnectionsReply)
    30  	cReply := new(litrpc.ChannelListReply)
    31  	err := lc.Call("LitRPC.ListConnections", nil, pReply)
    32  	if err != nil {
    33  		return channelpeers
    34  	}
    35  	err = lc.Call("LitRPC.ChannelList", nil, cReply)
    36  	if err != nil {
    37  		return channelpeers
    38  	}
    39  	if len(pReply.Connections) > 0 {
    40  		for _, peer := range pReply.Connections {
    41  			var peerStr = fmt.Sprint(peer.PeerNumber)
    42  			connectedpeers = append(connectedpeers, peerStr)
    43  		}
    44  	}
    45  	for _, c := range cReply.Channels {
    46  		var peerid = fmt.Sprint(c.PeerIdx)
    47  		var found = false
    48  		for _, v := range append(connectedpeers, channelpeers...) {
    49  			if v == peerid {
    50  				found = true
    51  				break
    52  			}
    53  		}
    54  		if !found {
    55  			channelpeers = append(channelpeers, peerid)
    56  		}
    57  	}
    58  	return channelpeers
    59  }
    60  
    61  func (lc *litAfClient) completeChannelIdx(line string) []string {
    62  	names := make([]string, 0)
    63  	cReply := new(litrpc.ChannelListReply)
    64  	err := lc.Call("LitRPC.ChannelList", nil, cReply)
    65  	if err != nil {
    66  		return names
    67  	}
    68  	for _, c := range cReply.Channels {
    69  		if !c.Closed {
    70  			var cidxStr = fmt.Sprint(c.CIdx)
    71  			names = append(names, cidxStr)
    72  		}
    73  	}
    74  	return names
    75  }
    76  
    77  func (lc *litAfClient) NewAutoCompleter() readline.AutoCompleter {
    78  	var completer = readline.NewPrefixCompleter(
    79  		readline.PcItem("help",
    80  			readline.PcItem("say"),
    81  			readline.PcItem("ls"),
    82  			readline.PcItem("con"),
    83  			readline.PcItem("lis"),
    84  			readline.PcItem("adr"),
    85  			readline.PcItem("send"),
    86  			readline.PcItem("fan"),
    87  			readline.PcItem("sweep"),
    88  			readline.PcItem("fund"),
    89  			readline.PcItem("dualfund"),
    90  			readline.PcItem("push"),
    91  			readline.PcItem("close"),
    92  			readline.PcItem("break"),
    93  			readline.PcItem("stop"),
    94  			readline.PcItem("exit"),
    95  		),
    96  		readline.PcItem("say",
    97  			readline.PcItemDynamic(lc.completePeers)),
    98  		readline.PcItem("ls"),
    99  		readline.PcItem("con",
   100  			readline.PcItemDynamic(lc.completeClosedPeers)),
   101  		readline.PcItem("lis"),
   102  		readline.PcItem("adr"),
   103  		readline.PcItem("send"),
   104  		readline.PcItem("fan"),
   105  		readline.PcItem("sweep"),
   106  		readline.PcItem("fund",
   107  			readline.PcItemDynamic(lc.completePeers)),
   108  		readline.PcItem("push",
   109  			readline.PcItemDynamic(lc.completeChannelIdx)),
   110  		readline.PcItem("close",
   111  			readline.PcItemDynamic(lc.completeChannelIdx)),
   112  		readline.PcItem("break",
   113  			readline.PcItemDynamic(lc.completeChannelIdx)),
   114  		readline.PcItem("stop"),
   115  		readline.PcItem("exit"),
   116  	)
   117  
   118  	return completer
   119  }