Skip to content

Commit

Permalink
feat: support tab history
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Mar 25, 2021
1 parent db3fef9 commit 52e58ea
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
2 changes: 2 additions & 0 deletions addons/menu/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"New Tab#!menu.newtab": "新建标签页",
"Duplicate Tab#!menu.duplicatetab": "复制标签页",
"New Window#!menu.newwindow": "新建窗口",
"Go Back#!menu.goback": "后退",
"Go Forward#!menu.goforward": "前进",
"Show Tab Options#!menu.showtaboptions": "展示标签页选项",
"Select Previous Tab#!menu.previoustab": "选择上一个标签页",
"Select Next Tab#!menu.nexttab": "选择下一个标签页",
Expand Down
65 changes: 54 additions & 11 deletions renderer/hooks/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,57 @@ export function useTerminalActiveIndex() {
return activeIndexRef
}

const currentTerminalRef = computed(() => {
const activeIndex = unref(activeIndexRef)
if (activeIndex === -1) return null
const tabs = unref(tabsRef)
return tabs[activeIndex]
})
export function useCurrentTerminal() {
return computed(() => {
const activeIndex = unref(activeIndexRef)
if (activeIndex === -1) return null
const tabs = unref(tabsRef)
return tabs[activeIndex]
})
return currentTerminalRef
}

export function getTerminalTabIndex(tab: TerminalTab) {
const tabs = unref(tabsRef)
return tabs.indexOf(toRaw(tab))
}

const historyRef = ref<TerminalTab[]>([])
const historyIndexRef = ref(-1)
const historyStateRef = computed(() => {
const history = unref(historyRef)
const historyIndex = unref(historyIndexRef)
return history[historyIndex]
})

watch(currentTerminalRef, (value, oldValue) => {
if (!value) return
const historyState = unref(historyStateRef)
if (oldValue && historyState !== oldValue) return
const history = unref(historyRef)
const historyIndex = unref(historyIndexRef)
// TODO: max stack size
historyRef.value = [...history.slice(0, historyIndex + 1), value]
historyIndexRef.value += 1
})

export function travelInTerminalHistory(step: number) {
const history = unref(historyRef)
if (!history.length) return
const historyIndex = unref(historyIndexRef)
const index = Math.min(Math.max(historyIndex + step, 0), history.length - 1)
const state = history[index]
const tabIndex = getTerminalTabIndex(state)
if (tabIndex === -1) {
history.splice(index, 1)
travelInTerminalHistory(step)
} else {
// Must change tab index before history index changed
activeIndexRef.value = tabIndex
historyIndexRef.value = index
}
}

export const useTerminalShells = memoize(() => {
return useRemoteData<string[]>([], {
getter: 'get-shells',
Expand Down Expand Up @@ -163,7 +200,7 @@ export async function createTerminalTab({ cwd, shell: shellPath, command, launch

const createResizeObserver = memoize(() => {
return new ResizeObserver(debounce(() => {
const tab = unref(useCurrentTerminal())
const tab = unref(currentTerminalRef)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (tab?.xterm?.element) {
tab.addons.fit.fit()
Expand Down Expand Up @@ -193,7 +230,7 @@ export function handleTerminalMessages() {
createTerminalTab(options)
})
ipcRenderer.on('duplicate-tab', event => {
const currentTerminal = unref(useCurrentTerminal())
const currentTerminal = unref(currentTerminalRef)
if (currentTerminal) {
createTerminalTab({
cwd: currentTerminal.cwd,
Expand All @@ -202,7 +239,7 @@ export function handleTerminalMessages() {
}
})
ipcRenderer.on('close-tab', () => {
const currentTerminal = unref(useCurrentTerminal())
const currentTerminal = unref(currentTerminalRef)
if (currentTerminal) {
closeTerminalTab(currentTerminal)
}
Expand Down Expand Up @@ -232,13 +269,13 @@ export function handleTerminalMessages() {
removeTerminalTab(tab)
})
ipcRenderer.on('clear-terminal', () => {
const tab = unref(useCurrentTerminal())
const tab = unref(currentTerminalRef)
if (!tab) return
const xterm = tab.xterm
xterm.clear()
})
ipcRenderer.on('close-terminal', () => {
const tab = unref(useCurrentTerminal())
const tab = unref(currentTerminalRef)
if (!tab) return
closeTerminalTab(tab)
})
Expand All @@ -262,6 +299,12 @@ export function handleTerminalMessages() {
activeIndexRef.value = activeIndex + 1
}
})
ipcRenderer.on('go-back', () => {
travelInTerminalHistory(-1)
})
ipcRenderer.on('go-forward', () => {
travelInTerminalHistory(1)
})
ipcRenderer.on('show-tab-options', () => {
const tabs = unref(tabsRef)
const activeIndex = unref(activeIndexRef)
Expand Down
10 changes: 10 additions & 0 deletions resources/terminal.menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
{
"type": "separator"
},
{
"label": "Go Back#!menu.goback",
"accelerator": "CmdOrCtrl+[",
"command": "go-back"
},
{
"label": "Go Forward#!menu.goforward",
"accelerator": "CmdOrCtrl+]",
"command": "go-forward"
},
{
"label": "Show Tab Options#!menu.showtaboptions",
"accelerator": "CmdOrCtrl+S",
Expand Down

0 comments on commit 52e58ea

Please sign in to comment.