Skip to content

Commit

Permalink
feat(utils): support template expr interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 31, 2021
1 parent 84dc676 commit 5c7ff2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
5 changes: 4 additions & 1 deletion packages/koishi-utils/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ export namespace template {
}

export function format(source: string, ...params: any[]) {
if (params[0] && typeof params[0] === 'object') {
source = interpolate(source, params[0])
}
let result = ''
let cap: RegExpExecArray
// eslint-disable-next-line no-cond-assign
while (cap = /\{([\w-]+)\}/.exec(source)) {
while (cap = /\{(\w+)\}/.exec(source)) {
result += source.slice(0, cap.index) + (cap[1] in params ? params[cap[1]] : '')
source = source.slice(cap.index + cap[0].length)
}
Expand Down
34 changes: 16 additions & 18 deletions packages/plugin-webui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, Channel, Argv, User } from 'koishi-core'
import { interpolate, Time } from 'koishi-utils'
import { Context, Channel, User } from 'koishi-core'
import { template, Time } from 'koishi-utils'
import { Meta } from './data'
import { Synchronizer } from './stats'
import { SandboxBot } from './adapter'
Expand Down Expand Up @@ -65,10 +65,18 @@ User.extend(() => ({
expire: 0,
}))

export interface Config extends WebServer.Config {
format?: string
formatBot?: string
}
template.set('status', {
// eslint-disable-next-line no-template-curly-in-string
bot: '{{ username }}:{{ code ? `无法连接` : `工作中(${currentRate[0]}/min)` }}',
output: [
'{{ bots }}',
'==========',
'活跃用户数量:{{ activeUsers }}',
'活跃群数量:{{ activeGroups }}',
'CPU 使用率:{{ (cpu[0] * 100).toFixed() }}% / {{ (cpu[1] * 100).toFixed() }}%',
'内存使用率:{{ (memory[0] * 100).toFixed() }}% / {{ (memory[1] * 100).toFixed() }}%',
].join('\n'),
})

const defaultConfig: Config = {
apiPath: '/status',
Expand All @@ -78,16 +86,6 @@ const defaultConfig: Config = {
expiration: Time.week,
tickInterval: Time.second * 5,
refreshInterval: Time.hour,
// eslint-disable-next-line no-template-curly-in-string
formatBot: '{{ username }}:{{ code ? `无法连接` : `工作中(${currentRate[0]}/min)` }}',
format: [
'{{ bots }}',
'==========',
'活跃用户数量:{{ activeUsers }}',
'活跃群数量:{{ activeGroups }}',
'CPU 使用率:{{ (cpu[0] * 100).toFixed() }}% / {{ (cpu[1] * 100).toFixed() }}%',
'内存使用率:{{ (memory[0] * 100).toFixed() }}% / {{ (memory[1] * 100).toFixed() }}%',
].join('\n'),
}

export const name = 'status'
Expand Down Expand Up @@ -115,12 +113,12 @@ export function apply(ctx: Context, config: Config = {}) {
}
status.bots.toString = () => {
return status.bots.map(bot => {
let output = interpolate(formatBot, bot)
let output = template('status.bot', bot)
if (options.all) output = `[${bot.platform}] ` + output
return output
}).join('\n')
}
return interpolate(format, status)
return template('status.output', status)
})

async function getStatus() {
Expand Down

0 comments on commit 5c7ff2a

Please sign in to comment.