Skip to content

Commit

Permalink
Summary and fix of UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiliopg91 committed Sep 20, 2024
1 parent 4ed2d3a commit 61321d8
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 42 deletions.
36 changes: 18 additions & 18 deletions src/components/cpuBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,26 +185,26 @@ export const CpuBlock: FC = () => {
onChange={onFpplChange}
/>
</PanelSectionRow>
<PanelSectionRow>
<ToggleField
label="SMT"
description={Translator.translate('smt.description')}
checked={profile.smtEnabled}
onChange={onSmtChange}
highlightOnFocus
/>
</PanelSectionRow>
<PanelSectionRow>
<ToggleField
label="CPU Boost"
description={Translator.translate("cpu.boost.description")}
checked={profile.cpuBoost}
onChange={onCpuBoostChange}
highlightOnFocus
/>
</PanelSectionRow>
</>
}
<PanelSectionRow>
<ToggleField
label="SMT"
description={Translator.translate('smt.description')}
checked={profile.smtEnabled}
onChange={onSmtChange}
highlightOnFocus
/>
</PanelSectionRow>
<PanelSectionRow>
<ToggleField
label="CPU Boost"
description={Translator.translate("cpu.boost.description")}
checked={profile.cpuBoost}
onChange={onCpuBoostChange}
highlightOnFocus
/>
</PanelSectionRow>
</PanelSection>
);
};
56 changes: 32 additions & 24 deletions src/settings/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@ import { State } from "../utils/state"
import { debounce } from "lodash"
import { BackendUtils } from "../utils/backend"
import { Profile } from "../utils/models"
import { SpreadSheet, SpreadSheetCell } from "../utils/spreadsheet"

