summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/center.rs37
-rw-r--r--src/main.rs56
2 files changed, 93 insertions, 0 deletions
diff --git a/src/center.rs b/src/center.rs
new file mode 100644
index 0000000..173ef63
--- /dev/null
+++ b/src/center.rs
@@ -0,0 +1,37 @@
+use unicode_width::UnicodeWidthChar;
+
+pub trait Center {
+ fn center(&self, term_width: usize) -> Self;
+}
+
+impl Center for String {
+ fn center(&self, term_width: usize) -> Self {
+ let mut result = String::new();
+ let mut buffer = String::new();
+ let mut buffer_width = 0;
+
+ let max = self.chars().count() - 1;
+
+ for (i, c) in self.chars().enumerate() {
+ buffer_width += UnicodeWidthChar::width(c)
+ .unwrap();
+
+ buffer.push(c);
+
+ if i == max || buffer_width >= term_width - 12 {
+ result.push_str(&String::from(" ")
+ .repeat((term_width - buffer_width) / 2));
+ result.push_str(&buffer);
+
+ if i != max {
+ result.push('\n');
+ }
+
+ buffer.clear();
+ buffer_width = 0;
+ }
+ }
+
+ result
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..f067e2b
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,56 @@
+use rust_embed::RustEmbed;
+use rand::seq::IteratorRandom;
+use serde::Deserialize;
+use colored::*;
+
+mod center;
+use center::Center;
+
+#[derive(RustEmbed)]
+#[folder = "nihongo-benkyou/kotowaza"]
+#[include = "*.json"]
+struct KotowazaDir;
+
+#[derive(Deserialize)]
+#[allow(unused)]
+struct Kotowaza {
+ en: String,
+ ja: String,
+ kana: String,
+ week: String,
+}
+
+
+fn main() {
+ let mut rng = rand::thread_rng();
+
+ let name = KotowazaDir::iter()
+ .choose(&mut rng)
+ .unwrap();
+
+ let file = KotowazaDir::get(&name)
+ .unwrap();
+
+ let data = file.data
+ .as_ref();
+
+ let kotowaza: Kotowaza = serde_json::from_slice(data)
+ .unwrap();
+
+ let (term_width, _) = term_size::dimensions()
+ .unwrap();
+
+ print!("\n{}\n{}\n\n{}\n\n",
+ kotowaza.ja
+ .center(term_width)
+ .bold()
+ .yellow(),
+ kotowaza.kana
+ .center(term_width)
+ .dimmed()
+ .yellow(),
+ kotowaza.en
+ .center(term_width)
+ .italic()
+ .cyan());
+}