mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
utils: 提取公共模块
This commit is contained in:
1 parent
64fec8a407
commit
5cc63bcb22
16 files changed
+356
-427
No files matched your search
@@ -2,44 +2,10 @@ const locales = {};
|
||||
|
||||
declare global {
|
||||
interface String {
|
||||
format: (...args: Array<any>) => string;
|
||||
formatFromArray: (args: any[]) => string;
|
||||
rawformat: (object: any) => string;
|
||||
translate: (...languages: string[]) => string;
|
||||
}
|
||||
}
|
||||
|
||||
String.prototype.format = function formatStr(...args) {
|
||||
let result = this;
|
||||
if (args.length > 0) {
|
||||
if (args.length === 1 && typeof (args[0]) === 'object') {
|
||||
for (const key in args) {
|
||||
if (args[key] !== undefined) {
|
||||
const reg = new RegExp(`(\\{${key}\\})`, 'g');
|
||||
result = result.replace(reg, args[key]);
|
||||
}
|
||||
}
|
||||
} else return this.formatFromArray(args);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
String.prototype.formatFromArray = function formatStr(args) {
|
||||
let result = this;
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] !== undefined) {
|
||||
const reg = new RegExp(`(\\{)${i}(\\})`, 'g');
|
||||
result = result.replace(reg, args[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
String.prototype.rawformat = function rawFormat(object) {
|
||||
const res = this.split('{@}');
|
||||
return [res[0], object, res[1]].join();
|
||||
};
|
||||
|
||||
String.prototype.translate = function translate(...languages: string[]) {
|
||||
for (const language of languages) {
|
||||
if (!language) continue;
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import AnsiUp from 'ansi_up';
|
||||
import { ObjectID } from 'mongodb';
|
||||
import { size, formatSeconds } from '@hydrooj/utils/lib/utils';
|
||||
import { md5 } from './crypto';
|
||||
|
||||
export { size, formatSeconds } from '@hydrooj/utils/lib/utils';
|
||||
|
||||
const AU = new AnsiUp();
|
||||
|
||||
export function ansiToHtml(str: string) {
|
||||
@@ -49,30 +52,6 @@ export function* paginate(page: number, numPages: number) {
|
||||
}
|
||||
}
|
||||
|
||||
export function size(s: number, base = 1) {
|
||||
s *= base;
|
||||
const unit = 1024;
|
||||
const unitNames = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||
for (const unitName of unitNames) {
|
||||
if (s < unit) return '{0} {1}'.format(Math.round(s * 10) / 10, unitName);
|
||||
s /= unit;
|
||||
}
|
||||
return `${Math.round(s * unit)} ${unitNames[unitNames.length - 1]}`;
|
||||
}
|
||||
|
||||
function _digit2(number: number) {
|
||||
return number < 10 ? `0${number}` : number.toString();
|
||||
}
|
||||
|
||||
export function formatSeconds(_seconds = '0') {
|
||||
const seconds = parseInt(_seconds, 10);
|
||||
return '{0}:{1}:{2}'.format(
|
||||
_digit2(Math.floor(seconds / 3600)),
|
||||
_digit2(Math.floor((seconds % 3600) / 60)),
|
||||
_digit2(seconds % 60),
|
||||
);
|
||||
}
|
||||
|
||||
global.Hydro.lib.misc = {
|
||||
gravatar, datetimeSpan, paginate, size, formatSeconds, ansiToHtml,
|
||||
};
|
||||
@@ -1,56 +0,0 @@
|
||||
import systeminformation, { Systeminformation } from 'systeminformation';
|
||||
|
||||
const cache: any = {};
|
||||
|
||||
export interface StatusUpdate {
|
||||
memory: Systeminformation.MemData;
|
||||
load: Systeminformation.CurrentLoadData;
|
||||
battery: Systeminformation.BatteryData;
|
||||
CpuTemp: Systeminformation.CpuTemperatureData;
|
||||
}
|
||||
|
||||
export interface StatusFull extends StatusUpdate {
|
||||
mid: string;
|
||||
cpu: Systeminformation.CpuData;
|
||||
osinfo: Systeminformation.OsData;
|
||||
}
|
||||
|
||||
export async function get(): Promise<StatusFull> {
|
||||
const [uuid, cpu, memory, osinfo, load, CpuTemp, battery] = await Promise.all([
|
||||
systeminformation.uuid(),
|
||||
systeminformation.cpu(),
|
||||
systeminformation.mem(),
|
||||
systeminformation.osInfo(),
|
||||
systeminformation.currentLoad(),
|
||||
systeminformation.cpuTemperature(),
|
||||
systeminformation.battery(),
|
||||
]);
|
||||
const mid = uuid.hardware;
|
||||
cache.cpu = cpu;
|
||||
cache.osinfo = osinfo;
|
||||
cache.mid = mid;
|
||||
return {
|
||||
mid, cpu, memory, osinfo, load, CpuTemp, battery,
|
||||
};
|
||||
}
|
||||
|
||||
export async function update(): Promise<[string, StatusUpdate, StatusFull]> {
|
||||
const [memory, load, CpuTemp, battery] = await Promise.all([
|
||||
systeminformation.mem(),
|
||||
systeminformation.currentLoad(),
|
||||
systeminformation.cpuTemperature(),
|
||||
systeminformation.battery(),
|
||||
]);
|
||||
const { mid, cpu, osinfo } = cache;
|
||||
return [
|
||||
mid,
|
||||
{
|
||||
memory, load, battery, CpuTemp,
|
||||
},
|
||||
{
|
||||
mid, cpu, memory, osinfo, load, battery, CpuTemp,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
global.Hydro.lib.sysinfo = { get, update };
|
||||
Reference in new issue
Block a user