-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from NotThatRqd/custom-data
1.0.0 update 🎉
- Loading branch information
Showing
6 changed files
with
460 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,129 @@ | ||
# Btnify | ||
Hosts a website with buttons for you so you can focus on what matters! | ||
<div align="center"> | ||
<pre> | ||
██████╗ ████████╗███╗ ██╗██╗███████╗██╗ ██╗ | ||
██╔══██╗╚══██╔══╝████╗ ██║██║██╔════╝╚██╗ ██╔╝ | ||
██████╔╝ ██║ ██╔██╗ ██║██║█████╗ ╚████╔╝ | ||
██╔══██╗ ██║ ██║╚██╗██║██║██╔══╝ ╚██╔╝ | ||
██████╔╝ ██║ ██║ ╚████║██║██║ ██║ | ||
╚═════╝ ╚═╝ ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝ | ||
|
||
Btnify is a small libary that lets you host a website with some buttons that will call a function or closure | ||
when clicked. Under the hood, Btnify uses [Axum](https://github.com/tokio-rs/axum). This library is, I must admit, | ||
rather crude, but it works and it's open source! Please leave a pull request with any improvments you have :) | ||
I would appriciate it very much. | ||
--------------------------------------------------- | ||
rust library to simplify allowing user input over the web | ||
</pre> | ||
|
||
# Examples | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/NotThatRqd/btnify/rust.yml) | ||
[![docs.rs](https://img.shields.io/docsrs/btnify)](https://docs.rs/btnify) | ||
|
||
</div> | ||
|
||
> Hosts a website with buttons for you so you can focus on what matters! | ||
Btnify is a small library that lets you host a website with some buttons that will call a function or closure | ||
when clicked. Under the hood, Btnify uses [Axum](https://github.com/tokio-rs/axum). This library is pretty simple, | ||
but it works, and it's open source! Please leave a pull request with any improvements you have :) I would appreciate it | ||
very much. | ||
|
||
## Installation | ||
|
||
Run `cargo add btnify` | ||
|
||
or | ||
|
||
Add `btnify = "1.0.0"` to your `Cargo.toml` | ||
|
||
## How to use | ||
|
||
[Docs are here](https://docs.rs/btnify) | ||
|
||
## Examples | ||
|
||
Hello World | ||
|
||
```rust | ||
use btnify::{ | ||
bind_server, | ||
button::{Button, ButtonResponse} | ||
}; | ||
use btnify::button::{Button, ButtonResponse, ExtraResponse}; | ||
|
||
fn greet_handler(_: &()) -> ButtonResponse { | ||
ButtonResponse::from("Hello world!") | ||
fn greet_handler() -> ButtonResponse { | ||
ButtonResponse::from("hello world!") | ||
} | ||
|
||
let greet_button = Button::new("Greet", greet_handler); | ||
let greet_button: Button<()> = Button::create_basic_button("Greet!", Box::new(greet_handler)); | ||
``` | ||
|
||
Hello World 2.0 | ||
|
||
```rust | ||
use btnify::button::{Button, ButtonResponse}; | ||
|
||
let buttons = vec![greet_button]; | ||
fn better_greet_handler(responses: Vec<Option<String>>) -> ButtonResponse { | ||
// responses is guaranteed to be the same length as the number of extra prompts | ||
// specified when creating a button | ||
let name = &responses[0]; | ||
match name { | ||
Some(name) => format!("Hello, {name}").into(), | ||
None => format!("You didn't provide a name! :(").into() | ||
} | ||
} | ||
|
||
// Notice: bind_server is async and you must await it | ||
bind_server(&"0.0.0.0:3000".parse().unwrap(), buttons, ()) | ||
.await | ||
.unwrap(); | ||
let better_greet_button: Button<()> = Button::create_button_with_prompts( | ||
"Greet 2.0", | ||
Box::new(better_greet_handler), | ||
vec!["What's your name?".to_string()] | ||
); | ||
``` | ||
|
||
Counter App | ||
|
||
```rust | ||
use std::sync::Mutex; | ||
use btnify::{ | ||
bind_server, | ||
button::{Button, ButtonResponse} | ||
}; | ||
use btnify::bind_server; | ||
use btnify::button::{Button, ButtonResponse, ExtraResponse}; | ||
|
||
struct Counter { | ||
// must use mutex to be thread-safe | ||
count: Mutex<i32> | ||
} | ||
|
||
impl Counter { | ||
fn new() -> Counter { | ||
Counter { | ||
count: Mutex::new(0) | ||
} | ||
} | ||
} | ||
|
||
fn count_handler(state: &Counter) -> ButtonResponse { | ||
let mut count = state.count.lock().unwrap(); | ||
*count += 1; | ||
format!("The count now is: {count}").into() | ||
let count = state.count.lock().unwrap(); | ||
format!("The count is: {count}").into() | ||
} | ||
|
||
let count_button = Button::new("Count", count_handler); | ||
fn plus_handler(counter_struct: &Counter, responses: Vec<Option<String>>) -> ButtonResponse { | ||
match &responses[0] { | ||
Some(response_str) => { | ||
if let Ok(amount) = response_str.parse::<i32>() { | ||
let mut count = counter_struct.count.lock().unwrap(); | ||
*count += amount; | ||
format!("The count now is: {}", *count).into() | ||
} else { | ||
"You did not provide a number.".into() | ||
} | ||
} | ||
None => "You didn't provide any input.".into(), | ||
} | ||
} | ||
|
||
let count_button = Button::create_button_with_state("Counter", Box::new(count_handler)); | ||
|
||
let plus_button = Button::create_button_with_state_and_prompts( | ||
"add to counter", | ||
Box::new(plus_handler), | ||
vec!["How much do you want to add?".to_string()] | ||
); | ||
|
||
let buttons = vec![count_button]; | ||
let buttons = [count_button, plus_button]; | ||
|
||
// Notice: bind_server is async and you must await it | ||
bind_server(&"0.0.0.0:3000".parse().unwrap(), buttons, ()) | ||
.await | ||
.unwrap(); | ||
// uncomment to run server on localhost:3000 | ||
// bind_server(&"0.0.0.0:3000".parse().unwrap(), buttons, Counter::new()) | ||
// .await | ||
// .unwrap(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<div align="center"> | ||
<pre> | ||
██████╗ ████████╗███╗ ██╗██╗███████╗██╗ ██╗ | ||
██╔══██╗╚══██╔══╝████╗ ██║██║██╔════╝╚██╗ ██╔╝ | ||
██████╔╝ ██║ ██╔██╗ ██║██║█████╗ ╚████╔╝ | ||
██╔══██╗ ██║ ██║╚██╗██║██║██╔══╝ ╚██╔╝ | ||
██████╔╝ ██║ ██║ ╚████║██║██║ ██║ | ||
╚═════╝ ╚═╝ ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝ | ||
|
||
--------------------------------------------------- | ||
rust library to simplify allowing user input over the web | ||
</pre> | ||
|
||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/NotThatRqd/btnify/rust.yml) | ||
[![docs.rs](https://img.shields.io/docsrs/btnify)](https://docs.rs/btnify) | ||
|
||
</div> | ||
|
||
> Hosts a website with buttons for you so you can focus on what matters! | ||
|
||
Btnify is a small library that lets you host a website with some buttons that will call a function or closure | ||
when clicked. Under the hood, Btnify uses [Axum](https://crates.io/crates/axum). This library is pretty simple, | ||
but it works, and it's open source! Please leave a pull request with any improvements you have :) I would appreciate it | ||
very much. | ||
|
||
## Installation | ||
|
||
Run `cargo add btnify` | ||
|
||
or | ||
|
||
Add `btnify = "{{version}}"` to your `Cargo.toml` | ||
|
||
## How to use | ||
|
||
[Docs are here](https://docs.rs/btnify) | ||
|
||
{{readme}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.