2017-07-01 00:09:23 +08:00
|
|
|
package io.neoterm.utils
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import io.neoterm.R
|
|
|
|
import io.neoterm.backend.TerminalSession
|
2017-07-02 23:07:56 +08:00
|
|
|
import io.neoterm.customize.font.FontManager
|
2017-07-21 18:07:58 +08:00
|
|
|
import io.neoterm.frontend.ShellParameter
|
|
|
|
import io.neoterm.frontend.ShellTermSession
|
2017-08-03 00:37:49 +08:00
|
|
|
import io.neoterm.frontend.service.ServiceManager
|
2017-07-01 00:09:23 +08:00
|
|
|
import io.neoterm.preference.NeoPreference
|
2017-07-26 00:26:08 +08:00
|
|
|
import io.neoterm.view.eks.ExtraKeysView
|
2017-07-02 23:07:56 +08:00
|
|
|
import io.neoterm.view.TerminalView
|
2017-07-21 00:42:30 +08:00
|
|
|
import io.neoterm.view.TerminalViewClient
|
2017-07-01 00:09:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author kiva
|
|
|
|
*/
|
|
|
|
object TerminalUtils {
|
2017-07-21 00:42:30 +08:00
|
|
|
fun setupTerminalView(terminalView: TerminalView?, terminalViewClient: TerminalViewClient? = null) {
|
2017-07-03 17:05:42 +08:00
|
|
|
terminalView?.textSize = NeoPreference.loadInt(NeoPreference.KEY_FONT_SIZE, 30)
|
2017-08-03 00:37:49 +08:00
|
|
|
terminalView?.setTypeface(ServiceManager.getService<FontManager>().getCurrentFont().getTypeFace())
|
2017-07-02 23:07:56 +08:00
|
|
|
if (terminalViewClient != null) {
|
2017-07-18 00:08:08 +08:00
|
|
|
terminalView?.setTerminalViewClient(terminalViewClient)
|
2017-07-02 23:07:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 17:05:42 +08:00
|
|
|
fun setupExtraKeysView(extraKeysView: ExtraKeysView?) {
|
2017-08-03 00:37:49 +08:00
|
|
|
extraKeysView?.setTypeface(ServiceManager.getService<FontManager>().getCurrentFont().getTypeFace())
|
2017-07-03 17:05:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fun setupTerminalSession(session: TerminalSession?) {
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:07:58 +08:00
|
|
|
fun createShellSession(context: Context, parameter: ShellParameter): TerminalSession {
|
|
|
|
val initCommand = parameter.initialCommand ?:
|
2017-07-21 00:42:30 +08:00
|
|
|
NeoPreference.loadString(R.string.key_general_initial_command, "")
|
2017-07-18 23:55:01 +08:00
|
|
|
|
2017-07-21 00:42:30 +08:00
|
|
|
val session = ShellTermSession.Builder()
|
2017-07-21 18:07:58 +08:00
|
|
|
.executablePath(parameter.executablePath)
|
|
|
|
.currentWorkingDirectory(parameter.cwd)
|
|
|
|
.callback(parameter.sessionCallback)
|
|
|
|
.systemShell(parameter.systemShell)
|
|
|
|
.envArray(parameter.env)
|
|
|
|
.argArray(parameter.arguments)
|
2017-07-21 00:42:30 +08:00
|
|
|
.create(context)
|
2017-07-03 17:05:42 +08:00
|
|
|
setupTerminalSession(session)
|
2017-07-21 00:42:30 +08:00
|
|
|
session.initialCommand = initCommand
|
2017-07-01 00:09:23 +08:00
|
|
|
return session
|
|
|
|
}
|
2017-07-18 23:55:01 +08:00
|
|
|
|
|
|
|
fun escapeString(s: String?): String {
|
|
|
|
if (s == null) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
val builder = StringBuilder()
|
|
|
|
val specialChars = "\"\\$`!"
|
|
|
|
builder.append('"')
|
|
|
|
val length = s.length
|
|
|
|
for (i in 0..length - 1) {
|
|
|
|
val c = s[i]
|
|
|
|
if (specialChars.indexOf(c) >= 0) {
|
|
|
|
builder.append('\\')
|
|
|
|
}
|
|
|
|
builder.append(c)
|
|
|
|
}
|
|
|
|
builder.append('"')
|
|
|
|
return builder.toString()
|
|
|
|
}
|
2017-07-01 00:09:23 +08:00
|
|
|
}
|