github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/events/onPrompt/interrupts.go (about)

     1  package onprompt
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type opInterrupt string
     9  
    10  const (
    11  	opInterruptBefore = "before"
    12  	opInterruptAfter  = "after"
    13  	opInterruptEOF    = "eof"
    14  	opInterruptCancel = "cancel"
    15  )
    16  
    17  var interrupts = []string{
    18  	opInterruptBefore,
    19  	opInterruptAfter,
    20  	opInterruptEOF,
    21  	opInterruptCancel,
    22  }
    23  
    24  func isValidInterrupt(interrupt string) error {
    25  	for i := range interrupts {
    26  		if interrupts[i] == interrupt {
    27  			return nil
    28  		}
    29  	}
    30  
    31  	return fmt.Errorf("invalid interrupt. Expecting one of the following: %s", strings.Join(interrupts, ", "))
    32  }
    33  
    34  func compileInterruptKey(interrupt, name string) string {
    35  	return fmt.Sprintf("%s_%s", interrupt, name)
    36  }
    37  
    38  func getInterruptFromKey(key string) []string {
    39  	split := strings.SplitN(key, "_", 2)
    40  	switch len(split) {
    41  	case 2:
    42  		return split
    43  	case 1:
    44  		return []string{"", split[0]}
    45  	default:
    46  		return []string{"", ""}
    47  	}
    48  }