feat: convert to plain text
This commit is contained in:
parent
0c57078a8a
commit
1b1862cb3f
3 changed files with 84 additions and 13 deletions
4
main.go
4
main.go
|
@ -14,6 +14,10 @@ func main() {
|
|||
switch os.Args[1] {
|
||||
case "sync":
|
||||
syncLyrics(os.Args[2:])
|
||||
case "convert":
|
||||
convertLyrics(os.Args[2:])
|
||||
case "fmt":
|
||||
fmtLyrics(os.Args[2:])
|
||||
case "help":
|
||||
fmt.Println(USAGE)
|
||||
default:
|
||||
|
|
5
model.go
5
model.go
|
@ -9,6 +9,11 @@ type Lyrics struct {
|
|||
const USAGE = `Usage: lyc-cli [command] [options]
|
||||
Commands:
|
||||
sync Synchronize timeline of two lyrics files
|
||||
convert Convert lyrics file to another format
|
||||
help Show help`
|
||||
|
||||
const SYNC_USAGE = `Usage: lyc-cli sync <source> <target>`
|
||||
const CONVERT_USAGE = `Usage: lyc-cli convert <source> <target>
|
||||
Note:
|
||||
Target format is determined by file extension. Supported formats:
|
||||
.txt Plain text format(No meta/timeline tags)`
|
||||
|
|
88
util.go
88
util.go
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
@ -49,6 +50,26 @@ func parseLyrics(filePath string) (Lyrics, error) {
|
|||
return lyrics, nil
|
||||
}
|
||||
|
||||
func saveLyrics(filePath string, lyrics Lyrics) error {
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Write metadata
|
||||
for key, value := range lyrics.Metadata {
|
||||
fmt.Fprintf(file, "[%s: %s]\n", key, value)
|
||||
}
|
||||
|
||||
// Write timeline and content
|
||||
for i := 0; i < len(lyrics.Timeline); i++ {
|
||||
fmt.Fprintf(file, "%s %s\n", lyrics.Timeline[i], lyrics.Content[i])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func syncLyrics(args []string) {
|
||||
if len(args) < 2 {
|
||||
fmt.Println(SYNC_USAGE)
|
||||
|
@ -110,22 +131,63 @@ func syncLyrics(args []string) {
|
|||
// }
|
||||
// }
|
||||
|
||||
func saveLyrics(filePath string, lyrics Lyrics) error {
|
||||
file, err := os.Create(filePath)
|
||||
func convertLyrics(args []string) {
|
||||
if len(args) < 2 {
|
||||
fmt.Println(CONVERT_USAGE)
|
||||
return
|
||||
}
|
||||
|
||||
sourceFile := args[0]
|
||||
targetFile := args[1]
|
||||
|
||||
targetFmt := strings.TrimPrefix(filepath.Ext(targetFile), ".")
|
||||
|
||||
switch targetFmt {
|
||||
case "txt":
|
||||
lrcToTxt(sourceFile, targetFile)
|
||||
default:
|
||||
fmt.Println("Unsupported target format:", targetFmt)
|
||||
}
|
||||
}
|
||||
|
||||
func fmtLyrics(args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println("Usage: lyc-cli fmt <source>")
|
||||
return
|
||||
}
|
||||
|
||||
sourceFile := args[0]
|
||||
|
||||
sourceLyrics, err := parseLyrics(sourceFile)
|
||||
if err != nil {
|
||||
return err
|
||||
fmt.Println("Error parsing source lyrics file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// save to target (source_name_fmt.lrc)
|
||||
targetFile := strings.TrimSuffix(sourceFile, ".lrc") + "_fmt.lrc"
|
||||
err = saveLyrics(targetFile, sourceLyrics)
|
||||
if err != nil {
|
||||
fmt.Println("Error saving formatted lyrics file:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func lrcToTxt(sourceFile, targetFile string) {
|
||||
sourceLyrics, err := parseLyrics(sourceFile)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing source lyrics file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Create(targetFile)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating target file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Write metadata
|
||||
for key, value := range lyrics.Metadata {
|
||||
fmt.Fprintf(file, "[%s: %s]\n", key, value)
|
||||
for _, content := range sourceLyrics.Content {
|
||||
fmt.Fprintln(file, content)
|
||||
}
|
||||
|
||||
// Write timeline and content
|
||||
for i := 0; i < len(lyrics.Timeline); i++ {
|
||||
fmt.Fprintf(file, "%s %s\n", lyrics.Timeline[i], lyrics.Content[i])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Reference in a new issue