Skip to content

Commit

Permalink
feature: add separate file for isolated type mode #163
Browse files Browse the repository at this point in the history
  • Loading branch information
jitwxs committed Feb 26, 2023
1 parent 8f69632 commit 5332529
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 34 deletions.
4 changes: 2 additions & 2 deletions MusicLyricApp/Bean/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static string GetContent(TypeEnum typeEnum)
list.Add("当毫秒的占位符为 S 或 SS 时,『毫秒截位规则』配置生效");
break;
case TypeEnum.OUTPUT_SETTING:
list.Add("您可自行调整『输出文件名』配置,系统预设的元变量有:");
list.Add("您可自行调整『保存文件名』配置,系统预设的元变量有:");
list.Add("${id} -> 歌曲 ID");
list.Add("${index} -> 歌曲位于搜索结果中的索引序号");
list.Add("${name} -> 歌曲名");
Expand All @@ -76,7 +76,7 @@ public static string GetContent(TypeEnum typeEnum)
list.Add("-----");
list.Add("您可自行决定输出哪些歌词类型,通过勾选复选框进行启用和关闭");
list.Add("拖拽最左侧的箭头可以调整输出的顺序");
list.Add("罗马音功能需要安装罗马音插件非原始译文类型需要指定翻译 API");
list.Add("罗马音功能需要安装罗马音插件非原始译文类型需要指定翻译 API");
break;
case TypeEnum.DEFAULT:
default:
Expand Down
5 changes: 5 additions & 0 deletions MusicLyricApp/Bean/SettingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public class ConfigBean
/// </summary>
public bool IgnorePureMusicInSave = true;

/// <summary>
/// 对于 "独立" 歌词格式,保存在不同的文件中
/// </summary>
public bool SeparateFileForIsolated = false;

/// <summary>
/// 输出文件名格式
/// </summary>
Expand Down
49 changes: 37 additions & 12 deletions MusicLyricApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MusicLyricApp.Api.Music;
using MusicLyricApp.Bean;
Expand Down Expand Up @@ -574,11 +575,7 @@ private async void SingleSave()

try
{
using (var sw = new StreamWriter(saveDialog.FileName, false, GlobalUtils.GetEncoding(_globalSearchInfo.SettingBean.Param.Encoding)))
{
await sw.WriteAsync(await LyricUtils.GetOutputContent(saveVo.LyricVo, _globalSearchInfo));
await sw.FlushAsync();
}
await WriteToFile(saveDialog.FileName, saveVo.LyricVo);

MessageBox.Show(string.Format(ErrorMsg.SAVE_COMPLETE, 1, 0), "提示");
}
Expand Down Expand Up @@ -626,12 +623,9 @@ private async void BatchSave()
}

var path = filePath + '/' + GlobalUtils.GetOutputName(saveVo, _globalSearchInfo.SettingBean.Config.OutputFileNameFormat) + fileSuffix;
using(var sw = new StreamWriter(path, false, GlobalUtils.GetEncoding(_globalSearchInfo.SettingBean.Param.Encoding)))
{
await sw.WriteAsync(await LyricUtils.GetOutputContent(lyricVo, _globalSearchInfo));
await sw.FlushAsync();
success.Add(item.Key);
}

await WriteToFile(path, lyricVo);
success.Add(item.Key);
}

MessageBox.Show(string.Format(ErrorMsg.SAVE_COMPLETE, success.Count, skipCount), "提示");
Expand All @@ -653,6 +647,37 @@ private async void BatchSave()
UpdateLrcTextBox(log.ToString());
}

private async Task WriteToFile(string path, LyricVo lyricVo)
{
var encoding = GlobalUtils.GetEncoding(_globalSearchInfo.SettingBean.Param.Encoding);

var res = await LyricUtils.GetOutputContent(lyricVo, _globalSearchInfo);

if (res.Count == 1)
{
using (var sw = new StreamWriter(path, false, encoding))
{
await sw.WriteAsync(res[0]);
await sw.FlushAsync();
}
}
else
{
var doxIndex = path.LastIndexOf(".", StringComparison.Ordinal);
var suffix = path.Substring(doxIndex);
path = path.Substring(0, doxIndex);

for (var i = 0; i < res.Count; i++)
{
using (var sw = new StreamWriter(path + " - " + i + suffix, false, encoding))
{
await sw.WriteAsync(res[i]);
await sw.FlushAsync();
}
}
}
}

/**
* 保存按钮点击事件
*/
Expand Down Expand Up @@ -695,7 +720,7 @@ private async void UpdateLrcTextBox(string replace)
}
else
{
Console_TextBox.Text = await LyricUtils.GetOutputContent(lyricVo, _globalSearchInfo);
Console_TextBox.Text = GlobalUtils.MergeStr(await LyricUtils.GetOutputContent(lyricVo, _globalSearchInfo));
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions MusicLyricApp/SettingForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions MusicLyricApp/SettingForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private void Close_Btn_Click(object sender, EventArgs e)

// 输出设置
_settingBean.Config.IgnorePureMusicInSave = IgnorePureMusicInSave_CheckBox.Checked;
_settingBean.Config.SeparateFileForIsolated = SeparateFileForIsolated_CheckBox.Checked;
_settingBean.Config.OutputFileNameFormat = OutputName_TextBox.Text;

// 应用设置
Expand Down
5 changes: 5 additions & 0 deletions MusicLyricApp/Utils/GlobalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ public static int toInt(string str, int defaultValue)
return result;
}

