mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-03 09:49:24 +02:00
refactor: improve readability
This commit is contained in:
parent
c5786fefd9
commit
094b859791
4 changed files with 55 additions and 68 deletions
|
@ -38,27 +38,27 @@ export class Cmd extends AdbCommandBase {
|
||||||
command: string,
|
command: string,
|
||||||
...args: string[]
|
...args: string[]
|
||||||
) {
|
) {
|
||||||
let supportAbb: boolean;
|
let supportsAbb: boolean;
|
||||||
let supportCmd: boolean = this.supportsCmd;
|
let supportsCmd: boolean = this.supportsCmd;
|
||||||
let service: string;
|
let service: string;
|
||||||
let Protocol: AdbSubprocessProtocolConstructor;
|
let Protocol: AdbSubprocessProtocolConstructor;
|
||||||
if (shellProtocol) {
|
if (shellProtocol) {
|
||||||
supportAbb = this._supportsAbb;
|
supportsAbb = this._supportsAbb;
|
||||||
supportCmd &&= this.supportsShellV2;
|
supportsCmd &&= this.supportsShellV2;
|
||||||
service = "abb";
|
service = "abb";
|
||||||
Protocol = AdbSubprocessShellProtocol;
|
Protocol = AdbSubprocessShellProtocol;
|
||||||
} else {
|
} else {
|
||||||
supportAbb = this._supportsAbbExec;
|
supportsAbb = this._supportsAbbExec;
|
||||||
service = "abb_exec";
|
service = "abb_exec";
|
||||||
Protocol = AdbSubprocessNoneProtocol;
|
Protocol = AdbSubprocessNoneProtocol;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (supportAbb) {
|
if (supportsAbb) {
|
||||||
const socket = await this.adb.createSocket(
|
const socket = await this.adb.createSocket(
|
||||||
`${service}:${command}\0${args.join("\0")}\0`
|
`${service}:${command}\0${args.join("\0")}\0`
|
||||||
);
|
);
|
||||||
return new Protocol(socket);
|
return new Protocol(socket);
|
||||||
} else if (supportCmd) {
|
} else if (supportsCmd) {
|
||||||
return Protocol.raw(this.adb, `cmd ${command} ${args.join(" ")}`);
|
return Protocol.raw(this.adb, `cmd ${command} ${args.join(" ")}`);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Not supported");
|
throw new Error("Not supported");
|
||||||
|
|
|
@ -116,46 +116,34 @@ export interface AndroidLogEntry extends LoggerEntry {
|
||||||
toString(format?: LogcatFormat, modifiers?: LogcatFormatModifiers): string;
|
toString(format?: LogcatFormat, modifiers?: LogcatFormatModifiers): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function padZero(number: number, length: number) {
|
||||||
|
return number.toString().padStart(length, "0");
|
||||||
|
}
|
||||||
|
|
||||||
function formatSeconds(seconds: number, modifiers: LogcatFormatModifiers) {
|
function formatSeconds(seconds: number, modifiers: LogcatFormatModifiers) {
|
||||||
if (modifiers.monotonic) {
|
if (modifiers.monotonic) {
|
||||||
return seconds.toString().padStart(6);
|
return padZero(seconds, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (modifiers.epoch) {
|
if (modifiers.epoch) {
|
||||||
return seconds.toString().padStart(19);
|
return padZero(seconds, 19);
|
||||||
}
|
}
|
||||||
|
|
||||||
const date = new Date(seconds * 1000);
|
const date = new Date(seconds * 1000);
|
||||||
|
|
||||||
|
const month = padZero(date.getMonth() + 1, 2);
|
||||||
|
const day = padZero(date.getDate(), 2);
|
||||||
|
const hour = padZero(date.getHours(), 2);
|
||||||
|
const minute = padZero(date.getMinutes(), 2);
|
||||||
|
const second = padZero(date.getSeconds(), 2);
|
||||||
|
const result = `${month}-${day} ${hour}:${minute}:${second}`;
|
||||||
|
|
||||||
if (modifiers.year) {
|
if (modifiers.year) {
|
||||||
// prettier-ignore
|
const year = padZero(date.getFullYear(), 4);
|
||||||
return `${
|
return `${year}-${result}`;
|
||||||
date.getFullYear().toString().padStart(4, "0")
|
|
||||||
}-${
|
|
||||||
(date.getMonth() + 1).toString().padStart(2, "0")
|
|
||||||
}-${
|
|
||||||
date.getDate().toString().padStart(2, "0")
|
|
||||||
} ${
|
|
||||||
date.getHours().toString().padStart(2, "0")
|
|
||||||
}:${
|
|
||||||
date.getMinutes().toString().padStart(2, "0")
|
|
||||||
}:${
|
|
||||||
date.getSeconds().toString().padStart(2, "0")
|
|
||||||
}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// prettier-ignore
|
return result;
|
||||||
return `${
|
|
||||||
(date.getMonth() + 1).toString().padStart(2, "0")
|
|
||||||
}-${
|
|
||||||
date.getDate().toString().padStart(2, "0")
|
|
||||||
} ${
|
|
||||||
date.getHours().toString().padStart(2, "0")
|
|
||||||
}:${
|
|
||||||
date.getMinutes().toString().padStart(2, "0")
|
|
||||||
}:${
|
|
||||||
date.getSeconds().toString().padStart(2, "0")
|
|
||||||
}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatNanoseconds(
|
function formatNanoseconds(
|
||||||
|
@ -163,14 +151,14 @@ function formatNanoseconds(
|
||||||
modifiers: LogcatFormatModifiers
|
modifiers: LogcatFormatModifiers
|
||||||
) {
|
) {
|
||||||
if (modifiers.nanoseconds) {
|
if (modifiers.nanoseconds) {
|
||||||
return nanoseconds.toString().padStart(9, "0");
|
return padZero(nanoseconds, 9);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (modifiers.microseconds) {
|
if (modifiers.microseconds) {
|
||||||
return ((nanoseconds / 1000) | 0).toString().padStart(6, "0");
|
return padZero(nanoseconds / 1000, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ((nanoseconds / 1000000) | 0).toString().padStart(3, "0");
|
return padZero(nanoseconds / 1000000, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatTimezone(seconds: number, modifiers: LogcatFormatModifiers) {
|
function formatTimezone(seconds: number, modifiers: LogcatFormatModifiers) {
|
||||||
|
|
|
@ -39,14 +39,14 @@ export class Settings extends AdbCommandBase {
|
||||||
tag?: string,
|
tag?: string,
|
||||||
makeDefault?: boolean
|
makeDefault?: boolean
|
||||||
) {
|
) {
|
||||||
return this.base(
|
const args = [key, value];
|
||||||
"put",
|
if (tag) {
|
||||||
namespace,
|
args.push(tag);
|
||||||
key,
|
}
|
||||||
value,
|
if (makeDefault) {
|
||||||
...(tag ? [tag] : []),
|
args.push("default");
|
||||||
...(makeDefault ? ["default"] : [])
|
}
|
||||||
);
|
return this.base("put", namespace, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public reset(
|
public reset(
|
||||||
|
@ -63,12 +63,11 @@ export class Settings extends AdbCommandBase {
|
||||||
modeOrPackageName: string,
|
modeOrPackageName: string,
|
||||||
tag?: string
|
tag?: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
return this.base(
|
const args = [modeOrPackageName];
|
||||||
"reset",
|
if (tag) {
|
||||||
namespace,
|
args.push(tag);
|
||||||
modeOrPackageName,
|
}
|
||||||
...(tag ? [tag] : [])
|
return this.base("reset", namespace, ...args);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async list(namespace: SettingsNamespace): Promise<string[]> {
|
public async list(namespace: SettingsNamespace): Promise<string[]> {
|
||||||
|
|
|
@ -142,13 +142,16 @@ export class BTreeNode {
|
||||||
|
|
||||||
public has(value: number): boolean {
|
public has(value: number): boolean {
|
||||||
let index = this.search(value);
|
let index = this.search(value);
|
||||||
|
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.height > 0) {
|
if (this.height > 0) {
|
||||||
index = ~index;
|
index = ~index;
|
||||||
return this.children[index]!.has(value);
|
return this.children[index]!.has(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,16 +195,18 @@ export class BTreeNode {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.height > 0) {
|
if (this.height === 0) {
|
||||||
index = ~index;
|
return false;
|
||||||
const deleted = this.children[index]!.delete(value);
|
|
||||||
if (deleted) {
|
|
||||||
this.balance(index);
|
|
||||||
}
|
|
||||||
return deleted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
index = ~index;
|
||||||
|
const deleted = this.children[index]!.delete(value);
|
||||||
|
|
||||||
|
if (deleted) {
|
||||||
|
this.balance(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
public max(): number {
|
public max(): number {
|
||||||
|
@ -340,17 +345,12 @@ export class BTree {
|
||||||
|
|
||||||
public constructor(order: number) {
|
public constructor(order: number) {
|
||||||
this._order = order;
|
this._order = order;
|
||||||
this._root = new BTreeNode(
|
const keys = new Int32Array(order - 1);
|
||||||
order,
|
const children = new Array<BTreeNode>(order);
|
||||||
new Int32Array(order - 1),
|
this._root = new BTreeNode(order, keys, 0, 0, children);
|
||||||
0,
|
|
||||||
0,
|
|
||||||
new Array<BTreeNode>(order)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public has(value: number) {
|
public has(value: number) {
|
||||||
// TODO(btree): benchmark this non-recursive version
|
|
||||||
let node = this._root;
|
let node = this._root;
|
||||||
while (true) {
|
while (true) {
|
||||||
const index = node.search(value);
|
const index = node.search(value);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue