@@ -23,14 +23,12 @@ public class Caption : INotifyPropertyChanged
2323 public string TranslatedCaption { get ; set ; } = string . Empty ;
2424
2525 public Queue < TranslationHistoryEntry > Contexts { get ; } = new ( MAX_CONTEXTS ) ;
26-
27- public IEnumerable < TranslationHistoryEntry > AwareContexts => Contexts . Reverse ( ) . Take (
28- Math . Min ( Translator . Setting . NumContexts , Contexts . Count ) ) . Reverse ( ) ;
29- public string AwareContextsCaption => GetPreviousCaption (
30- Math . Min ( Translator . Setting . NumContexts , Contexts . Count ) ) ;
3126
32- public IEnumerable < TranslationHistoryEntry > DisplayLogCards => Contexts . Reverse ( ) . Take (
33- Math . Min ( Translator . Setting . DisplaySentences , Contexts . Count ) ) ;
27+ public IEnumerable < TranslationHistoryEntry > AwareContexts => GetPreviousContexts ( Translator . Setting . NumContexts ) ;
28+ public string AwareContextsCaption => GetPreviousText ( Translator . Setting . NumContexts , TextType . Caption ) ;
29+
30+ public IEnumerable < TranslationHistoryEntry > DisplayLogCards =>
31+ GetPreviousContexts ( Translator . Setting . DisplaySentences ) . Reverse ( ) ;
3432
3533 public string DisplayOriginalCaption
3634 {
@@ -78,8 +76,9 @@ public string OverlayCurrentTranslation
7876 OnPropertyChanged ( "OverlayCurrentTranslation" ) ;
7977 }
8078 }
81- public string OverlayPreviousTranslation => GetPreviousTranslation (
82- Math . Min ( Translator . Setting . DisplaySentences , Contexts . Count ) ) ;
79+
80+ public string OverlayPreviousTranslation =>
81+ GetPreviousText ( Translator . Setting . DisplaySentences , TextType . Translation ) ;
8382
8483 private Caption ( )
8584 {
@@ -93,14 +92,16 @@ public static Caption GetInstance()
9392 return instance ;
9493 }
9594
96- public string GetPreviousCaption ( int count )
95+ public string GetPreviousText ( int count , TextType textType )
9796 {
9897 if ( count <= 0 )
9998 return string . Empty ;
10099
101100 var prev = Contexts
102101 . Reverse ( ) . Take ( count ) . Reverse ( )
103- . Select ( entry => entry . SourceText )
102+ . Select ( entry => string . CompareOrdinal ( entry . TranslatedText , "N/A" ) == 0 ||
103+ entry . TranslatedText . Contains ( "[ERROR]" ) || entry . TranslatedText . Contains ( "[WARNING]" ) ?
104+ "" : ( textType == TextType . Caption ? entry . SourceText : entry . TranslatedText ) )
104105 . Aggregate ( ( accu , cur ) =>
105106 {
106107 if ( ! string . IsNullOrEmpty ( accu ) )
@@ -114,46 +115,36 @@ public string GetPreviousCaption(int count)
114115 return accu + cur ;
115116 } ) ;
116117
118+ if ( textType == TextType . Translation )
119+ prev = RegexPatterns . NoticePrefix ( ) . Replace ( prev , "" ) ;
117120 if ( ! string . IsNullOrEmpty ( prev ) && Array . IndexOf ( TextUtil . PUNC_EOS , prev [ ^ 1 ] ) == - 1 )
118121 prev += TextUtil . isCJChar ( prev [ ^ 1 ] ) ? "。" : "." ;
119122 if ( ! string . IsNullOrEmpty ( prev ) && Encoding . UTF8 . GetByteCount ( prev [ ^ 1 ] . ToString ( ) ) < 2 )
120123 prev += " " ;
121124 return prev ;
122125 }
123126
124- public string GetPreviousTranslation ( int count )
127+ public IEnumerable < TranslationHistoryEntry > GetPreviousContexts ( int count )
125128 {
126129 if ( count <= 0 )
127- return string . Empty ;
130+ return Enumerable . Empty < TranslationHistoryEntry > ( ) ;
128131
129- var prev = Contexts
132+ return Contexts
130133 . Reverse ( ) . Take ( count ) . Reverse ( )
131- . Select ( entry => entry . TranslatedText . Contains ( "[ERROR]" ) || entry . TranslatedText . Contains ( "[WARNING]" ) ?
132- "" : entry . TranslatedText )
133- . Aggregate ( ( accu , cur ) =>
134- {
135- if ( ! string . IsNullOrEmpty ( accu ) )
136- {
137- if ( Array . IndexOf ( TextUtil . PUNC_EOS , accu [ ^ 1 ] ) == - 1 )
138- accu += TextUtil . isCJChar ( accu [ ^ 1 ] ) ? "。" : ". " ;
139- else
140- accu += TextUtil . isCJChar ( accu [ ^ 1 ] ) ? "" : " " ;
141- }
142- cur = RegexPatterns . NoticePrefix ( ) . Replace ( cur , "" ) ;
143- return accu + cur ;
144- } ) ;
145-
146- prev = RegexPatterns . NoticePrefix ( ) . Replace ( prev , "" ) ;
147- if ( ! string . IsNullOrEmpty ( prev ) && Array . IndexOf ( TextUtil . PUNC_EOS , prev [ ^ 1 ] ) == - 1 )
148- prev += TextUtil . isCJChar ( prev [ ^ 1 ] ) ? "。" : "." ;
149- if ( ! string . IsNullOrEmpty ( prev ) && Encoding . UTF8 . GetByteCount ( prev [ ^ 1 ] . ToString ( ) ) < 2 )
150- prev += " " ;
151- return prev ;
134+ . Where ( entry => string . CompareOrdinal ( entry . TranslatedText , "N/A" ) != 0 &&
135+ ! entry . TranslatedText . Contains ( "[ERROR]" ) &&
136+ ! entry . TranslatedText . Contains ( "[WARNING]" ) ) ;
152137 }
153138
154139 public void OnPropertyChanged ( [ CallerMemberName ] string propName = "" )
155140 {
156141 PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propName ) ) ;
157142 }
158143 }
144+
145+ public enum TextType
146+ {
147+ Caption ,
148+ Translation
149+ }
159150}
0 commit comments