textwrap: Update to textwrap 0.13

This commit is contained in:
Sebastian Dröge 2020-12-11 12:25:47 +02:00
parent 09561686b8
commit b5796f5773
2 changed files with 24 additions and 29 deletions

View file

@ -10,7 +10,7 @@ repository = "https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs"
[dependencies] [dependencies]
glib = { git = "https://github.com/gtk-rs/gtk-rs" } glib = { git = "https://github.com/gtk-rs/gtk-rs" }
once_cell = "1.0" once_cell = "1.0"
textwrap = { version = "0.12", features = ["hyphenation"] } textwrap = { version = "0.13", features = ["hyphenation"] }
hyphenation = "0.8" hyphenation = "0.8"
[dependencies.gst] [dependencies.gst]

View file

@ -95,28 +95,23 @@ impl Default for Settings {
} }
} }
#[allow(clippy::large_enum_variant)] // FIXME: Not needed anymore after https://github.com/mgeisler/textwrap/pull/254
enum Wrapper { #[derive(Debug)]
H(textwrap::Wrapper<'static, Standard>), struct Splitter(Box<dyn textwrap::WordSplitter + Send>);
N(textwrap::Wrapper<'static, textwrap::NoHyphenation>),
impl textwrap::WordSplitter for Splitter {
fn split_points(&self, word: &str) -> Vec<usize> {
self.0.split_points(word)
}
} }
struct State { struct State {
wrapper: Option<Wrapper>, options: Option<textwrap::Options<'static, Splitter>>,
}
impl Wrapper {
fn fill(&self, s: &str) -> String {
match *self {
Wrapper::H(ref w) => w.fill(s),
Wrapper::N(ref w) => w.fill(s),
}
}
} }
impl Default for State { impl Default for State {
fn default() -> Self { fn default() -> Self {
Self { wrapper: None } Self { options: None }
} }
} }
@ -132,11 +127,11 @@ impl TextWrap {
let settings = self.settings.lock().unwrap(); let settings = self.settings.lock().unwrap();
let mut state = self.state.lock().unwrap(); let mut state = self.state.lock().unwrap();
if state.wrapper.is_some() { if state.options.is_some() {
return; return;
} }
state.wrapper = if let Some(dictionary) = &settings.dictionary { state.options = if let Some(dictionary) = &settings.dictionary {
let dict_file = match File::open(dictionary) { let dict_file = match File::open(dictionary) {
Err(err) => { Err(err) => {
gst_error!(CAT, obj: element, "Failed to open dictionary file: {}", err); gst_error!(CAT, obj: element, "Failed to open dictionary file: {}", err);
@ -159,15 +154,15 @@ impl TextWrap {
Ok(standard) => standard, Ok(standard) => standard,
}; };
Some(Wrapper::H(textwrap::Wrapper::with_splitter( Some(textwrap::Options::with_splitter(
settings.columns as usize, settings.columns as usize,
standard, Splitter(Box::new(standard)),
))) ))
} else { } else {
Some(Wrapper::N(textwrap::Wrapper::with_splitter( Some(textwrap::Options::with_splitter(
settings.columns as usize, settings.columns as usize,
textwrap::NoHyphenation, Splitter(Box::new(textwrap::NoHyphenation)),
))) ))
}; };
} }
@ -211,11 +206,11 @@ impl TextWrap {
let data = { let data = {
let state = self.state.lock().unwrap(); let state = self.state.lock().unwrap();
let wrapper = state let options = state
.wrapper .options
.as_ref() .as_ref()
.expect("We should have a wrapper by now"); .expect("We should have a wrapper by now");
wrapper.fill(data) textwrap::fill(data, options)
}; };
// If the lines property was set, we want to split the result into buffers // If the lines property was set, we want to split the result into buffers
@ -347,13 +342,13 @@ impl ObjectImpl for TextWrap {
let mut settings = self.settings.lock().unwrap(); let mut settings = self.settings.lock().unwrap();
let mut state = self.state.lock().unwrap(); let mut state = self.state.lock().unwrap();
settings.dictionary = value.get().expect("type checked upstream"); settings.dictionary = value.get().expect("type checked upstream");
state.wrapper = None; state.options = None;
} }
subclass::Property("columns", ..) => { subclass::Property("columns", ..) => {
let mut settings = self.settings.lock().unwrap(); let mut settings = self.settings.lock().unwrap();
let mut state = self.state.lock().unwrap(); let mut state = self.state.lock().unwrap();
settings.columns = value.get_some().expect("type checked upstream"); settings.columns = value.get_some().expect("type checked upstream");
state.wrapper = None; state.options = None;
} }
subclass::Property("lines", ..) => { subclass::Property("lines", ..) => {
let mut settings = self.settings.lock().unwrap(); let mut settings = self.settings.lock().unwrap();