Skip to content

Commit

Permalink
Fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shettysach committed Feb 1, 2024
1 parent 43ab1b3 commit 209f631
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 42 deletions.
31 changes: 31 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use leptos::{component, create_signal, view, For, IntoView, SignalGet};
use std::collections::VecDeque;

mod prompt;
use prompt::Prompt;

#[component]
pub fn Base() -> impl IntoView {
// Signals for number of prompts and history vector
let (prompts, set_prompts) = create_signal(1);
let (history, set_history) = create_signal(VecDeque::new());

let prompt_list = move || (0..prompts.get()).collect::<Vec<u32>>();

view! {
<div>
<For
each = prompt_list
key = |&prompt| prompt
children = move |_| {
view! {
<Prompt
submitter=set_prompts
updater=set_history
history=history/>
}
}
/>
</div>
}
}
13 changes: 9 additions & 4 deletions src/prompt.rs → src/base/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ pub fn Prompt(
updater: WriteSignal<VecDeque<String>>,
history: ReadSignal<VecDeque<String>>,
) -> impl IntoView {
//Output and history index signals
let (out, set_out) = create_signal(String::new());
let (history_index, set_history_index) = create_signal(0);

let input_element: NodeRef<Input> = create_node_ref();
//Form and input elements
let form_element: NodeRef<Form> = create_node_ref();
let input_element: NodeRef<Input> = create_node_ref();

//Themes
let UseColorModeReturn { mode, set_mode, .. } = use_color_mode_with_options(
UseColorModeOptions::default()
.custom_modes(vec!["catppuccin".into(), "nord".into(), "classic".into()])
Expand All @@ -39,14 +42,15 @@ pub fn Prompt(
UseCycleListOptions::default().initial_value(Some((mode, set_mode).into())),
);

//On submit
let on_submit = move |ev: SubmitEvent| {
ev.prevent_default();
let value = input_element().unwrap().value();
let next = next.clone();

spawn_local(async move {
let val = value.trim();
let val = val.split_once(' ').unwrap_or((val, " "));
let value = value.trim().replace("<", "‹").replace(">", "›");
let val = value.split_once(' ').unwrap_or((&value, " "));

match val.0 {
"clear" => {
Expand Down Expand Up @@ -76,7 +80,6 @@ pub fn Prompt(

updater.update(|hist| {
if value != "" && hist.front() != Some(&value) {
let value = value.replace("<", "‹").replace(">", "›");
hist.push_front(value);
if hist.len() > 20 {
hist.pop_back();
Expand All @@ -93,6 +96,7 @@ pub fn Prompt(
input_element().unwrap().set_inert(true);
};

// Focus on the new prompt on mount
create_effect(move |_| {
if let Some(ref_input) = input_element.get() {
let _ = ref_input.on_mount(|input| {
Expand All @@ -101,6 +105,7 @@ pub fn Prompt(
}
});

// Event listener for Up and Down arrow keys, Tab and Ctrl/Command + L
let _ = use_event_listener(input_element, keydown, move |ev: KeyboardEvent| {
let index = history_index.get_untracked();
let hist = history.get_untracked();
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub enum Bash {
You,
Echo(String),
Nothing,
Invalid,
Invalid(String),
}

impl Bash {
Expand All @@ -75,7 +75,7 @@ impl Bash {
"whoami" => Self::You,
"echo" => Self::Echo(String::from(inp1)),
"" => Self::Nothing,
_ => Self::Invalid,
_ => Self::Invalid(String::from(inp0)),
}
}

Expand All @@ -92,9 +92,9 @@ impl Bash {
Self::Edit => String::from("Nothing to change."),
Self::Power => String::from("With great power comes great responsibility."),
Self::You => String::from("Despite everything, it's still you."),
Self::Echo(s) => String::from(s).replace("<", "‹").replace(">", "›"),
Self::Echo(s) => String::from(s),
Self::Nothing => String::new(),
_ => String::from("Command not found..."),
Self::Invalid(s) => format!("{s}: command not found",),
}
}
}
Expand Down
32 changes: 3 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
use leptos::{component, create_signal, view, For, IntoView, SignalGet};
use std::collections::VecDeque;
use leptos::view;

mod prompt;
use prompt::Prompt;
mod base;
use base::Base;

fn main() {
leptos::mount_to_body(|| view! { <Base/> });
}

#[component]
fn Base() -> impl IntoView {
let (prompts, set_prompts) = create_signal(1);
let (history, set_history) = create_signal(VecDeque::new());

let prompt_list = move || (0..prompts.get()).collect::<Vec<_>>();

view! {
<div>
<For
each = prompt_list
key = |&prompt| prompt
children = move |_| {
view! {
<Prompt
submitter=set_prompts
updater=set_history
history=history/>
}
}
/>
</div>
}
}
15 changes: 12 additions & 3 deletions src/texts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ Made by <span class="rd semibold">Sachith C Shetty</span>
<a href="https://github.com/shettysach/termfolio" target="_blank" class="blu semibold">Repo</a>: github.com/shettysach/termfolio
<span class="rd semibold">APIs used -</span>
* Github API
* <a href="https://github.com/Ysn4Irix/gh-pinned-repos-api" target="_blank" class="blu semibold">Pinned repos</a> - Ysn4Irix/gh-pinned-repos-api
* <a href="https://github.com/idealclover/GitHub-Star-Counter" target="_blank" class="blu semibold">Total stars and forks</a> - idealclover/GitHub-Star-Counter"#;
* <a
href="https://docs.github.com/en/rest/about-the-rest-api"
target="_blank"
class="blu semibold">Github REST API</a>
* <a
href="https://github.com/Ysn4Irix/gh-pinned-repos-api"
target="_blank"
class="blu semibold">Pinned repos</a> - Ysn4Irix/gh-pinned-repos-api
* <a
href="https://github.com/idealclover/GitHub-Star-Counter"
target="_blank"
class="blu semibold">Total stars and forks</a> - idealclover/GitHub-Star-Counter"#;

pub const READ_JSON_ERROR: &str = r#"<span class="rd semibold">Error reading config.json</span>"#;
pub const FETCH_GITHUB_ERROR: &str =
Expand Down
6 changes: 4 additions & 2 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ form {

.inline {
display: inline;
font-weight: 500;
color: var(--green);
}

Expand Down Expand Up @@ -119,7 +120,8 @@ form {
font-size: 35px;
}

/* Links */

a {
text-decoration-thickness: 1px;
font-weight: 500;
text-decoration-thickness: 2px;
}

0 comments on commit 209f631

Please sign in to comment.