-
-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workspace): add createStatusBarItem method
- Loading branch information
Showing
7 changed files
with
137 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Disposable } from 'vscode-languageserver-protocol' | ||
import { NeovimClient as Neovim } from '@chemzqm/neovim' | ||
import { StatusBarItem } from '../types' | ||
import uuidv1 = require('uuid/v1') | ||
|
||
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] | ||
|
||
export default class StatusLine implements Disposable { | ||
private items: Map<string, StatusBarItem> = new Map() | ||
private shownIds: Set<string> = new Set() | ||
private _text = '' | ||
private interval: NodeJS.Timer | ||
constructor(private nvim: Neovim) { | ||
this.interval = setInterval(() => { | ||
this.setStatusText().catch(_e => { | ||
// noop | ||
}) | ||
}, 100) | ||
} | ||
|
||
public dispose(): void { | ||
clearInterval(this.interval) | ||
} | ||
|
||
public createStatusBarItem(priority = 0, isProgress = false): StatusBarItem { | ||
let uid = uuidv1() | ||
|
||
let item: StatusBarItem = { | ||
text: '', | ||
priority, | ||
isProgress, | ||
show: () => { | ||
this.shownIds.add(uid) | ||
}, | ||
hide: () => { | ||
this.shownIds.delete(uid) | ||
}, | ||
dispose: () => { | ||
this.shownIds.delete(uid) | ||
this.items.delete(uid) | ||
} | ||
} | ||
this.items.set(uid, item) | ||
return item | ||
} | ||
|
||
private getText(): string { | ||
if (this.shownIds.size == 0) return '' | ||
let d = new Date() | ||
let idx = Math.floor(d.getMilliseconds() / 100) | ||
let text = '' | ||
let items: StatusBarItem[] = [] | ||
for (let [id, item] of this.items) { | ||
if (this.shownIds.has(id)) { | ||
items.push(item) | ||
} | ||
} | ||
items.sort((a, b) => a.priority - b.priority) | ||
for (let item of items) { | ||
if (!item.isProgress) { | ||
text = `${text} ${item.text}` | ||
} else { | ||
text = `${text} ${frames[idx]} ${item.text}` | ||
} | ||
} | ||
return text | ||
} | ||
|
||
private async setStatusText(): Promise<void> { | ||
let text = this.getText() | ||
if (text != this._text) { | ||
this._text = text | ||
await this.nvim.setVar('coc_status', text) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters