Skip to content

Commit 7256f27

Browse files
committed
chat_offset -> chat_log
1 parent 3cb999f commit 7256f27

2 files changed

Lines changed: 41 additions & 28 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
*.txt
44
markut
55
markut.exe
6+
TAGS
7+
.build/

‎markut.go‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ type EvalContext struct {
150150
inputPathLog []Token
151151
outputPath string
152152
chatLog []ChatMessageGroup
153+
chatOffset Millis
154+
chatsLoaded int
153155
chunks []Chunk
156+
chunksDefinedForCurrentInput int
154157
chapters []Chapter
155158
cuts []Cut
156159

@@ -333,7 +336,7 @@ func (context EvalContext) containsChunkWithName(filePath string) bool {
333336

334337
// IMPORTANT! chatLog is assumed to be sorted by TimeOffset.
335338
func sliceChatLog(chatLog []ChatMessageGroup, start, end Millis) []ChatMessageGroup {
336-
// TODO: use Binary Search for a speed up on big chat logs
339+
// TODO: use Binary Search for a speed up on big chat logs when the performance becomes a problem
337340
lower := 0
338341
for lower < len(chatLog) && chatLog[lower].TimeOffset < start {
339342
lower += 1
@@ -349,6 +352,7 @@ func sliceChatLog(chatLog []ChatMessageGroup, start, end Millis) []ChatMessageGr
349352
}
350353

351354
// IMPORTANT! chatLog is assumed to be sorted by TimeOffset.
355+
// Groups messages with equal timestamps
352356
func compressChatLog(chatLog []ChatMessageGroup) []ChatMessageGroup {
353357
result := []ChatMessageGroup{}
354358
for i := range chatLog {
@@ -1413,20 +1417,26 @@ func main() {
14131417
fmt.Printf("%s: ERROR: could not load the chat logs: %s\n", path.Loc, err)
14141418
return false
14151419
}
1420+
context.chatOffset = 0
1421+
context.chatsLoaded += 1
14161422
return true
14171423
},
14181424
},
1419-
"chat_offset": {
1420-
Description: "Offsets the timestamps of the currently loaded chat log$SPOILER$ by removing all the messages between `start` and `end` Timestamps",
1425+
"chat_pin": {
1426+
Description: "Pins the chat timestamp to the specific video timestamp$SPOILER$ for the currently loaded with the `chat` command chat log.",
14211427
Category: "Chat",
1422-
Signature: "<start:Timestamp> <end:Timestamp> --",
1428+
Signature: "<video:Timestamp> <chat:Timestamp> --",
14231429
Run: func(context *EvalContext, command string, token Token) bool {
1424-
// // TODO: this check does not make any sense when there are several chat commands
1425-
// // But I still want to have some sort of sanity check for chat_offsets
1426-
// if len(context.chunks) > 0 {
1427-
// fmt.Printf("%s: ERROR: chat offset should be applied after `chat` commands but before any `chunks` commands. This is due to `chunk` commands making copies of the chat slices that are not affected by the consequent chat offsets\n", token.Loc);
1428-
// return false;
1429-
// }
1430+
if context.chatsLoaded == 0 {
1431+
fmt.Printf("%s: ERROR: chat pins should be applied after a `chat` command. Otherwise they are applied to nothing.\n", token.Loc)
1432+
return false
1433+
}
1434+
if context.chunksDefinedForCurrentInput > 0 {
1435+
fmt.Printf("%s: ERROR: chat pins should be applied before any `chunk` commands within a single `input`. This is due to `chunk` commands making copies of the chat slices that are not affected by the consequent chat pins\n", token.Loc);
1436+
return false
1437+
}
1438+
// TODO: it's important that chat offsets are applied in the sorted order and do not overlap.
1439+
// Maybe the language should enforce that somehow.
14301440

14311441
args, err := context.typeCheckArgs(token.Loc, TokenTimestamp, TokenTimestamp)
14321442
if err != nil {
@@ -1435,34 +1445,33 @@ func main() {
14351445
return false
14361446
}
14371447

1438-
start := args[1]
1439-
end := args[0]
1448+
video := args[1]
1449+
chat := args[0]
14401450

1441-
if start.Timestamp < 0 {
1442-
fmt.Printf("%s: ERROR: the start of the chat offset is negative %s\n", start.Loc, millisToTs(start.Timestamp))
1451+
if video.Timestamp < 0 {
1452+
fmt.Printf("%s: ERROR: the video timestamp of the chat pin is negative %s\n", video.Loc, millisToTs(video.Timestamp))
14431453
return false
14441454
}
14451455

1446-
if end.Timestamp < 0 {
1447-
fmt.Printf("%s: ERROR: the end of the chat offset is negative %s\n", end.Loc, millisToTs(end.Timestamp))
1448-
return false
1449-
}
1450-
1451-
if start.Timestamp > end.Timestamp {
1452-
fmt.Printf("%s: ERROR: the end of the chat offset %s is earlier than its start %s\n", end.Loc, millisToTs(end.Timestamp), millisToTs(start.Timestamp))
1453-
fmt.Printf("%s: NOTE: the start is located here\n", start.Loc)
1456+
if chat.Timestamp < 0 {
1457+
fmt.Printf("%s: ERROR: the chat timestamp of the chat pin is negative %s\n", chat.Loc, millisToTs(chat.Timestamp))
14541458
return false
14551459
}
14561460

14571461
chatLen := len(context.chatLog)
14581462
if chatLen > 0 {
1459-
last := context.chatLog[chatLen-1].TimeOffset
1460-
before := sliceChatLog(context.chatLog, 0, start.Timestamp)
1461-
after := sliceChatLog(context.chatLog, end.Timestamp, last)
1462-
delta := end.Timestamp - start.Timestamp
1463+
last := context.chatLog[chatLen-1].TimeOffset
1464+
video := video.Timestamp
1465+
chat := chat.Timestamp - context.chatOffset
1466+
// TODO: I'm not sure what's the best way to handle the case when `chat <= video`.
1467+
// Do we just cut out the messages between `chat` and `video` or not?
1468+
delta := chat - video
1469+
before := sliceChatLog(context.chatLog, 0, video)
1470+
after := sliceChatLog(context.chatLog, chat, last)
14631471
for i := range after {
1464-
after[i].TimeOffset -= delta
1472+
after[i].TimeOffset = after[i].TimeOffset - delta
14651473
}
1474+
context.chatOffset += delta
14661475
context.chatLog = append(before, after...)
14671476
}
14681477

@@ -1475,6 +1484,7 @@ func main() {
14751484
Signature: "--",
14761485
Run: func(context *EvalContext, command string, token Token) bool {
14771486
context.chatLog = []ChatMessageGroup{}
1487+
context.chatOffset = 0
14781488
return true
14791489
},
14801490
},
@@ -1535,8 +1545,8 @@ func main() {
15351545
}
15361546

15371547
context.chapOffset += chunk.End - chunk.Start
1538-
15391548
context.chapStack = []Chapter{}
1549+
context.chunksDefinedForCurrentInput += 1
15401550
return true
15411551
},
15421552
},
@@ -1774,6 +1784,7 @@ func main() {
17741784
}
17751785
context.inputPath = string(path.Text)
17761786
context.inputPathLog = append(context.inputPathLog, path)
1787+
context.chunksDefinedForCurrentInput = 0
17771788
return true
17781789
},
17791790
},

0 commit comments

Comments
 (0)