Skip to content

Commit

Permalink
Move chat_offset and no_chat funcs to global funcs map
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 6, 2024
1 parent be186c6 commit e8104a3
Showing 1 changed file with 64 additions and 47 deletions.
111 changes: 64 additions & 47 deletions markut.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,15 @@ func compressChatLog(chatLog []ChatMessage) []ChatMessage {

type Func struct {
Description string
Signature string
Category string
Run func(context *EvalContext, command string, token Token) bool
}

var funcs = map[string]Func{
"chat": {
Description: "Load a chat log generated by https://www.twitchchatdownloader.com/ which is going to be used by the subsequent `chunk` function calls to include certain message into the subtitles generated by `markut chat` subcommand",
Description: "Load a chat log generated by https://www.twitchchatdownloader.com/ which is going to be used by the subsequent `chunk` func calls to include certain messages into the subtitles generated by the `markut chat` subcommand. There could be only one loaded chat log at a time. Repeated calls to the `chat` func replace the currently loaded chat log with another one. The already defined chunks keep the copy of the logs that were loaded at the time of their definition.",
Signature: "<path:String> %name",
Category: "chat",
Run: func(context *EvalContext, command string, token Token) bool {
args, err := context.typeCheckArgs(token.Loc, TokenString)
Expand All @@ -239,6 +241,67 @@ var funcs = map[string]Func{
return true
},
},
"chat_offset": {
Description: "Offsets the timestamps of the currently loaded chat log by removing all the messages between `start` and `end` Timestamps",
Category: "chat",
Signature: "<start:Timestamp> <end:Timestamp> %name",
Run: func(context *EvalContext, command string, token Token) bool {
// // TODO: this check does not make any sense when there are several chat commands
// if len(context.chunks) > 0 {
// 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);
// return false;
// }

args, err := context.typeCheckArgs(token.Loc, TokenTimestamp, TokenTimestamp)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}

start := args[1]
end := args[0]

if start.Timestamp < 0 {
fmt.Printf("%s: ERROR: the start of the chat offset is negative %s\n", start.Loc, millisToTs(start.Timestamp));
return false
}

if end.Timestamp < 0 {
fmt.Printf("%s: ERROR: the end of the chat offset is negative %s\n", end.Loc, millisToTs(end.Timestamp));
return false
}

if start.Timestamp > end.Timestamp {
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));
fmt.Printf("%s: NOTE: the start is located here\n", start.Loc);
return false
}

chatLen := len(context.chatLog)
if chatLen > 0 {
last := context.chatLog[chatLen-1].TimeOffset
before := sliceChatLog(context.chatLog, 0, start.Timestamp)
after := sliceChatLog(context.chatLog, end.Timestamp, last)
delta := end.Timestamp - start.Timestamp
for i := range after {
after[i].TimeOffset -= delta
}
context.chatLog = append(before, after...)
}

return true
},
},
"no_chat": {
Description: "Clears out the current loaded chat log as if nothing is loaded",
Category: "chat",
Signature: "%name",
Run: func(context *EvalContext, command string, token Token) bool {
context.chatLog = []ChatMessage{}
return true
},
},
}

// This function is compatible with the format https://www.twitchchatdownloader.com/ generates.
Expand Down Expand Up @@ -391,52 +454,6 @@ func (context *EvalContext) evalMarkutFile(path string) bool {
}
inFlag := args[0]
context.ExtraInFlags = append(context.ExtraInFlags, string(inFlag.Text))
case "chat_offset":
// // TODO: this check does not make any sense when there are several chat commands
// if len(context.chunks) > 0 {
// 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);
// return false;
// }

args, err = context.typeCheckArgs(token.Loc, TokenTimestamp, TokenTimestamp)
if err != nil {
fmt.Printf("%s: ERROR: type check failed for %s\n", token.Loc, command)
fmt.Printf("%s\n", err)
return false
}

start := args[1]
end := args[0]

if start.Timestamp < 0 {
fmt.Printf("%s: ERROR: the start of the chat offset is negative %s\n", start.Loc, millisToTs(start.Timestamp));
return false
}

if end.Timestamp < 0 {
fmt.Printf("%s: ERROR: the end of the chat offset is negative %s\n", end.Loc, millisToTs(end.Timestamp));
return false
}

if start.Timestamp > end.Timestamp {
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));
fmt.Printf("%s: NOTE: the start is located here\n", start.Loc);
return false
}

chatLen := len(context.chatLog)
if chatLen > 0 {
last := context.chatLog[chatLen-1].TimeOffset
before := sliceChatLog(context.chatLog, 0, start.Timestamp)
after := sliceChatLog(context.chatLog, end.Timestamp, last)
delta := end.Timestamp - start.Timestamp
for i := range after {
after[i].TimeOffset -= delta
}
context.chatLog = append(before, after...)
}
case "no_chat":
context.chatLog = []ChatMessage{}
case "include":
args, err = context.typeCheckArgs(token.Loc, TokenString)
if err != nil {
Expand Down

0 comments on commit e8104a3

Please sign in to comment.