Skip to content

Commit

Permalink
Fix mensa formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Dec 5, 2024
1 parent 8cabfb2 commit 88ebea8
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions Sources/D2Commands/CAU/Mensa/MensaCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,12 @@ public class MensaCommand: StringCommand {
let canteen = try await client.canteen(for: canteenId)
do {
let meals = try await client.meals(for: canteenId)
let mealsByCategory = [String?: [Meal]](grouping: meals, by: \.category)
.map { (key: $0.key ?? "No title", value: $0.value) }
.sorted(by: ascendingComparator(comparing: \.key))
await output.append(Embed(
title: ":fork_knife_plate: Today's menu for \(canteen.name)",
fields: mealsByCategory.compactMap { (category, meals) -> Embed.Field? in
guard !meals.isEmpty else { return nil }
return Embed.Field(
name: "**+++ \(category) +++**",
value: self.format(meals: meals.sorted(by: ascendingComparator(comparing: \.name)))
fields: meals.map { meal in
Embed.Field(
name: "\(meal.name)\(meal.category.map { " [\($0)]" } ?? "")".nilIfEmpty ?? "?",
value: format(meal: meal)
)
}
))
Expand All @@ -64,16 +60,11 @@ public class MensaCommand: StringCommand {
}
}

private func format(meals: [Meal]) -> String {
meals.flatMap { meal in
[
"**\(meal.name.nilIfEmpty ?? "_no title_")**",
("\(meal.prices) \(emojisOf(attributes: meal.attributes))")
.trimmingCharacters(in: .whitespacesAndNewlines)
.nilIfEmpty
?? "_no properties_"
]
}.joined(separator: "\n")
private func format(meal: Meal) -> String {
("\(meal.prices) \(emojisOf(attributes: meal.attributes))")
.trimmingCharacters(in: .whitespacesAndNewlines)
.nilIfEmpty
?? "_no properties_"
}

private func emojisOf(attributes: Meal.Attributes) -> String {
Expand Down

0 comments on commit 88ebea8

Please sign in to comment.