Skip to content

Commit

Permalink
feat(request): padding query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuwn committed Jul 1, 2024
1 parent 5449d79 commit ba557de
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ easy it is to understand! It's all contained in under 300 (294) liberally newlin

## Usage

Mayu currently has seven available themes selectable using the `theme` query parameter of any `get` operation.
Mayu currently has nine available themes selectable using the `theme` query parameter of any `get` operation.

E.g., [counter.due.moe/get/@demo?theme=urushi](https://counter.due.moe/get/@demo?theme=urushi)

Expand All @@ -34,6 +34,8 @@ E.g., [counter.due.moe/get/@demo?theme=urushi](https://counter.due.moe/get/@demo
- `lain`
- `garukura`

Mayu will pad the counter number with zeroes until it reaches a length of 6 characters. You can modify this behavior by changing the `padding` query parameter of any `get` operation.

### Local

```bash
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://gleam.run/writing-gleam/gleam-toml/.

name = "mayu"
version = "0.1.11"
version = "0.1.12"
gleam = ">= 1.2.0"
description = "Moe-Counter Compatible Website Hit Counter"
licenses = ["GPL-3.0-only"]
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ <h2>Username</h2>
themeValue = themeSelect.value;
}

image.src = `https://mayu.due.moe/get/@${inputValue}?theme=${themeValue}`;
image.src = `https://mayu.due.moe/get/@${inputValue}?theme=${themeValue}&padding=6`;

setCopyCodes();
};
Expand Down
26 changes: 19 additions & 7 deletions src/request.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import database
import envoy
import gleam/int
import gleam/json
import gleam/list
import gleam/string
import gleam/string_builder
import simplifile
Expand Down Expand Up @@ -44,13 +46,23 @@ pub fn handle(request, connection) {
Ok(counter) -> {
wisp.ok()
|> wisp.set_header("Content-Type", "image/svg+xml")
|> wisp.string_body(svg.xml(
case wisp.get_query(request) {
[#("theme", theme)] -> theme
_ -> "asoul"
},
counter.num,
))
|> wisp.string_body(
svg.xml(
case list.key_find(wisp.get_query(request), "theme") {
Ok(theme) -> theme
_ -> "asoul"
},
counter.num,
case list.key_find(wisp.get_query(request), "padding") {
Ok(padding) ->
case int.parse(padding) {
Ok(n) -> n
Error(_) -> 6
}
_ -> 6
},
),
)
}
Error(_) -> wisp.unprocessable_entity()
}
Expand Down
4 changes: 2 additions & 2 deletions src/svg.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ fn images(theme, digits, width, height, svgs) {
}
}

pub fn xml(theme, number) {
pub fn xml(theme, number, padding) {
let xml =
images(
theme,
{
let assert Ok(digits) = int.digits(number, 10)
let digits_padding = 5 - list.length(digits)
let digits_padding = padding - list.length(digits)

case digits_padding {
n if n > 0 -> list.concat([list.repeat(0, digits_padding), digits])
Expand Down

0 comments on commit ba557de

Please sign in to comment.