public static string MergeStr(IEnumerable<string> strList)
{
return string.Join(Environment.NewLine, strList);
}

public static List<T> GetEnumList<T>() where T : Enum
{
return Enum.GetValues(typeof(T)).OfType<T>().ToList();
Expand Down
60 changes: 42 additions & 18 deletions MusicLyricApp/Utils/LyricUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,38 @@ public abstract class LyricUtils
/// <param name="lyricVo"></param>
/// <param name="searchInfo"></param>
/// <returns></returns>
public static async Task<string> GetOutputContent(LyricVo lyricVo, SearchInfo searchInfo)
public static async Task<List<string>> GetOutputContent(LyricVo lyricVo, SearchInfo searchInfo)
{
var param = searchInfo.SettingBean.Param;

var dotType = param.DotType;
var timestampFormat = param.OutputFileFormat == OutputFormatEnum.SRT ? param.SrtTimestampFormat : param.LrcTimestampFormat;

var voList = await FormatLyric(lyricVo.Lyric, lyricVo.TranslateLyric, searchInfo);
var voListList = await FormatLyric(lyricVo.Lyric, lyricVo.TranslateLyric, searchInfo);

if (searchInfo.SettingBean.Param.EnableVerbatimLyric)
{
voList = FormatSubLineLyric(voList, timestampFormat, dotType);
for (var i = 0; i < voListList.Count; i++)
{
voListList[i] = FormatSubLineLyric(voListList[i], timestampFormat, dotType);
}
}

var res = new List<string>();

if (param.OutputFileFormat == OutputFormatEnum.SRT)
foreach (var voList in voListList)
{
return SrtUtils.LrcToSrt(voList, timestampFormat, dotType, lyricVo.Duration);
}
else
{
return string.Join(Environment.NewLine, from o in voList select o.Print(timestampFormat, dotType));
if (param.OutputFileFormat == OutputFormatEnum.SRT)
{
res.Add(SrtUtils.LrcToSrt(voList, timestampFormat, dotType, lyricVo.Duration));
}
else
{
res.Add(string.Join(Environment.NewLine, from o in voList select o.Print(timestampFormat, dotType)));
}
}

return res;
}

/// <summary>
Expand Down Expand Up @@ -143,17 +153,19 @@ private static List<LyricLineVo> FormatSubLineLyric(List<LyricLineVo> vos, strin
/// <param name="translateLrc">原始的译文内容</param>
/// <param name="searchInfo">处理参数</param>
/// <returns></returns>
private static async Task<List<LyricLineVo>> FormatLyric(string originLrc, string translateLrc, SearchInfo searchInfo)
private static async Task<List<List<LyricLineVo>>> FormatLyric(string originLrc, string translateLrc, SearchInfo searchInfo)
{
var outputLyricsTypes = searchInfo.SettingBean.Config.DeserializationOutputLyricsTypes();
var showLrcType = searchInfo.SettingBean.Param.ShowLrcType;
var searchSource = searchInfo.SettingBean.Param.SearchSource;
var ignoreEmptyLyric = searchInfo.SettingBean.Param.IgnoreEmptyLyric;

var res = new List<List<LyricLineVo>>();

// 未配置任何输出
if (outputLyricsTypes.Count == 0)
{
return new List<LyricLineVo>();
return res;
}

var originLyrics = SplitLrc(originLrc, searchSource, ignoreEmptyLyric);
Expand All @@ -163,7 +175,8 @@ private static async Task<List<LyricLineVo>> FormatLyric(string originLrc, strin
// 仅输出原文
if (outputLyricsTypes.Count == 1 && originLyricsOutputSortInConfig != -1)
{
return originLyrics;
res.Add(originLyrics);
return res;
}

// 原始译文歌词的空行没有意义,指定 true 不走配置
Expand All @@ -176,33 +189,44 @@ private static async Task<List<LyricLineVo>> FormatLyric(string originLrc, strin
{
lyricsComplexList.Insert(originLyricsOutputSortInConfig, originLyrics);
}

var res = new List<LyricLineVo>();

var single = new List<LyricLineVo>();
switch (showLrcType)
{
case ShowLrcTypeEnum.STAGGER:
foreach (var each in lyricsComplexList)
{
res = SortLrc(res, each, true);
single = SortLrc(single, each, true);
}
break;
case ShowLrcTypeEnum.ISOLATED:
foreach (var each in lyricsComplexList)
if (searchInfo.SettingBean.Config.SeparateFileForIsolated)
{
res.AddRange(lyricsComplexList);
}
else
{
res.AddRange(each);
foreach (var each in lyricsComplexList)
{
single.AddRange(each);
}
}
break;
case ShowLrcTypeEnum.MERGE:
foreach (var each in lyricsComplexList)
{
res = MergeLrc(res, each, searchInfo.SettingBean.Param.LrcMergeSeparator, true);
single = MergeLrc(single, each, searchInfo.SettingBean.Param.LrcMergeSeparator, true);
}
break;
default:
throw new NotSupportedException("not support showLrcType: " + showLrcType);
}

if (single.Count > 0)
{
res.Add(single);
}

return res;
}

Expand Down

0 comments on commit 5332529

Please sign in to comment.