Skip to content

Commit

Permalink
hide cursor in progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jan 7, 2024
1 parent 93a0dfe commit a382291
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
20 changes: 14 additions & 6 deletions src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,23 @@ export class TrzszFilter {
this.uploadFilesResolve = null;
this.uploadFilesReject = null;
this.trzszTransfer.cleanup();
if (this.textProgressBar) {
this.textProgressBar.showCursor();
}
this.textProgressBar = null;
this.trzszTransfer = null;
}
}

private createProgressBar(quiet?: boolean, tmuxPaneColumns?: number) {
if (quiet === true) {
this.textProgressBar = null;
return;
}
this.textProgressBar = new TextProgressBar(this.writeToTerminal, this.terminalColumns, tmuxPaneColumns);
this.textProgressBar.hideCursor();
}

private async handleTrzszDownloadFiles(_version: string, remoteIsWindows: boolean) {
let savePath: string;
let saveParam: any;
Expand Down Expand Up @@ -360,9 +372,7 @@ export class TrzszFilter {
await this.trzszTransfer.sendAction(true, remoteIsWindows);
const config = await this.trzszTransfer.recvConfig();

if (config.quiet !== true) {
this.textProgressBar = new TextProgressBar(this.writeToTerminal, this.terminalColumns, config.tmux_pane_width);
}
this.createProgressBar(config.quiet, config.tmux_pane_width);

const localNames = await this.trzszTransfer.recvFiles(saveParam, openSaveFile, this.textProgressBar);

Expand Down Expand Up @@ -393,9 +403,7 @@ export class TrzszFilter {
checkDuplicateNames(sendFiles);
}

if (config.quiet !== true) {
this.textProgressBar = new TextProgressBar(this.writeToTerminal, this.terminalColumns, config.tmux_pane_width);
}
this.createProgressBar(config.quiet, config.tmux_pane_width);

const remoteNames = await this.trzszTransfer.sendFiles(sendFiles, this.textProgressBar);

Expand Down
10 changes: 9 additions & 1 deletion src/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class TextProgressBar implements ProgressCallback {
public constructor(
writer: (output: string) => void,
columns: number,
tmuxPaneColumns: number | undefined = undefined
tmuxPaneColumns: number | undefined = undefined,
) {
this.writer = writer;
this.tmuxPaneColumns = tmuxPaneColumns || 0;
Expand Down Expand Up @@ -153,6 +153,14 @@ export class TextProgressBar implements ProgressCallback {
this.showProgress();
}

public hideCursor() {
this.writer("\x1b[?25l");
}

public showCursor() {
this.writer("\x1b[?25h");
}

private showProgress() {
const now = Date.now();
if (now - this.lastUpdateTime < 200) {
Expand Down
1 change: 1 addition & 0 deletions src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ export class TrzszTransfer {
}
process.stdout.write(msg);
process.stdout.write("\r\n");
process.stdout.write("\x1b[?25h");
if (this.transferConfig.tmux_output_junk) {
await tmuxRefreshClient();
}
Expand Down
12 changes: 6 additions & 6 deletions test/addon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ test("trz upload files using addon", async () => {

onMessage({ data: "Received test.txt.0 to /tmp\n" });

expect(term.write.mock.calls.length).toBe(4);
expect(term.write.mock.calls[1][0]).toContain("test.txt [");
expect(term.write.mock.calls.length).toBe(6);
expect(term.write.mock.calls[2][0]).toContain("test.txt [");
expect(term.write.mock.calls[3][0]).toBe("Received test.txt.0 to /tmp\n");
expect(term.write.mock.calls[3][0]).toContain("test.txt [");
expect(term.write.mock.calls[5][0]).toBe("Received test.txt.0 to /tmp\n");

ws.listener["close"]();
expect(term.mockDispose.dispose.mock.calls.length).toBe(3);
Expand Down Expand Up @@ -199,10 +199,10 @@ test("tsz download files using addon", async () => {

onMessage({ data: "Saved test.txt.0 to /tmp\n" });

expect(term.write.mock.calls.length).toBe(4);
expect(term.write.mock.calls[1][0]).toContain("test.txt [");
expect(term.write.mock.calls.length).toBe(6);
expect(term.write.mock.calls[2][0]).toContain("test.txt [");
expect(term.write.mock.calls[3][0]).toBe("Saved test.txt.0 to /tmp\n");
expect(term.write.mock.calls[3][0]).toContain("test.txt [");
expect(term.write.mock.calls[5][0]).toBe("Saved test.txt.0 to /tmp\n");

expect(file.closeFile.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(file.getLocalName.mock.calls.length).toBeGreaterThanOrEqual(1);
Expand Down
24 changes: 12 additions & 12 deletions test/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ test("trz upload files", async () => {

trzsz.processServerOutput("Received test.txt.0 to /tmp\n");

expect(writeToTerminal.mock.calls.length).toBe(4);
expect(writeToTerminal.mock.calls[1][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls.length).toBe(6);
expect(writeToTerminal.mock.calls[2][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[3][0]).toBe("Received test.txt.0 to /tmp\n");
expect(writeToTerminal.mock.calls[3][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[5][0]).toBe("Received test.txt.0 to /tmp\n");
});

test("tsz download files", async () => {
Expand Down Expand Up @@ -231,10 +231,10 @@ test("tsz download files", async () => {

trzsz.processServerOutput("Saved test.txt.0 to /tmp\n");

expect(writeToTerminal.mock.calls.length).toBe(4);
expect(writeToTerminal.mock.calls[1][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls.length).toBe(6);
expect(writeToTerminal.mock.calls[2][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[3][0]).toBe("Saved test.txt.0 to /tmp\n");
expect(writeToTerminal.mock.calls[3][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[5][0]).toBe("Saved test.txt.0 to /tmp\n");

expect(fs.readFileSync(path.join(tmpDir, "test.txt.0")).toString()).toBe("test content\n");
});
Expand Down Expand Up @@ -392,10 +392,10 @@ test("trz upload files in browser", async () => {

trzsz.processServerOutput("Received test.txt.0 to /tmp\n");

expect(writeToTerminal.mock.calls.length).toBe(4);
expect(writeToTerminal.mock.calls[1][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls.length).toBe(6);
expect(writeToTerminal.mock.calls[2][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[3][0]).toBe("Received test.txt.0 to /tmp\n");
expect(writeToTerminal.mock.calls[3][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[5][0]).toBe("Received test.txt.0 to /tmp\n");

selectSendFiles.mockRestore();
});
Expand Down Expand Up @@ -462,10 +462,10 @@ test("tsz download files in browser", async () => {

trzsz.processServerOutput("Saved test.txt.0 to /tmp\n");

expect(writeToTerminal.mock.calls.length).toBe(4);
expect(writeToTerminal.mock.calls[1][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls.length).toBe(6);
expect(writeToTerminal.mock.calls[2][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[3][0]).toBe("Saved test.txt.0 to /tmp\n");
expect(writeToTerminal.mock.calls[3][0]).toContain("test.txt [");
expect(writeToTerminal.mock.calls[5][0]).toBe("Saved test.txt.0 to /tmp\n");

expect(file.closeFile.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(file.getLocalName.mock.calls.length).toBeGreaterThanOrEqual(1);
Expand Down

0 comments on commit a382291

Please sign in to comment.