// PrevFilteredUntilSelection is like PrevUntilSelection, with the
// option to filter the results based on a selector string. It returns a new
// Selection object containing the matched elements.
335723
func (s *Selection) PrevFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {
	return s.PrevMatcherUntilSelection(compileMatcher(filterSelector), sel)
}
344700
func (s *Selection) ParentsFilteredUntil(filterSelector, untilSelector string) *Selection {
	return filterAndPush(s, getParentsNodes(s.Nodes, compileMatcher(untilSelector), nil), compileMatcher(filterSelector))
}
579748
func (s *Selection) NextMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection {
	return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,
		nil, nodes), filter)
}
1133461
func (c *Menu) GetSelectedIndex() int {
	for idx := range c.options {
		if c.options[idx].IsSelected() {
			return idx
		}
	}
	return -1
}
269360
func (s *Selection) ParentsFiltered(selector string) *Selection {
	return filterAndPush(s, getParentsNodes(s.Nodes, nil, nil), compileMatcher(selector))
}
1323577
func (s *Selection) PrependSelection(sel *Selection) *Selection {
	return s.PrependNodes(sel.Nodes...)
}
689197
func NewFilterOption() *DialogOption {
	return &DialogOption{
		Description: []string{
			"Insert a regular expression to filter the story list.",
			"(case insensitive match against the story title)",
		},
		IsActive: func(stories []common.Story, depth int) bool {
			return len(stories) != 0
		},
		MatchesInput: func(input string, stories []common.Story) bool {
			return true
		},
		SelectStory: func(
			input string,
			stories []common.Story,
			currentDialog *Dialog,
		) (common.Story, error) {

			fmt.Println()
			fmt.Printf("Using '%v' to filter stories ...\n", input)
			fmt.Println()

			filter, err := regexp.Compile("(?i)" + input)
			if err != nil {
				return nil, err
			}
			filteredStories := common.FilterStories(stories, func(story common.Story) bool {
				return filter.MatchString(story.Title())
			})

			subdialog := currentDialog.NewSubdialog()
			subdialog.opts = currentDialog.opts
			return subdialog.Run(filteredStories)
		},
	}
}
1142085
func regexpSelection(text []byte, expr string) Selection {
	var matches [][]int
	if rx, err := regexp.Compile(expr); err == nil {
		matches = rx.FindAllIndex(text, -1)
	}
	return makeSelection(matches)
}
648391
func NewMultiRowRangeFilter(rowRangeList []*RowRange) *MultiRowRangeFilter {
	rangeList := make([]*pb.RowRange, len(rowRangeList))
	for i, rr := range rowRangeList {
		rangeList[i] = (*pb.RowRange)(rr)
	}
	return &MultiRowRangeFilter{
		RowRangeList: rangeList,
	}
}
89925
func parseSelection(parser *Parser) (interface{}, error) {
	if peek(parser, lexer.SPREAD) {
		return parseFragment(parser)
	}
	return parseField(parser)
}
1327069
func (f *FilterQLParser) ParseFilter() (*FilterSelect, error) {
	f.setLexer(f.statement)
	return f.parseSelectStart()
}
118582
func newEventInputSelector(awsSession *session.Session,
	app *tview.Application,
	lambdaAWSInfos []*LambdaAWSInfo,
	settings map[string]string,
	functionSelectedBroadcaster broadcast.Broadcaster,
	logger *logrus.Logger) (tview.Primitive, []tview.Primitive) {

	divider := strings.Repeat("━", 20)
	activeFunction := ""
	ch := make(chan interface{})
	functionSelectedBroadcaster.Register(ch)
	go func() {
		//lint:ignore S1000 to make the check happy
		for {
			select {
			case funcSelected := <-ch:
				activeFunction = funcSelected.(string)
			}
		}
	}()
	lambdaSvc := lambda.New(awsSession)

	// First walk the directory for anything that looks
	// like a JSON file...
	curDir, curDirErr := os.Getwd()
	if curDirErr != nil {
		return nil, nil
	}
	jsonFiles := []string{}
	walkerFunc := func(path string, info os.FileInfo, err error) error {
		if strings.ToLower(filepath.Ext(path)) == ".json" &&
			!strings.Contains(path, ScratchDirectory) {
			relPath := strings.TrimPrefix(path, curDir)
			jsonFiles = append(jsonFiles, relPath)
			logger.WithField("RelativePath", relPath).Debug("Event file found")
		}
		return nil
	}
	walkErr := filepath.Walk(curDir, walkerFunc)
	if walkErr != nil {
		logger.WithError(walkErr).Error("Failed to find JSON files in directory: " + curDir)
		return nil, nil
	}
	// Create all the views...
	var selectedJSONData []byte
	selectedInput := 0
	eventSelected := settings[settingSelectedEvent]
	for index, eachJSONFile := range jsonFiles {
		if eventSelected == eachJSONFile {
			selectedInput = index
			break
		}
	}
	eventDataView := tview.NewTextView().SetScrollable(true).SetDynamicColors(true)
	dropdown := tview.NewDropDown().
		SetCurrentOption(selectedInput).
		SetLabel("Event: ").
		SetOptions(jsonFiles, nil)

	submitEventData := func(key tcell.Key) {
		// What's the selected item?
		selected, value := dropdown.GetCurrentOption()
		if selected == -1 {
			return
		}
		eventDataView.Clear()
		// Save it...
		saveSetting(settingSelectedEvent, value)
		fullPath := curDir + value
		/* #nosec */
		jsonFile, jsonFileErr := ioutil.ReadFile(fullPath)
		if jsonFileErr != nil {
			writePrettyString(eventDataView, jsonFileErr.Error())
		} else {
			writePrettyString(eventDataView, string(jsonFile))
		}
		selectedJSONData = jsonFile
	}
	submitEventData(tcell.KeyEnter)
	dropdown.SetDoneFunc(submitEventData)
	submitButton := tview.NewButton("Submit")
	submitButton.SetBackgroundColorActivated(tcell.ColorDarkGreen)
	submitButton.SetLabelColorActivated(tcell.ColorWhite)
	submitButton.SetBackgroundColor(tcell.ColorGray)
	submitButton.SetLabelColor(tcell.ColorDarkGreen)
	submitButton.SetSelectedFunc(func() {
		if activeFunction == "" {
			return
		}
		// Submit it to lambda
		if activeFunction != "" {
			lambdaInput := &lambda.InvokeInput{
				FunctionName: aws.String(activeFunction),
				Payload:      selectedJSONData,
			}
			invokeOutput, invokeOutputErr := lambdaSvc.Invoke(lambdaInput)
			if invokeOutputErr != nil {
				logger.WithFields(logrus.Fields{
					"Error": invokeOutputErr,
				}).Error("Failed to invoke Lambda function")
			} else if invokeOutput.FunctionError != nil {
				logger.WithFields(logrus.Fields{
					"Error": invokeOutput.FunctionError,
				}).Error("Lambda function produced an error")
			} else {
				var m interface{}

				jsonErr := json.Unmarshal(invokeOutput.Payload, &m)
				var responseData interface{}
				if jsonErr == nil {
					responseData = m
				} else {
					responseData = string(invokeOutput.Payload)
				}
				logger.WithFields(logrus.Fields{
					"payload": responseData,
				}).Info(divider + " AWS Lambda Response " + divider)
			}
		}
	})

	// Ok, so what we need now is a flexbox with a row,
	flexRow := tview.NewFlex().SetDirection(tview.FlexColumn).
		AddItem(dropdown, 0, 4, false).
		AddItem(submitButton, 10, 1, false)

	flex := tview.NewFlex().SetDirection(tview.FlexRow).
		AddItem(flexRow, 1, 0, false).
		AddItem(eventDataView, 0, 1, false)
	flex.SetBorder(true).SetTitle("Select Event Input")
	return flex, []tview.Primitive{dropdown, submitButton, eventDataView}
}
426248
func ExtractOptionalFilters(opts ...interface{}) *opt.OptionalFiltersOption {
	for _, o := range opts {
		if v, ok := o.(*opt.OptionalFiltersOption); ok {
			return v
		}
	}
	return nil
}
362910
func (s *Selection) FilterFunction(f func(int, *Selection) bool) *Selection {
	return pushStack(s, winnowFunction(s, f, true))
}
474885
func (r *SearchRequest) PostFilter(filter Query) *SearchRequest {
	r.searchSource = r.searchSource.PostFilter(filter)
	return r
}
808987
func (s *Selection) IndexOfSelection(sel *Selection) int {
	if sel != nil && len(sel.Nodes) > 0 {
		return indexInSlice(s.Nodes, sel.Nodes[0])
	}
	return -1
}
419859
func (s *Selection) ParentsUntilMatcher(m Matcher) *Selection {
	return pushStack(s, getParentsNodes(s.Nodes, m, nil))
}