Enable appending selection range to selection

This commit is contained in:
Takashi Kokubun 2014-07-04 09:12:11 +09:00
parent 9a0ff8e2c9
commit 1bebaa3dcd
3 changed files with 23 additions and 9 deletions

View file

@ -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)
}

10
ctx.go
View file

@ -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 {

View file

@ -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
}