diff --git a/action.go b/action.go index dbcd320..9e11a47 100644 --- a/action.go +++ b/action.go @@ -99,6 +99,7 @@ func init() { ActionFunc(doSelectAll).Register("SelectAll") ActionFunc(doSelectVisible).Register("SelectVisible") ActionFunc(doToggleSelectMode).Register("ToggleSelectMode") + ActionFunc(doCancelSelectMode).Register("CancelSelectMode") } func doRotateMatcher(i *Input, ev termbox.Event) { @@ -122,14 +123,23 @@ func doToggleSelection(i *Input, _ termbox.Event) { func doToggleSelectMode(i *Input, _ termbox.Event) { if i.IsSelectMode() { + for _, line := range i.RangeSelection() { + i.selection.Add(line) + } + i.selection.Add(i.currentLine) + i.selectionRangeStart = NoSelectionRange - i.selection = Selection{} } else { i.selectionRangeStart = i.currentLine } i.DrawMatches(nil) } +func doCancelSelectMode(i *Input, _ termbox.Event) { + i.selectionRangeStart = NoSelectionRange + i.DrawMatches(nil) +} + func doSelectNone(i *Input, _ termbox.Event) { i.selection.Clear() i.DrawMatches(nil) @@ -156,7 +166,7 @@ func doFinish(i *Input, _ termbox.Event) { i.selection.Add(i.currentLine) i.result = []Match{} - for _, lineno := range i.selection { + for _, lineno := range append(i.selection, i.RangeSelection()...) { if lineno <= len(i.current) { i.result = append(i.result, i.current[lineno-1]) } @@ -165,6 +175,11 @@ func doFinish(i *Input, _ termbox.Event) { } func doCancel(i *Input, ev termbox.Event) { + if i.IsSelectMode() { + doCancelSelectMode(i, ev) + return + } + // peco.Cancel -> end program, exit with failure i.ExitWith(1) } diff --git a/ctx.go b/ctx.go index c2a14e0..543853b 100644 --- a/ctx.go +++ b/ctx.go @@ -149,22 +149,22 @@ func (c *Ctx) IsSelectMode() bool { return c.selectionRangeStart != NoSelectionRange } -func (c *Ctx) ApplySelectionRange() { +func (c *Ctx) RangeSelection() Selection { if !c.IsSelectMode() { - return + return Selection{} } selectedLines := []int{} if c.selectionRangeStart < c.currentLine { - for i := c.selectionRangeStart; i <= c.currentLine; i++ { + for i := c.selectionRangeStart; i < c.currentLine; i++ { selectedLines = append(selectedLines, i) } } else { - for i := c.selectionRangeStart; i >= c.currentLine; i-- { + for i := c.selectionRangeStart; i > c.currentLine; i-- { selectedLines = append(selectedLines, i) } } - c.selection = Selection(selectedLines) + return Selection(selectedLines) } func (c *Ctx) Result() []Match { diff --git a/view.go b/view.go index f9efc19..d8c302c 100644 --- a/view.go +++ b/view.go @@ -112,7 +112,6 @@ func (v *View) movePage(p PagingRequest) { } else if v.current != nil && v.currentLine > len(v.current) { v.currentLine = 1 } - v.ApplySelectionRange() v.drawScreen(nil) } @@ -208,7 +207,7 @@ CALCULATE_PAGE: if n+currentPage.offset == v.currentLine { fgAttr = v.config.Style.Selected.fg bgAttr = v.config.Style.Selected.bg - } else if v.selection.Has(n + currentPage.offset) { + } else if v.selection.Has(n + currentPage.offset) || v.RangeSelection().Has(n + currentPage.offset) { fgAttr = v.config.Style.SavedSelection.fg bgAttr = v.config.Style.SavedSelection.bg }