From 2f704d0ff9981320bebd6a55e3ee6a3015ddf2c3 Mon Sep 17 00:00:00 2001 From: altart Date: Thu, 15 Jul 2021 22:58:29 -0300 Subject: [PATCH] Make output as html --- src/lorem/mod.rs | 9 ++++++++- src/main.rs | 16 +++++++++++----- src/resources/lorem.glade | 23 ++++++++++++++++++++++- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/lorem/mod.rs b/src/lorem/mod.rs index 8613f57..b68f7ee 100644 --- a/src/lorem/mod.rs +++ b/src/lorem/mod.rs @@ -31,7 +31,7 @@ impl Lorem { } } - pub fn get_phrase(self, min: u32, max: u32) -> String { + pub fn get_phrase(self, min: u32, max: u32, output_type: &str) -> String { let mut rng = rand::thread_rng(); let count:u32; if min >= max { @@ -43,6 +43,9 @@ impl Lorem { let quantity = Uniform::from(10..20); let mut phrase = String::new(); for _ in 0..count { + if output_type == "HTML" { + phrase.push_str("

"); + } for _ in 0..sentences.sample(&mut rng) { let mut first_word = self.get_words(1); first_word = first_word.first_to_uppper_case(); @@ -52,6 +55,10 @@ impl Lorem { phrase = phrase.trim_matches(',').to_string(); phrase.push_str(". "); } + if output_type == "HTML" { + phrase.pop(); + phrase.push_str("

"); + } phrase.push_str("\n\n") } phrase.trim().to_string() diff --git a/src/main.rs b/src/main.rs index cf96f1b..fd95285 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,14 +6,14 @@ extern crate glib; use gtk::prelude::*; use glib::clone; -use gtk::{ApplicationWindow, Builder, Label, Button, Adjustment, Clipboard}; +use gtk::{ApplicationWindow, Builder, Label, Button, Adjustment, Clipboard, ComboBoxText}; mod lorem; use lorem::Lorem; -fn generate(label: gtk::Label, min: u32, max: u32) { +fn generate(label: gtk::Label, min: u32, max: u32, output_type: &str) { let lorem = Lorem::new(); - label.set_text(&lorem.get_phrase(min, max)); + label.set_text(&lorem.get_phrase(min, max, output_type)); } fn build_ui(application: >k::Application) { @@ -25,16 +25,22 @@ fn build_ui(application: >k::Application) { let label: Label = builder.object("label").expect("Cound't get Label"); let min_adjustment: Adjustment = builder.object("min_adjust").expect("Cound't get Adjustment"); let max_adjustment: Adjustment = builder.object("max_adjust").expect("Cound't get Adjustment"); + let output_type: ComboBoxText = builder.object("output_type").expect("Cound't get ComboBoxText"); let gen_button: Button = builder.object("gen").expect("Cound't get Button"); gen_button.connect_clicked(clone!(@weak label => move |_| { - generate(label, min_adjustment.value() as u32, max_adjustment.value() as u32); + generate( + label, + min_adjustment.value() as u32, + max_adjustment.value() as u32, + output_type.active_text().unwrap().as_str() + ); })); let copy_button: Button = builder.object("copy").expect("Cound't get Button"); copy_button.connect_clicked(clone!(@weak label => move |_| { let clipboard = Clipboard::get(&gdk::SELECTION_CLIPBOARD); clipboard.set_text(&label.text()); })); - generate(label, 2, 3); + generate(label, 2, 3, "Text"); window.set_application(Some(application)); window.show_all(); } diff --git a/src/resources/lorem.glade b/src/resources/lorem.glade index 0b92757..6ef2b28 100644 --- a/src/resources/lorem.glade +++ b/src/resources/lorem.glade @@ -97,7 +97,7 @@ Author: alt-art 1 - + True False @@ -169,6 +169,27 @@ Author: alt-art + + 2 + + + + + True + False + 1 + 1 + + HTML + Text + + + + 3 + + + +