github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/interactive/interactive_helpers.go (about)

     1  package interactive
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/turbot/go-kit/helpers"
     7  )
     8  
     9  type queryCompletionInfo struct {
    10  	Table        string
    11  	EditingTable bool
    12  }
    13  
    14  func getQueryInfo(text string) *queryCompletionInfo {
    15  	table := getTable(text)
    16  	prevWord := getPreviousWord(text)
    17  
    18  	return &queryCompletionInfo{
    19  		Table:        table,
    20  		EditingTable: isEditingTable(prevWord),
    21  	}
    22  }
    23  
    24  func isEditingTable(prevWord string) bool {
    25  	var editingTable = prevWord == "from"
    26  	return editingTable
    27  }
    28  
    29  func getTable(text string) string {
    30  	// split on space and remove empty results - they occur if there is a double space
    31  	split := helpers.RemoveFromStringSlice(strings.Split(text, " "), "")
    32  
    33  	for idx, word := range split {
    34  		if word == "from" {
    35  			if idx+1 < len(split) {
    36  				return split[idx+1]
    37  			}
    38  		}
    39  	}
    40  	return ""
    41  }
    42  
    43  func getPreviousWord(text string) string {
    44  	// create a new document up the previous space
    45  	finalSpace := strings.LastIndex(text, " ")
    46  	if finalSpace == -1 {
    47  		return ""
    48  	}
    49  	lastNotSpace := lastIndexByteNot(text[:finalSpace], ' ')
    50  	if lastNotSpace == -1 {
    51  		return ""
    52  	}
    53  	prevSpace := strings.LastIndex(text[:lastNotSpace], " ")
    54  	if prevSpace == -1 {
    55  		return ""
    56  	}
    57  	return text[prevSpace+1 : lastNotSpace+1]
    58  }
    59  
    60  func lastIndexByteNot(s string, c byte) int {
    61  	for i := len(s) - 1; i >= 0; i-- {
    62  		if s[i] != c {
    63  			return i
    64  		}
    65  	}
    66  	return -1
    67  }
    68  
    69  // if there are no spaces this is the first word
    70  func isFirstWord(text string) bool {
    71  	return strings.LastIndex(text, " ") == -1
    72  }
    73  
    74  // split the string by spaces and return the last segment
    75  func lastWord(text string) string {
    76  	return text[strings.LastIndex(text, " "):]
    77  }
    78  
    79  //
    80  // keeping this around because we may need
    81  // to revisit exit on non-darwin platforms.
    82  // as per line #128
    83  //
    84  //
    85  // https://github.com/c-bata/go-prompt/issues/59
    86  // func exit(_ *prompt.Buffer) {
    87  // 	fmt.Println("Ctrl+D :: exitCallback")
    88  // 	panic(utils.ExitCode(0))
    89  // }