From c259caef3823e77e14d72831a7f6a62c0a008c9d Mon Sep 17 00:00:00 2001 From: redhoodsu Date: Tue, 29 Oct 2024 16:38:00 +0800 Subject: [PATCH] release(logcat): v0.2.0 --- src/logcat/README.md | 1 + src/logcat/index.ts | 74 ++++++++++++++++++++++++++++++----------- src/logcat/package.json | 2 +- src/logcat/story.js | 8 ++++- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/logcat/README.md b/src/logcat/README.md index af4b996..4c5fc6c 100644 --- a/src/logcat/README.md +++ b/src/logcat/README.md @@ -46,6 +46,7 @@ logcatp.append({ * entries(IEntry[]): Log entries. * filter(IFilter): Log filter. * maxNum(number): Max entry number, zero means infinite. +* view('standard' | 'compact'): Log formatting. * wrapLongLines(boolean): Wrap long lines. ## Api diff --git a/src/logcat/index.ts b/src/logcat/index.ts index f71ff26..0927c7d 100644 --- a/src/logcat/index.ts +++ b/src/logcat/index.ts @@ -20,6 +20,8 @@ export interface IOptions extends IComponentOptions { filter?: IFilter /** Log entries. */ entries?: IEntry[] + /** Log formatting. */ + view?: 'standard' | 'compact' /** Wrap long lines. */ wrapLongLines?: boolean } @@ -47,6 +49,10 @@ interface IEntry extends IBaseEntry { date: string | Date } +interface IInnerEntry extends IBaseEntry { + date: Date +} + /** * Android logcat viewer. * @@ -67,13 +73,14 @@ export default class Logcat extends Component { private render: types.AnyFn private entries: Array<{ container: HTMLElement - entry: IBaseEntry & { date: Date } + entry: IInnerEntry }> = [] constructor(container: HTMLElement, options: IOptions = {}) { super(container, { compName: 'logcat' }) this.initOptions(options, { maxNum: 0, + view: 'standard', entries: [], wrapLongLines: false, }) @@ -98,15 +105,13 @@ export default class Logcat extends Component { /** Append entry. */ append(entry: IEntry) { const { c, entries } = this - const { maxNum } = this.options + const { maxNum, view } = this.options const date: Date = isDate(entry.date) ? (entry.date as Date) : new Date(entry.date) - const level = ['?', '?', 'V', 'D', 'I', 'W', 'E'][entry.priority] - - const container = h(`.${c('entry')}.${c(level)}`) + const container = h(`.${c('entry')}.${c(toLetter(entry.priority))}`) const e = { ...entry, date, @@ -123,20 +128,8 @@ export default class Logcat extends Component { } } - const html = [ - `${dateFormat( - date, - 'yyyy-mm-dd HH:MM:ss.l' - )}`, - `${entry.pid}-${entry.tid}`, - `${escape( - entry.tag - )}`, - `${escape(entry.package)}`, - `${level}`, - `${escape(entry.message)}`, - ].join(' ') - container.innerHTML = html + container.innerHTML = + view === 'standard' ? this.formatStandard(e) : this.formatCompact(e) if (this.filterEntry(e)) { const isAtBottom = this.isAtBottom @@ -204,6 +197,16 @@ export default class Logcat extends Component { this.render() } break + case 'view': + each(entries, (entry) => { + const html = + val === 'standard' + ? this.formatStandard(entry.entry) + : this.formatCompact(entry.entry) + + entry.container.innerHTML = html + }) + break case 'filter': this.render() break @@ -224,6 +227,35 @@ export default class Logcat extends Component { } this.isAtBottom = isAtBottom } + private formatStandard(entry: IInnerEntry) { + const { c } = this + + return [ + `${dateFormat( + entry.date, + 'yyyy-mm-dd HH:MM:ss.l' + )}`, + `${entry.pid}-${entry.tid}`, + `${escape( + entry.tag + )}`, + `${escape(entry.package)}`, + `${toLetter(entry.priority)}`, + `${escape(entry.message)}`, + ].join(' ') + } + private formatCompact(entry: IInnerEntry) { + const { c } = this + + return [ + `${dateFormat( + entry.date, + 'HH:MM:ss.l' + )}`, + `${toLetter(entry.priority)}`, + `${escape(entry.message)}`, + ].join(' ') + } private _render() { const { container } = this this.$container.html('') @@ -239,6 +271,10 @@ export default class Logcat extends Component { } } +function toLetter(priority: number) { + return ['?', '?', 'V', 'D', 'I', 'W', 'E'][priority] +} + function getColor(str: string) { return colors[strHash(str) % colors.length] } diff --git a/src/logcat/package.json b/src/logcat/package.json index 54589bc..67b13d2 100644 --- a/src/logcat/package.json +++ b/src/logcat/package.json @@ -1,6 +1,6 @@ { "name": "logcat", - "version": "0.1.1", + "version": "0.2.0", "description": "Android logcat viewer", "luna": { "react": true diff --git a/src/logcat/story.js b/src/logcat/story.js index 397526c..53a746f 100644 --- a/src/logcat/story.js +++ b/src/logcat/story.js @@ -13,11 +13,12 @@ const def = story( (container) => { $(container).css('height', '500px') - const { wrapLongLines, maxNum, filter } = createKnobs() + const { wrapLongLines, maxNum, filter, view } = createKnobs() const logcat = new Logcat(container, { filter, maxNum, + view, wrapLongLines, }) each(logs, (log) => logcat.append(log)) @@ -63,6 +64,10 @@ function createKnobs() { max: 1000, step: 10, }) + const view = select('View', { + 'Standard View': 'standard', + 'Compact View': 'compact', + }) const wrapLongLines = boolean('Wrap Long Lines', false) return { @@ -71,6 +76,7 @@ function createKnobs() { package: filterPackage, tag: filterTag, }, + view, maxNum, wrapLongLines, }