Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LeetCodeManager extends EventEmitter {
public async getLoginStatus(): Promise<void> {
try {
const result: string = await leetCodeExecutor.getUserInfo();
this.currentUser = result.slice("You are now login as".length).trim();
this.currentUser = this.tryParseUserName(result);
this.userStatus = UserStatus.SignedIn;
} catch (error) {
this.currentUser = undefined;
Expand Down Expand Up @@ -117,6 +117,19 @@ class LeetCodeManager extends EventEmitter {
public getUser(): string | undefined {
return this.currentUser;
}

private tryParseUserName(output: string): string {
const lines: string[] = output.trim().split(/\r?\n/);
if (lines.length === 3) {
const reg: RegExp = /^\s*.{1}\s*(.+)\s*https:\/\/leetcode/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex executing on

'    ×      vigilans             https://leetcode-cn.com'

will return

'vigilans             '

Append ? to .+ to prevent greedy match could solve the problem.

Besides, is there any difference between .{1} and .?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

const match: RegExpMatchArray | null = lines[2].match(reg);
if (match && match.length === 2) {
return match[1].trim();
}
}

return "Unknown";
}
}

export const leetCodeManager: LeetCodeManager = new LeetCodeManager();