-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepend_previous_n_lines_to_every_item.go
36 lines (32 loc) · 1.2 KB
/
prepend_previous_n_lines_to_every_item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"flag"
//"fmt"
"github.com/Des-Nerger/go-astisub"
"github.com/asticode/go-astilog"
)
func init() {astilog.SetLogger(astilog.New(astilog.Configuration{Verbose: true}))}
func max(a, b int) int {if a > b {return a}; return b}
func main() {
var (n int; err error; removeItemOwnLines bool)
separatorLine := func() astisub.Line {
flag.IntVar(&n, "n", 4, "")
var separator string; flag.StringVar(&separator, "separator", "|", "")
flag.BoolVar(&removeItemOwnLines, "removeItemOwnLines", false, "")
flag.Parse()
return astisub.Line{Items: []astisub.LineItem{{Text: separator}}}
} ()
panicCheck := func() {if err != nil {panic(err)}}
subs, err := astisub.OpenFile(flag.Arg(0)); panicCheck()
linesCount := 0; for _, item := range subs.Items {linesCount += len(item.Lines)}
lines := make([]astisub.Line, 0, linesCount)
for _, item := range subs.Items {
itemLines := item.Lines
item.Lines = append( append(lines[max(0,len(lines)-n):len(lines):len(lines)], separatorLine),
func() []astisub.Line {if removeItemOwnLines {return nil}; return itemLines} ()...,
)
lines = append(lines, itemLines...)
}
if len(lines) != cap(lines) {panic(nil)}
err = subs.Write(flag.Arg(1)); panicCheck()
}