From 7c884576923c5688f19fad5669f2690b701ac620 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Sun, 23 Aug 2020 22:40:58 +0900 Subject: [PATCH] Support flexible card size --- dev_main.ts | 2 +- src/card.ts | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/dev_main.ts b/dev_main.ts index af842e1..3d37f01 100644 --- a/dev_main.ts +++ b/dev_main.ts @@ -19,7 +19,7 @@ for await (const req of s) { } else { req.respond( { - body: "Can not find username", + body: "Can not find a query parameter: username", status: 404, headers: new Headers({ "Content-Type": "text" }), }, diff --git a/src/card.ts b/src/card.ts index 6351162..99cd5ea 100644 --- a/src/card.ts +++ b/src/card.ts @@ -7,7 +7,7 @@ import { TotalPullRequestTrophy, TotalRepositoryTrophy, } from "./trophies.ts"; -import {UserInfo} from "./github_api_client.ts"; +import { UserInfo } from "./github_api_client.ts"; export class Card { private width = 0; @@ -15,10 +15,9 @@ export class Card { constructor( private panelSize = 110, private maxPanelWidthCount = 6, - private maxPanelHeightCount = 2, + private maxPanelHeightCount = 3, ) { this.width = panelSize * this.maxPanelWidthCount; - this.height = panelSize * this.maxPanelHeightCount; } render(userInfo: UserInfo): string { const trophyList = new Array( @@ -29,14 +28,18 @@ export class Card { new TotalPullRequestTrophy(userInfo.totalPullRequests), new TotalRepositoryTrophy(userInfo.totalRepositories), ); + + let panelHeightCount = + Math.floor((trophyList.length - 1) / this.maxPanelWidthCount) + 1; + if (panelHeightCount > this.maxPanelHeightCount) { + panelHeightCount = this.maxPanelHeightCount; + } + this.height = this.panelSize * panelHeightCount; + const renderdTrophy = trophyList.reduce( (sum: string, trophy: Trophy, i: number) => { - let x = (this.panelSize * i); - let y = 0; - if (i >= this.maxPanelWidthCount) { - x = (this.panelSize * (i - this.maxPanelWidthCount)); - y = this.panelSize; - } + const x = this.panelSize * (i % this.maxPanelWidthCount); + const y = this.panelSize * Math.floor(i / this.maxPanelWidthCount); return sum + trophy.render(x, y, this.panelSize); }, "",