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] {
|
switch os.Args[1] {
|
||||||
case "sync":
|
case "sync":
|
||||||
syncLyrics(os.Args[2:])
|
syncLyrics(os.Args[2:])
|
||||||
|
case "convert":
|
||||||
|
convertLyrics(os.Args[2:])
|
||||||
|
case "fmt":
|
||||||
|
fmtLyrics(os.Args[2:])
|
||||||
case "help":
|
case "help":
|
||||||
fmt.Println(USAGE)
|
fmt.Println(USAGE)
|
||||||
default:
|
default:
|
||||||
|
|
5
model.go
5
model.go
|
@ -9,6 +9,11 @@ type Lyrics struct {
|
||||||
const USAGE = `Usage: lyc-cli [command] [options]
|
const USAGE = `Usage: lyc-cli [command] [options]
|
||||||
Commands:
|
Commands:
|
||||||
sync Synchronize timeline of two lyrics files
|
sync Synchronize timeline of two lyrics files
|
||||||
|
convert Convert lyrics file to another format
|
||||||
help Show help`
|
help Show help`
|
||||||
|
|
||||||
const SYNC_USAGE = `Usage: lyc-cli sync <source> <target>`
|
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"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -49,6 +50,26 @@ func parseLyrics(filePath string) (Lyrics, error) {
|
||||||
return lyrics, nil
|
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) {
|
func syncLyrics(args []string) {
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
fmt.Println(SYNC_USAGE)
|
fmt.Println(SYNC_USAGE)
|
||||||
|
@ -110,22 +131,63 @@ func syncLyrics(args []string) {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func saveLyrics(filePath string, lyrics Lyrics) error {
|
func convertLyrics(args []string) {
|
||||||
file, err := os.Create(filePath)
|
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 {
|
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()
|
defer file.Close()
|
||||||
|
|
||||||
// Write metadata
|
for _, content := range sourceLyrics.Content {
|
||||||
for key, value := range lyrics.Metadata {
|
fmt.Fprintln(file, content)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue