2017-07-01 00:09:23 +08:00
|
|
|
package io.neoterm.utils
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.widget.Toast
|
|
|
|
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-01 00:09:23 +08:00
|
|
|
import io.neoterm.preference.NeoPreference
|
2017-07-18 23:55:01 +08:00
|
|
|
import io.neoterm.preference.NeoTermPath
|
2017-07-02 23:07:56 +08:00
|
|
|
import io.neoterm.view.BasicViewClient
|
2017-07-03 17:05:42 +08:00
|
|
|
import io.neoterm.view.ExtraKeysView
|
2017-07-02 23:07:56 +08:00
|
|
|
import io.neoterm.view.TerminalView
|
2017-07-01 00:09:23 +08:00
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author kiva
|
|
|
|
*/
|
|
|
|
object TerminalUtils {
|
2017-07-03 17:05:42 +08:00
|
|
|
fun setupTerminalView(terminalView: TerminalView?, terminalViewClient: BasicViewClient? = null) {
|
|
|
|
terminalView?.textSize = NeoPreference.loadInt(NeoPreference.KEY_FONT_SIZE, 30)
|
|
|
|
terminalView?.setTypeface(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?) {
|
|
|
|
extraKeysView?.setTypeface(FontManager.getCurrentFont().getTypeFace())
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setupTerminalSession(session: TerminalSession?) {
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:55:01 +08:00
|
|
|
fun createSession(context: Context, executablePath: String?, arguments: Array<String>?,
|
|
|
|
cwd: String?, initialCommand: String?, env: Array<String>?,
|
|
|
|
sessionCallback: TerminalSession.SessionChangedCallback?,
|
|
|
|
systemShell: Boolean): TerminalSession {
|
|
|
|
|
2017-07-01 00:09:23 +08:00
|
|
|
var executablePath = executablePath
|
|
|
|
var arguments = arguments
|
2017-07-18 23:55:01 +08:00
|
|
|
var initialCommand = initialCommand
|
2017-07-01 00:09:23 +08:00
|
|
|
var cwd = cwd
|
2017-07-18 23:55:01 +08:00
|
|
|
|
2017-07-01 00:09:23 +08:00
|
|
|
if (cwd == null) {
|
|
|
|
cwd = NeoTermPath.HOME_PATH
|
|
|
|
}
|
|
|
|
|
|
|
|
if (executablePath == null) {
|
|
|
|
executablePath = if (systemShell)
|
|
|
|
"/system/bin/sh"
|
|
|
|
else
|
|
|
|
NeoTermPath.USR_PATH + "/bin/" + NeoPreference.loadString(R.string.key_general_shell, "sh")
|
|
|
|
|
|
|
|
if (!File(executablePath).exists()) {
|
|
|
|
Toast.makeText(context, context.getString(R.string.shell_not_found, executablePath), Toast.LENGTH_LONG).show()
|
|
|
|
executablePath = NeoTermPath.USR_PATH + "/bin/sh"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments == null) {
|
|
|
|
arguments = arrayOf<String>(executablePath)
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:55:01 +08:00
|
|
|
if (initialCommand == null) {
|
|
|
|
initialCommand = NeoPreference.loadString(R.string.key_general_initial_command, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
val session = TerminalSession(executablePath, cwd, initialCommand, arguments,
|
2017-07-01 00:09:23 +08:00
|
|
|
env ?: NeoPreference.buildEnvironment(cwd, systemShell, executablePath),
|
|
|
|
sessionCallback)
|
2017-07-03 17:05:42 +08:00
|
|
|
setupTerminalSession(session)
|
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
|
|
|
}
|