antosdk-apps/vTerm/main.coffee

159 lines
5.5 KiB
CoffeeScript
Raw Permalink Normal View History

2020-08-06 23:21:42 +02:00
# Copyright 2017-2018 Xuan Sang LE <xsang.le AT gmail DOT com>
# AnTOS Web desktop is is licensed under the GNU General Public
# License v3.0, see the LICENCE file for more information
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
#along with this program. If not, see https://www.gnu.org/licenses/
class vTerm extends this.OS.application.BaseApplication
constructor: (args) ->
super "vTerm", args
main: () ->
@mterm = @find "myterm"
2023-01-04 09:55:18 +01:00
@term = new Terminal { cursorBlink: true, fontSize: 12 }
2020-08-06 23:21:42 +02:00
@fitAddon = new FitAddon.FitAddon()
@term.loadAddon(@fitAddon)
@term.open @mterm
2020-08-08 21:41:33 +02:00
@sub = undefined
2021-03-17 21:34:27 +01:00
@bindKey "CTRL-SHIFT-C", (e) =>
@mctxHandle {id: "copy"}
@term.focus()
@bindKey "CTRL-SHIFT-V", (e) =>
@mctxHandle {id: "paste"}
2020-08-06 23:21:42 +02:00
@term.onKey (d) =>
return unless @sub
@sub.send Antunnel.Msg.DATA, new TextEncoder("utf-8").encode(d.key)
@on "focus", () => @term.focus()
@mterm.contextmenuHandle = (e, m) =>
m.items = [
{ text: "__(Copy)", id: "copy" },
{ text: "__(Paste)", id: "paste"}
]
m.onmenuselect = (e) =>
return unless e
@mctxHandle e.data.item.data
m.show e
@resizeContent()
# make desktop menu if not exist
@systemsetting.desktop.menu[@name] = { text: "__(Open terminal)", app: "vTerm" } unless @systemsetting.desktop.menu[@name]
@on "resize", (e) =>
2020-08-06 23:21:42 +02:00
@resizeContent()
2020-08-08 21:41:33 +02:00
2020-08-08 19:40:21 +02:00
checklib = () =>
if not Antunnel.tunnel
@_gui.pushService("Antunnel/AntunnelService")
.then (d) =>
return unless @systemsetting.system.tunnel_uri
Antunnel.init(@systemsetting.system.tunnel_uri).then (t) =>
@notify __("Tunnel now connected to the server at: {0}", @systemsetting.system.tunnel_uri)
@tunnel = Antunnel.tunnel
@openSession()
.catch (e) =>
Antunnel.tunnel.close() if Antunnel.tunnel
@error __("Unable to connect to the tunnel: {0}", e.toString()), e
@quit()
2020-08-08 19:40:21 +02:00
.catch (e) =>
@error __("Unable to run Antunnel service: {0}",e.toString()), e
@quit()
2020-08-08 19:40:21 +02:00
else
@tunnel = Antunnel.tunnel
@openSession()
2020-08-08 21:41:33 +02:00
if not window.Antunnel
2020-08-08 19:40:21 +02:00
@_api.requires("pkg://Antunnel/main.js").then () =>
checklib()
2020-08-06 23:21:42 +02:00
.catch (e) =>
2020-08-08 19:40:21 +02:00
@error __("Unable to load Antunnel: {0}",e.toString()), e
@quit()
2020-08-06 23:21:42 +02:00
else
2020-08-08 19:40:21 +02:00
checklib()
2020-08-06 23:21:42 +02:00
mctxHandle: (data) ->
switch data.id
when "paste"
2020-11-29 23:10:42 +01:00
cb = (text) =>
2020-08-06 23:21:42 +02:00
return unless text and text isnt ""
2020-11-29 23:10:42 +01:00
text = text.replace /\n/g, "\r"
2020-08-06 23:21:42 +02:00
@sub.send Antunnel.Msg.DATA, new TextEncoder("utf-8").encode(text) if @sub
2020-09-15 12:40:06 +02:00
@term.focus()
2020-11-29 23:10:42 +01:00
@_api.getClipboard()
.then (text) =>
cb(text)
.catch (e) =>
@error __("Unable to paste"), e
#ask for user to enter the text manually
@openDialog("TextDialog", { title: "Paste text"})
.then (text) =>
cb(text)
.catch (err) => @error err.toString(), err
2020-08-06 23:21:42 +02:00
when "copy"
text = @term.getSelection()
return unless text and text isnt ""
@_api.setClipboard text
else
resizeContent: () ->
@fitAddon.fit()
ncol = @term.cols
nrow = @term.rows
return unless @sub
arr = new Uint8Array(8)
2021-11-27 17:42:20 +01:00
arr.set Antunnel.Msg.bytes_of(ncol,4), 0
arr.set Antunnel.Msg.bytes_of(nrow,4), 4
2020-08-06 23:21:42 +02:00
@sub.send Antunnel.Msg.CTRL, arr
openSession: () ->
@term.clear()
@term.focus()
@sub = new Antunnel.Subscriber("vterm")
@sub.onopen = () =>
console.log("Subscribed")
@resizeContent (($ @mterm).width()) , (($ @mterm).height())
@term.focus()
@sub.onerror = (e) =>
@error __("Unable to connect to: vterm"), e
@sub = undefined
@sub.onmessage = (e) =>
@term.write(new TextDecoder("utf-8").decode(e.data)) if @term and e.data
@sub.onclose = () =>
@sub = undefined
@notify __("Terminal connection closed")
@quit()
@tunnel.subscribe @sub
cleanup: (e) ->
@sub.close() if @sub
2020-12-18 21:52:44 +01:00
vTerm.dependencies = [
"pkg://xTerm/main.js",
"pkg://xTerm/main.css",
"pkg://Antunnel/main.js"
]
2020-08-06 23:21:42 +02:00
this.OS.register "vTerm", vTerm