Skip to content

Commit

Permalink
Merge pull request #16 from romancitodev/refactor/core
Browse files Browse the repository at this point in the history
feat: 💄 refactored totally UI and functionality
  • Loading branch information
romancitodev authored Jul 25, 2024
2 parents daafa1b + 3a47abc commit bdb1133
Show file tree
Hide file tree
Showing 17 changed files with 505 additions and 351 deletions.
117 changes: 50 additions & 67 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ clap_derive = { version = "4.0.0-rc.1" }
colored = "2.1.0"
directories = "5.0.1"
env_logger = "0.11.3"
inquire = "0.7.1"
fuzzy-matcher = "0.3.7"
log = "0.4.21"
merge2 = "0.3.0"
promptuity = "0.0.5"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["rt", "macros", "sync", "rt-multi-thread"] }
toml = "0.8.8"
Expand Down
3 changes: 3 additions & 0 deletions sc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ description = "<replace>"
name = "handler"
description = "<replace>"

[[scopes]]
name = "crab"

[git]
skip_preview = false
skip_emojis = false
4 changes: 3 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,7 @@ pub fn get_config() -> SimpleCommitsConfig {
}

pub fn start_logging() {
env_logger::init();
env_logger::builder()
// .filter_level(log::LevelFilter::Trace)
.init();
}
59 changes: 0 additions & 59 deletions src/git/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ use colored::Colorize;

use crate::tui::State;

pub trait Builder<T> {
fn build(self) -> T;
}

#[derive(Clone)]
pub struct Commit {
pub _type: String,
Expand All @@ -16,61 +12,6 @@ pub struct Commit {

pub struct ColoredCommit(Commit);

#[derive(Default)]
pub struct CommitBuilder {
_type: Option<String>,
emoji: Option<String>,
scope: Option<String>,
msg: Option<String>,
}

impl CommitBuilder {
pub fn set_type(self, _type: String) -> CommitBuilder {
Self {
_type: Some(_type),
..self
}
}
pub fn set_emoji(self, emoji: String) -> CommitBuilder {
Self {
emoji: Some(emoji),
..self
}
}
pub fn set_scope(self, scope: String) -> CommitBuilder {
Self {
scope: Some(scope),
..self
}
}
pub fn set_msg(self, msg: String) -> CommitBuilder {
Self {
msg: Some(msg),
..self
}
}
}

impl Builder<Commit> for CommitBuilder {
fn build(self) -> Commit {
let Self {
_type: Some(_type),
emoji,
scope,
msg: Some(msg),
} = self
else {
panic!("Cannot build because the type or msg of the commmit wasn't assigned")
};
Commit {
_type,
emoji,
scope,
msg,
}
}
}

impl std::fmt::Display for Commit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
Expand Down
30 changes: 13 additions & 17 deletions src/tui/config.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
use inquire::ui::{Attributes, Color, RenderConfig, StyleSheet, Styled};
use promptuity::themes::FancyTheme;
use promptuity::{Promptuity, Term, Terminal, Theme};

/// Generates the config for the TUI
///
/// Returns a [`RenderConfig`] from the inquire TUI.
pub fn generate_tui_config<'config>() -> RenderConfig<'config> {
let prefix = Styled::new("").with_fg(Color::White);
let selected = Styled::new("►").with_fg(Color::DarkGrey);
let skipped = Styled::new("*skipped*").with_fg(Color::DarkGrey);
let option = StyleSheet::empty().with_fg(Color::DarkGrey);
let selected_option = StyleSheet::empty().with_fg(Color::Grey);
let msg = StyleSheet::empty()
.with_fg(Color::DarkGrey)
.with_attr(Attributes::BOLD);
RenderConfig::default_colored()
.with_prompt_prefix(prefix)
.with_highlighted_option_prefix(selected)
.with_selected_option(Some(selected_option))
.with_option(option)
.with_canceled_prompt_indicator(skipped)
.with_help_message(msg)
pub fn generate_prompt<'a, W>(
term: &'a mut dyn Terminal<W>,
theme: &'a mut dyn Theme<W>,
) -> Promptuity<'a, W>
where
W: std::io::Write,
{
Promptuity::new(term, theme)
}
pub fn generate_config() -> (Term<std::io::Stderr>, FancyTheme) {
(Term::default(), FancyTheme::default())
}
33 changes: 4 additions & 29 deletions src/tui/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
use std::error::Error;

use inquire::list_option::ListOption;
use inquire::validator::Validation;

type ValidationError = Box<dyn Error + Send + Sync>;

use crate::gen::Emoji;

use crate::tui::structs::Commit;

pub fn format_emojis(list: ListOption<&Emoji>) -> String {
let Emoji { emoji, .. } = list.value;
let correct = '\u{2705}';
format!("{emoji} | {correct}")
}

pub fn format_commits(list: ListOption<&Commit<'_>>) -> String {
let Commit { label, .. } = list.value;
let correct = '\u{2705}';
format!("{label} | {correct}")
}

pub fn valid_length(text: &str) -> Result<Validation, ValidationError> {
if !text.is_empty() {
Ok(Validation::Valid)
pub fn valid_length(text: &str, min: usize, msg: &str) -> Result<(), String> {
if text.len() > min {
Ok(())
} else {
Ok(Validation::Invalid(
"Commit must contain 1 letter at least".into(),
))
Err(msg.to_owned())
}
}
Loading

0 comments on commit bdb1133

Please sign in to comment.