export class Profiles {

public static summary() {

const profiles = Settings.getConfigurationStructured()["profiles"]

let maxNameLen = 0
let maxProfLen = 0
let profCount = 0;
let sortedAppIds: Array<{ appId: string, name: string }> = []
Object.keys(profiles).forEach((appId) => {
const len = (profiles[appId].name as String).length + appId.length + 3
maxNameLen = Math.max(len, maxNameLen)
sortedAppIds.push({ appId, name: profiles[appId].name })

Object.keys(profiles[appId]).forEach((pwr) => {
if (pwr != "name") {
maxProfLen = Math.max(Mode[Number(profiles[appId][pwr].mode)].length, maxProfLen)
profCount++
}
})
Expand All @@ -42,36 +37,49 @@ export class Profiles {
return 0;
});

Logger.info("")
Logger.info("Loaded profiles " + profCount + " for " + Object.keys(profiles).length + " games: ")

const header1 = " --------------------------------------------------" + ("".padStart(maxProfLen, "-"));
const header2 = " | POWER | " + ("MODE".padStart(((maxProfLen + 4) / 2) + (maxProfLen % 2))).padEnd(maxProfLen) + " | SPL | SPPL | FPPL | SMT | BOOST |";
const separator = "-----------------------------------------------------" + ("".padStart(maxProfLen, "-"))

Logger.info((header1.padStart(header1.length + maxNameLen + 2)))
Logger.info((header2.padStart(header2.length + maxNameLen + 2)))
const headers: Array<SpreadSheetCell> = []
headers.push({ data: "NAME", align: "center" })
headers.push({ data: "APPID", align: "center" })
headers.push({ data: "POWER", align: "center" })
headers.push({ data: "MODE", align: "center" })
headers.push({ data: "SPL*", align: "center" })
headers.push({ data: "SPPL*", align: "center" })
headers.push({ data: "FPPL*", align: "center" })
headers.push({ data: "SMT*", align: "center" })
headers.push({ data: "BOOST*", align: "center" })

const body: Array<Array<SpreadSheetCell>> = []
sortedAppIds.forEach((entry) => {
Logger.info(separator.padStart(separator.length + maxNameLen, "-"))
let isFirst = true
Object.keys(profiles[entry.appId]).forEach((pwr) => {
if (pwr != "name") {
const profile = profiles[entry.appId][pwr]
let line = "| "
line += (isFirst ? ((profiles[entry.appId].name + " (" + entry.appId + ")").padStart(maxNameLen)) : "".padStart(maxNameLen)) + " | ";
line += pwr.toUpperCase() + " | "
line += Mode[Number(profile.mode)].padStart(maxProfLen) + " | "
line += (profile.cpu.tdp.spl as String).padStart(3) + " | "
line += (profile.cpu.tdp.sppl as String).padStart(3) + " | "
line += (profile.cpu.tdp.fppl as String).padStart(3) + " | "
line += (profile.cpu.smt as String).padStart(5) + " | "
line += (profile.cpu.boost as String).padStart(5) + " | "
Logger.info(line)

let line: Array<SpreadSheetCell> = []
line.push({ data: (isFirst ? profiles[entry.appId].name : ""), align: "right", rowspan: !isFirst })
line.push({ data: (isFirst ? entry.appId : ""), align: "right", rowspan: !isFirst })
line.push({ data: pwr.toUpperCase(), align: "right" })
line.push({ data: Mode[Number(profile.mode)], align: "right" })
line.push({ data: profile.cpu.tdp.spl, align: "right" })
line.push({ data: profile.cpu.tdp.sppl, align: "right" })
line.push({ data: profile.cpu.tdp.fppl, align: "right" })
line.push({ data: profile.cpu.smt, align: "right" })
line.push({ data: profile.cpu.boost, align: "right" })

body.push(line)

isFirst = false
}
})
})

Logger.info(separator.padStart(separator.length + maxNameLen, "-"))
SpreadSheet.printSpreadSheet(headers, body)
Logger.info("")
Logger.info("* Only is used on CUSTOM mode")
Logger.info("")
}

public static getAppId(id: string): string {
Expand Down
75 changes: 75 additions & 0 deletions src/utils/spreadsheet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Logger } from "decky-plugin-framework"

export type SpreadSheetAlignment = "left" | "center" | "right"

export interface SpreadSheetCell {
data: any
align: SpreadSheetAlignment
padding?: string
rowspan?: boolean
colspan?: boolean
}

export class SpreadSheet {
public static printSpreadSheet(headers: Array<SpreadSheetCell>, body: Array<Array<SpreadSheetCell>>) {
const lengths = SpreadSheet.calcLengths(headers, body)
const totalLineLength = lengths.reduce((sum, current) => sum + current + 3, 0) + 1
let head = ""
let line = ""
for (let i = 0; i < headers.length; i++) {
if (headers[i].rowspan) {
head += "|" + SpreadSheet.padCell("", "left", lengths[i] + 2, " ")
} else {
head += SpreadSheet.padCell("", "left", lengths[i] + 3, "-")
}
line += (headers[i].colspan ? " " : "| ") + SpreadSheet.padCell(String(headers[i].data), headers[i].align, lengths[i], headers[i].padding) + " "
}
Logger.info(head + "-")
Logger.info(line + "|")
for (let i = 0; i < body.length; i++) {
let head = ""
let line = ""
for (let j = 0; j < body[i].length; j++) {
if (body[i][j].rowspan) {
head += "|" + SpreadSheet.padCell("", "left", lengths[j] + 2, " ")
} else {
head += SpreadSheet.padCell("", "left", lengths[j] + 3, "-")
}
line += (body[i][j].colspan ? " " : "| ") + SpreadSheet.padCell(String(body[i][j].data), body[i][j].align, lengths[j], body[i][j].padding) + " "
}
Logger.info(head + "-")
Logger.info(line + "|")
}
Logger.info(SpreadSheet.padCell("", "left", totalLineLength, "-"))
}

private static calcLengths(headers: Array<SpreadSheetCell>, body: Array<Array<SpreadSheetCell>>) {
const lengths: Array<number> = []

for (let i = 0; i < headers.length; i++) {
let maxLength = String(headers[i].data).length
for (let j = 0; j < body.length; j++) {
maxLength = Math.max(maxLength, String(body[j][i].data).length)
}
lengths.push(maxLength)
}

return lengths
}

private static padCell(data: string, align: SpreadSheetAlignment, size: number, padding?: string) {
if (align == "left") {
return data.padEnd(size, padding)
}

if (align == "right") {
return data.padStart(size, padding)
}

let leftPad = Math.ceil((size - data.length) / 2)
return data.padStart(leftPad + data.length).padEnd(size)
}
}



0 comments on commit 61321d8

Please sign in to comment.