Skip to content

Commit

Permalink
feat: add commas fuck
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Oct 8, 2022
1 parent 60c5cdd commit 4d71e8a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
7 changes: 7 additions & 0 deletions addons/cli/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ where <command> is one of:
},
})

commas.context.provide('cli.command', {
command: 'fuck',
handler({ sender }) {
sender.send('fuck')
},
})

commas.context.provide('cli.command', {
command: 'trick',
handler({ sender }) {
Expand Down
12 changes: 12 additions & 0 deletions src/renderer/compositions/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,18 @@ export function handleTerminalMessages() {
if (!currentTerminal) return
executeTerminalTab(currentTerminal, command, true)
})
ipcRenderer.on('fuck', () => {
if (!currentTerminal) return
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!currentTerminal.addons.shellIntegration) return
const actions = currentTerminal.addons.shellIntegration.getQuickFixActions()
if (!actions) return
if (actions.length === 1) {
executeTerminalTab(currentTerminal, actions[0].command)
} else {
currentTerminal.addons.shellIntegration.triggerQuickFixMenu()
}
})
}

export function loadTerminalAddons(tab: TerminalTab) {
Expand Down
60 changes: 42 additions & 18 deletions src/renderer/utils/shell-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface IntegratedShellCommand {
exitCode?: number,
marker: IMarker,
decoration: IDecoration,
actions?: IntegratedShellCommandAction[],
}

function updateDecorationElement(decoration: IDecoration, callback: (el: HTMLElement) => void) {
Expand Down Expand Up @@ -51,23 +52,9 @@ export class ShellIntegrationAddon implements ITerminalAddon {
case 'B': {
// PromptEnd
const marker = xterm.registerMarker()!
let actions: IntegratedShellCommandAction[] | undefined
const lastCommand = this.commands.length
? this.commands[this.commands.length - 1]
: undefined
if (lastCommand?.command && lastCommand.exitCode) {
const lastCommandLine = lastCommand.marker.line
let lastOutput = ''
// TODO: use actual command start
for (let line = lastCommandLine + 1; line < marker.line; line += 1) {
const bufferLine = xterm.buffer.active.getLine(line)
if (bufferLine) {
lastOutput += (bufferLine.isWrapped || !lastOutput ? '' : '\n')
+ bufferLine.translateToString(true)
}
}
actions = this._getQuickFixActions(lastCommand.command, lastOutput)
}
const actions = this.currentCommand
? this.currentCommand.actions
: this._generateQuickFixActions(marker)
const theme = useTheme()
const decoration = this._createCommandDecoration(
xterm,
Expand All @@ -83,6 +70,7 @@ export class ShellIntegrationAddon implements ITerminalAddon {
this.currentCommand = {
marker,
decoration,
actions,
}
this.commands.push(this.currentCommand)
this.recentMarker = undefined
Expand Down Expand Up @@ -247,7 +235,7 @@ export class ShellIntegrationAddon implements ITerminalAddon {
this.scrollToMarker(targetMarker)
}

_getQuickFixActions(command: string, output: string) {
_getQuickFixActionsByOutput(command: string, output: string) {
// Git push for upstream
const gitUpstreamMatches = output.match(/git push --set-upstream origin (\S+)/)
if (gitUpstreamMatches && /\bgit\b/.test(command)) {
Expand Down Expand Up @@ -288,4 +276,40 @@ export class ShellIntegrationAddon implements ITerminalAddon {
}
}

_generateQuickFixActions(marker: IMarker) {
const { xterm } = this.tab
const lastCommand = this.commands.length
? this.commands[this.commands.length - 1]
: undefined
if (lastCommand?.command && lastCommand.exitCode) {
const lastCommandLine = lastCommand.marker.line
let lastOutput = ''
// TODO: use actual command start
for (let line = lastCommandLine + 1; line < marker.line; line += 1) {
const bufferLine = xterm.buffer.active.getLine(line)
if (bufferLine) {
lastOutput += (bufferLine.isWrapped || !lastOutput ? '' : '\n')
+ bufferLine.translateToString(true)
}
}
return this._getQuickFixActionsByOutput(lastCommand.command, lastOutput)
}
}

getQuickFixActions() {
return this.currentCommand?.actions
}

triggerQuickFixMenu() {
if (!this.currentCommand?.actions) return
const el = this.currentCommand.decoration.element
if (!el) return
const rect = el.getBoundingClientRect()
const event = new MouseEvent('click', {
clientX: rect.left,
clientY: rect.top,
})
el.dispatchEvent(event)
}

}

0 comments on commit 4d71e8a

Please sign in to comment.