Skip to content

Commit

Permalink
💄 Don't wrap on mobile agents
Browse files Browse the repository at this point in the history
  • Loading branch information
malted committed Feb 11, 2024
1 parent ab3d02e commit 2925071
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 47 deletions.
51 changes: 19 additions & 32 deletions src/base/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,19 @@ pub async fn index(
"https://malted.dev/???",
"",
"",
];
let max_length = body
.iter()
.map(|s| s.replace(['🐢', '🐇'], "").len())
.max()
.unwrap_or(0);

let body = justify(false, &body, max_length);
].join("\n");

let location = if malted_state.read().battery == 0 {
let loc_in = match req_info.city {
Some(city) if !city.trim().is_empty() => format!("in {city} "),
_ => String::from("wherever you are "),
};

format!("🐢Hm. I was going to tell you where I am, but apparently my server doesn't know, or doesn't want to tell you. I hope to visit you {loc_in}soon though!🐇")
format!("🐢Hm. I was going to tell you where I am, but apparently my server doesn't know, or doesn't want to tell you. I hope to visit you {loc_in}soon though!🐇\n\n")
} else {
location_section(malted_state, &req_info)
};

let location = justify(false, &[&location, ""], max_length);

let battery_message = match malted_state.read().battery {
..=0 => format!(", but my phone is dead right now, so I won't get your call."),
b @ 1..=10 => {
Expand All @@ -83,32 +74,28 @@ pub async fn index(
),
};

let contact = justify(
false,
&[
"",
&format!("Message me @Malted on the Hack Club Slack, or email me at this domain. If you're in a pinch, call me at malted at malted dot dev{battery_message}"),
"",
],
max_length,
);

let epilogue = justify(
false,
&[
"🐢",
"-------------------",
"END OF TRANSMISSION",
"-------------------",
],
max_length,
);
let contact = format!("Message me @Malted on the Hack Club Slack, or email me at this domain. Otherwise, call me at malted at malted dot dev{battery_message}");

let epilogue = [
"🐢",
"-------------------",
"END OF TRANSMISSION",
"-------------------",
]
.join("\n");

let agent = req_info.user_agent.map(|ua| ua.to_string());

TextStream! {
let mut interval = time::interval(long_interval);
macro_rules! typewr {
($text:expr) => {
let line_max = 50;
let mut line_max = 50;
if let Some(ref ua) = agent {
if ua.to_lowercase().contains("mobile") {
line_max = i32::MAX;
}
}
let mut line_length = 0;

/* Safari does not display streamed content realtime
Expand Down
22 changes: 7 additions & 15 deletions src/base/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use rocket::{
Request,
};

#[derive(Debug, Default, FromForm)]
#[derive(Debug, Default, Clone, FromForm)]
pub struct RequesterInfo<'r> {
pub user_agent: Option<&'r str>,
pub coords: Option<(f64, f64)>,
pub city: Option<&'r str>,
pub region: Option<&'r str>,
Expand All @@ -20,11 +21,14 @@ impl<'r> FromRequest<'r> for RequesterInfo<'r> {

async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
if cfg!(debug_assertions) {
return Outcome::Success(RequesterInfo::default());
let mut info = RequesterInfo::default();
info.user_agent = request.headers().get_one("User-Agent");
return Outcome::Success(info);
}

let headers = request.headers();

let user_agent = headers.get_one("User-Agent");
let lat: Option<f64> = headers
.get_one("Cf-Iplatitude")
.and_then(|x| x.parse().ok());
Expand All @@ -41,6 +45,7 @@ impl<'r> FromRequest<'r> for RequesterInfo<'r> {
};

Outcome::Success(RequesterInfo {
user_agent,
coords,
city,
region,
Expand Down Expand Up @@ -110,16 +115,3 @@ pub fn location_section(
),
}
}

pub fn justify(should_justify: bool, text: &[&str], max_length: usize) -> String {
text.iter()
.map(|x| {
if should_justify {
format!("{:^width$}", x, width = max_length)
} else {
x.to_string()
}
})
.collect::<Vec<String>>()
.join("\n")
}

0 comments on commit 2925071

Please sign in to comment.