-
Notifications
You must be signed in to change notification settings - Fork 395
Live terminal output #1347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Live terminal output #1347
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f8e42da
Add live terminal output
pythongosssss 5e4aa12
Fix scrolling
pythongosssss 7291ca1
Refactor loading
pythongosssss 3236c98
Fallback to polling if endpoint fails
pythongosssss fbeb3a2
Comment
pythongosssss 8deb66d
Move clientId to executionStore
pythongosssss a212595
Merge remote-tracking branch 'origin/main' into live-terminal-output
pythongosssss f3d3d85
Remove polling
pythongosssss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
144 changes: 102 additions & 42 deletions
144
src/components/bottomPanel/tabs/IntegratedTerminal.vue
This file contains hidden or 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 |
|---|---|---|
| @@ -1,61 +1,121 @@ | ||
| <template> | ||
| <div class="p-terminal rounded-none h-full w-full"> | ||
| <ScrollPanel class="h-full w-full" ref="scrollPanelRef"> | ||
| <pre class="px-4 whitespace-pre-wrap">{{ log }}</pre> | ||
| </ScrollPanel> | ||
| <div class="relative h-full w-full bg-black"> | ||
| <ProgressSpinner | ||
| v-if="loading" | ||
| class="absolute inset-0 flex justify-center items-center h-full z-10" | ||
| /> | ||
| <div class="p-terminal rounded-none h-full w-full p-2"> | ||
| <div class="h-full" ref="terminalEl"></div> | ||
| </div> | ||
| </div> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import ScrollPanel from 'primevue/scrollpanel' | ||
| import { api } from '@/scripts/api' | ||
| import { onBeforeUnmount, onMounted, ref, watch } from 'vue' | ||
| import '@xterm/xterm/css/xterm.css' | ||
| import { Terminal } from '@xterm/xterm' | ||
| import { FitAddon } from '@xterm/addon-fit' | ||
| import { api, LogEntry, TerminalSize } from '@/scripts/api' | ||
| import { onMounted, onUnmounted, ref } from 'vue' | ||
| import { debounce } from 'lodash' | ||
| import ProgressSpinner from 'primevue/progressspinner' | ||
|
|
||
| const log = ref<string>('') | ||
| const scrollPanelRef = ref<InstanceType<typeof ScrollPanel> | null>(null) | ||
| /** | ||
| * Whether the user has scrolled to the bottom of the terminal. | ||
| * This is used to prevent the terminal from scrolling to the bottom | ||
| * when new logs are fetched. | ||
| */ | ||
| const scrolledToBottom = ref(false) | ||
| let intervalId: number | ||
| let useFallbackPolling: boolean = false | ||
| const loading = ref(true) | ||
| const terminalEl = ref<HTMLDivElement>() | ||
| const fitAddon = new FitAddon() | ||
| const terminal = new Terminal({ | ||
| convertEol: true | ||
| }) | ||
| terminal.loadAddon(fitAddon) | ||
|
|
||
| let intervalId: number = 0 | ||
| const resizeTerminal = () => | ||
| terminal.resize(terminal.cols, fitAddon.proposeDimensions().rows) | ||
|
|
||
| onMounted(async () => { | ||
| const element = scrollPanelRef.value?.$el | ||
| const scrollContainer = element?.querySelector('.p-scrollpanel-content') | ||
|
|
||
| if (scrollContainer) { | ||
| scrollContainer.addEventListener('scroll', () => { | ||
| scrolledToBottom.value = | ||
| scrollContainer.scrollTop + scrollContainer.clientHeight === | ||
| scrollContainer.scrollHeight | ||
| }) | ||
| const resizeObserver = new ResizeObserver(debounce(resizeTerminal, 50)) | ||
|
|
||
| const update = (entries: Array<LogEntry>, size?: TerminalSize) => { | ||
| if (size) { | ||
| terminal.resize(size.cols, fitAddon.proposeDimensions().rows) | ||
| } | ||
| terminal.write(entries.map((e) => e.m).join('')) | ||
| } | ||
|
|
||
| const scrollToBottom = () => { | ||
| if (scrollContainer) { | ||
| scrollContainer.scrollTop = scrollContainer.scrollHeight | ||
| } | ||
| const logReceived = ( | ||
| e: CustomEvent<{ | ||
| entries: Array<{ t: string; m: string }> | ||
| size: { | ||
| rows: number | ||
| cols: number | ||
| } | null | ||
| }> | ||
| ) => { | ||
| update(e.detail.entries, e.detail.size) | ||
| } | ||
|
|
||
| const loadLogText = async () => { | ||
| // Fallback to using string logs | ||
| const logs = await api.getLogs() | ||
| terminal.clear() | ||
| terminal.write(logs) | ||
| fitAddon.fit() | ||
| } | ||
|
|
||
| const loadLogEntries = async () => { | ||
| const logs = await api.getRawLogs() | ||
| update(logs.entries, logs.size) | ||
| } | ||
|
|
||
| const watchLogs = async () => { | ||
| if (useFallbackPolling) { | ||
| intervalId = window.setInterval(loadLogText, 500) | ||
| } else { | ||
| // It is possible for a user to open the terminal before a clientid is assigned | ||
| // subscribe requires this so wait for it | ||
| await api.waitForClientId() | ||
| api.subscribeLogs(true) | ||
| api.addEventListener('logs', logReceived) | ||
| } | ||
| } | ||
|
|
||
| watch(log, () => { | ||
| if (scrolledToBottom.value) { | ||
| scrollToBottom() | ||
| } | ||
| }) | ||
| onMounted(async () => { | ||
| terminal.open(terminalEl.value) | ||
|
|
||
| const fetchLogs = async () => { | ||
| log.value = await api.getLogs() | ||
| try { | ||
| await loadLogEntries() | ||
| } catch { | ||
| // On older backends the endpoints wont exist, fallback to poll | ||
| useFallbackPolling = true | ||
| await loadLogText() | ||
| } | ||
|
|
||
| await fetchLogs() | ||
| scrollToBottom() | ||
| intervalId = window.setInterval(fetchLogs, 500) | ||
| loading.value = false | ||
| resizeObserver.observe(terminalEl.value) | ||
|
|
||
| await watchLogs() | ||
| }) | ||
|
|
||
| onBeforeUnmount(() => { | ||
| window.clearInterval(intervalId) | ||
| onUnmounted(() => { | ||
| if (useFallbackPolling) { | ||
| window.clearInterval(intervalId) | ||
| } else { | ||
| if (api.clientId) { | ||
| api.subscribeLogs(false) | ||
| } | ||
| api.removeEventListener('logs', logReceived) | ||
| } | ||
|
|
||
| resizeObserver.disconnect() | ||
| }) | ||
| </script> | ||
|
|
||
| <style> | ||
pythongosssss marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .p-terminal .xterm { | ||
| overflow-x: auto; | ||
| } | ||
|
|
||
| .p-terminal .xterm-screen { | ||
| background-color: black; | ||
| overflow-y: hidden; | ||
| } | ||
| </style> | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.