-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement selecting from multiple cuts
- Loading branch information
Showing
10 changed files
with
227 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package edits | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
ps "github.com/xanni/jotty/permascroll" | ||
) | ||
|
||
const ( | ||
layout = time.DateTime | ||
minCut = 5 | ||
) | ||
|
||
// Select previous cut. | ||
func PrevCut() { | ||
if ps.Cuts() > 0 { | ||
Mode = Cuts | ||
currentCut-- | ||
if currentCut < 1 { | ||
currentCut = ps.Cuts() | ||
} | ||
} | ||
} | ||
|
||
// Select next cut. | ||
func NextCut() { | ||
if ps.Cuts() > 0 { | ||
Mode = Cuts | ||
currentCut++ | ||
if currentCut > ps.Cuts() { | ||
currentCut = 1 | ||
} | ||
} | ||
} | ||
|
||
func drawCut(current bool, text string, ts time.Time) (s string) { | ||
maxLen := ex - len(layout) - 2 | ||
if maxLen < minCut { | ||
maxLen = ex - 1 | ||
} else { | ||
switch { | ||
case ts.IsZero(): | ||
s = strings.Repeat(" ", len(layout)+1) | ||
case current: | ||
s = cutCurStyle(ts.Format(layout)) + " " | ||
default: | ||
s = cutTimeStyle(ts.Format(layout)) + " " | ||
} | ||
} | ||
|
||
if current { | ||
s += truncate(maxLen, text) | ||
} else { | ||
s += cutStyle(truncate(maxLen, text)) | ||
} | ||
|
||
return s | ||
} | ||
|
||
// The preceding, current and following cuts. | ||
func cutsWindow() (w []string) { | ||
w = []string{cutWinStyle(strings.Repeat("—", ex))} | ||
|
||
if currentCut > 1 { | ||
text, ts := ps.GetCut(currentCut - 1) | ||
w = append(w, drawCut(false, text, ts)) | ||
} | ||
|
||
text, ts := ps.GetCut(currentCut) | ||
w = append(w, drawCut(true, text, ts)) | ||
|
||
if currentCut < ps.Cuts() { | ||
text, ts := ps.GetCut(currentCut + 1) | ||
w = append(w, drawCut(false, text, ts)) | ||
} | ||
|
||
return w | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package edits | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
ps "github.com/xanni/jotty/permascroll" | ||
) | ||
|
||
func TestDrawCut(t *testing.T) { | ||
assert := assert.New(t) | ||
ResizeScreen(4, 2) | ||
assert.Equal("T…t", drawCut(false, "Test", time.Time{})) | ||
|
||
ResizeScreen(5, 2) | ||
assert.Equal("Test", drawCut(false, "Test", time.Time{})) | ||
|
||
ResizeScreen(26, 2) | ||
assert.Equal(" Test", drawCut(false, "Test", time.Time{})) | ||
ts := time.Date(2020, time.January, 2, 3, 4, 5, 6, time.UTC) | ||
expect := "2020-01-02 03:04:05 Test" | ||
assert.Equal(expect, drawCut(false, "Test", ts), "unselected") | ||
assert.Equal(expect, drawCut(true, "Test", ts), "selected") | ||
} | ||
|
||
func TestCutsWindow(t *testing.T) { | ||
assert := assert.New(t) | ||
setupTest() | ||
ResizeScreen(5, 2) | ||
ps.Init("I1,0:Test\nC1,0+4\n") | ||
currentCut = 1 | ||
assert.Equal([]string{"—————", "Test"}, cutsWindow()) | ||
|
||
currentCut = ps.CopyText(1, 0, 1) | ||
ps.CopyText(1, 2, 4) | ||
assert.Equal([]string{"—————", "Test", "T", "st"}, cutsWindow()) | ||
} | ||
|
||
func TestPrevCut(t *testing.T) { | ||
assert := assert.New(t) | ||
setupTest() | ||
|
||
PrevCut() | ||
assert.Equal(None, Mode) | ||
assert.Equal(0, currentCut) | ||
|
||
ps.Init("I1,0:Test\nC1,0+4\n") | ||
PrevCut() | ||
assert.Equal(Cuts, Mode) | ||
assert.Equal(1, currentCut) | ||
|
||
currentCut = ps.CopyText(1, 1, 3) | ||
PrevCut() | ||
assert.Equal(Cuts, Mode) | ||
assert.Equal(1, currentCut) | ||
} | ||
|
||
func TestNextCut(t *testing.T) { | ||
assert := assert.New(t) | ||
setupTest() | ||
|
||
NextCut() | ||
assert.Equal(None, Mode) | ||
assert.Equal(0, currentCut) | ||
|
||
ps.Init("I1,0:Test\nC1,0+4\n") | ||
NextCut() | ||
assert.Equal(Cuts, Mode) | ||
assert.Equal(1, currentCut) | ||
|
||
currentCut = ps.CopyText(1, 1, 3) | ||
NextCut() | ||
assert.Equal(Cuts, Mode) | ||
assert.Equal(1, currentCut) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters