Skip to content

Commit 4cf1642

Browse files
author
Ciro S. Costa
committed
Implements range cut in editor
1 parent 6c7d4d1 commit 4cf1642

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

editor/cut.go

+35
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
// It assumes that the provided `cast` is entirely valid (see
1212
// `github.com/cirocosta/asciinema-edit/cast#Validate`).
1313
//
14+
// If `from == to`:
15+
// the exact timestamp is removed.
16+
//
17+
// If `from < to`:
18+
// all timestamps from `from` to `to` are removed (both included).
19+
//
1420
// 1. search a time that is close to `from`; then
1521
// 2. search a time that is close to `to`; then
1622
// 3. remove all in between; then
@@ -33,5 +39,34 @@ func Cut(c *cast.Cast, from, to float64) (err error) {
3339
return
3440
}
3541

42+
var (
43+
fromIdx = -1
44+
toIdx = -1
45+
)
46+
47+
for idx, ev := range c.EventStream {
48+
if ev.Time == from {
49+
fromIdx = idx
50+
}
51+
52+
if ev.Time == to {
53+
toIdx = idx
54+
}
55+
}
56+
57+
if fromIdx == -1 {
58+
err = errors.Errorf("couldn't find initial frame")
59+
return
60+
}
61+
62+
if toIdx == -1 {
63+
err = errors.Errorf("couldn't find final frame")
64+
return
65+
}
66+
67+
c.EventStream = append(
68+
c.EventStream[:fromIdx],
69+
c.EventStream[toIdx+1:]...)
70+
3671
return
3772
}

editor/cut_test.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ var _ = Describe("Cut", func() {
4848

4949
Context("with non-empty event stream", func() {
5050
var (
51-
err error
52-
data *cast.Cast
51+
err error
52+
data *cast.Cast
53+
initialNumberOfEvents int
5354
)
5455

5556
BeforeEach(func() {
@@ -61,11 +62,37 @@ var _ = Describe("Cut", func() {
6162
event2,
6263
},
6364
}
65+
66+
initialNumberOfEvents = len(data.EventStream)
6467
})
6568

6669
It("fails if `from` > `to`", func() {
6770
err = editor.Cut(data, 3, 2)
6871
Expect(err).ToNot(Succeed())
6972
})
73+
74+
It("cuts single frame if `from` == `to`", func() {
75+
err = editor.Cut(data, 1.2, 1.2)
76+
Expect(err).To(Succeed())
77+
78+
Expect(data.EventStream).To(ContainElement(event1))
79+
Expect(data.EventStream).ToNot(ContainElement(event1_2))
80+
Expect(data.EventStream).To(ContainElement(event1_6))
81+
Expect(data.EventStream).To(ContainElement(event2))
82+
83+
Expect(len(data.EventStream)).To(Equal(initialNumberOfEvents - 1))
84+
})
85+
86+
It("cuts frames in range", func() {
87+
err = editor.Cut(data, 1.2, 2)
88+
Expect(err).To(Succeed())
89+
90+
Expect(data.EventStream).To(ContainElement(event1))
91+
Expect(data.EventStream).ToNot(ContainElement(event1_2))
92+
Expect(data.EventStream).ToNot(ContainElement(event1_6))
93+
Expect(data.EventStream).ToNot(ContainElement(event2))
94+
95+
Expect(len(data.EventStream)).To(Equal(initialNumberOfEvents - 3))
96+
})
7097
})
7198
})

0 commit comments

Comments
 (0)