You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Shortens a string to a specified line count and adds "[...]" to the
77
-
/// end of the shortened string.
78
-
///
79
-
/// returns tuple with the String and a boolean whether is was truncated
80
-
pub(crate)fntruncate_by_lines(
81
-
buf:String,
82
-
max_lines:usize,
83
-
max_line_len:usize,
84
-
) -> (String,bool){
85
-
letmut lines = 0;
86
-
letmut line_chars = 0;
87
-
letmut break_point:Option<usize> = None;
88
-
89
-
for(index, char)in buf.char_indices(){
90
-
if char == '\n'{
91
-
line_chars = 0;
92
-
lines += 1;
93
-
}else{
94
-
line_chars += 1;
95
-
if line_chars > max_line_len {
96
-
line_chars = 1;
97
-
lines += 1;
98
-
}
99
-
}
100
-
if lines == max_lines {
101
-
break_point = Some(index);
102
-
break;
103
-
}
104
-
}
105
-
106
-
ifletSome(end_pos) = break_point {
107
-
// Text has too many lines and needs to be truncated.
108
-
let text = {
109
-
ifletSome(buffer) = buf.get(..end_pos){
110
-
ifletSome(index) = buffer.rfind([' ','\n']){
111
-
buf.get(..=index)
112
-
}else{
113
-
buf.get(..end_pos)
114
-
}
115
-
}else{
116
-
None
117
-
}
118
-
};
119
-
120
-
ifletSome(truncated_text) = text {
121
-
(format!("{truncated_text}{DC_ELLIPSIS}"),true)
122
-
}else{
123
-
// In case of indexing/slicing error, we return an error
124
-
// message as a preview and add HTML version. This should
125
-
// never happen.
126
-
let error_text = "[Truncation of the message failed, this is a bug in the Delta Chat core. Please report it.\nYou can still open the full text to view the original message.]";
127
-
(error_text.to_string(),true)
128
-
}
129
-
}else{
130
-
// text is unchanged
131
-
(buf,false)
132
-
}
133
-
}
134
-
135
-
/// Shortens a message text if necessary according to the configuration. Adds "[...]" to the end of
136
-
/// the shortened text.
137
-
///
138
-
/// Returns the resulting text and a bool telling whether a truncation was done.
0 commit comments