Skip to content

Commit

Permalink
Make output as html
Browse files Browse the repository at this point in the history
  • Loading branch information
alt-art committed Jul 16, 2021
1 parent 1a1741a commit 2f704d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/lorem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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("<p>");
}
for _ in 0..sentences.sample(&mut rng) {
let mut first_word = self.get_words(1);
first_word = first_word.first_to_uppper_case();
Expand All @@ -52,6 +55,10 @@ impl Lorem {
phrase = phrase.trim_matches(',').to_string();
phrase.push_str(". ");
}
if output_type == "HTML" {
phrase.pop();
phrase.push_str("</p>");
}
phrase.push_str("\n\n")
}
phrase.trim().to_string()
Expand Down
16 changes: 11 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &gtk::Application) {
Expand All @@ -25,16 +25,22 @@ fn build_ui(application: &gtk::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();
}
Expand Down
23 changes: 22 additions & 1 deletion src/resources/lorem.glade
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Author: alt-art
<property name="position">1</property>
</packing>
</child>
<child type="title">
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
Expand Down Expand Up @@ -169,6 +169,27 @@ Author: alt-art
</packing>
</child>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="output_type">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="active">1</property>
<property name="active_id">1</property>
<items>
<item id="0" translatable="yes">HTML</item>
<item id="1" translatable="yes">Text</item>
</items>
</object>
<packing>
<property name="position">3</property>
</packing>
</child>
<child type="title">
<placeholder/>
</child>
</object>
</child>
Expand Down

0 comments on commit 2f704d0

Please sign in to comment.