Feature: Replace old eks configuration with NeoLang

This commit is contained in:
zt515 2017-08-09 21:09:17 +08:00
parent b974533ee3
commit 19d5be2a4c
11 changed files with 137 additions and 57 deletions

View File

@ -0,0 +1,28 @@
extra-key: {
version: 19
program: [ default ]
key: [
{
code: "-"
},
{
code: "/"
},
{
code: "\\"
},
{
code: "|"
},
{
code: "$"
},
{
code: "<"
},
{
code: ">"
}
]
}

View File

@ -0,0 +1,24 @@
extra-key: {
version: 19
with-default: true
program: [ vim, vi, neovim ]
key: [
{
code: "dd"
with-enter: true
},
{
code: ":x"
with-enter: true
},
{
code: ":w"
with-enter: true
},
{
code: ":q"
with-enter: true
}
]
}

View File

@ -6,6 +6,7 @@ import io.neoterm.R
import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.frontend.service.NeoService
import io.neoterm.utils.AssetsUtils
import io.neoterm.utils.FileUtils
import io.neoterm.view.TerminalView
import io.neoterm.view.eks.ExtraKeysView
@ -31,16 +32,7 @@ class ColorSchemeService : NeoService {
private fun extractDefaultColor(context: Context): Boolean {
try {
val assets = context.assets
assets.list("colors")
.map { File(NeoTermPath.COLORS_PATH, it) }
.takeWhile { !it.exists() }
.forEach {
val file = it
assets.open("colors/${file.name}").use {
FileUtils.writeFile(file, it)
}
}
AssetsUtils.extractAssetsDir(context, "colors", NeoTermPath.COLORS_PATH)
return true
} catch (e: Exception) {
return false

View File

@ -1,14 +1,24 @@
package io.neoterm.customize.eks
import android.content.Context
import io.neoterm.App
import io.neoterm.frontend.logging.NLog
import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.frontend.service.NeoService
import io.neoterm.utils.AssetsUtils
import io.neoterm.view.eks.ExtraKeysView
import java.io.File
import java.io.FileFilter
/**
* @author kiva
*/
class ExtraKeysService : NeoService {
companion object {
private val FILTER = FileFilter {
it.extension == "nl"
}
}
override fun onServiceInit() {
checkForFiles()
}
@ -20,41 +30,56 @@ class ExtraKeysService : NeoService {
checkForFiles()
}
val EKS_KEYS: MutableMap<String, IExtraKey> = mutableMapOf()
val extraKeys: MutableMap<String, NeoExtraKey> = mutableMapOf()
fun showShortcutKeys(program: String, extraKeysView: ExtraKeysView?) {
if (extraKeysView == null) {
return
}
if (this.EKS_KEYS.containsKey(program)) {
val shortcutKey = EKS_KEYS[program]
shortcutKey?.applyShortcutKeys(extraKeysView)
val extraKey = extraKeys[program]
if (extraKey != null) {
extraKey.applyExtraKeys(extraKeysView)
return
}
extraKeysView.loadDefaultUserKeys()
}
fun registerShortcutKeys(program: String, eksKey: IExtraKey?) {
if (eksKey == null) {
if (this.EKS_KEYS.containsKey(program)) {
this.EKS_KEYS.remove(program)
}
return
fun registerShortcutKeys(extraKey: NeoExtraKey) {
extraKey.programNames.forEach {
extraKeys[it] = extraKey
}
this.EKS_KEYS[program] = eksKey
}
private fun checkForFiles() {
// TODO: Builtin keys
File(NeoTermPath.EKS_PATH).mkdirs()
val defaultFile = File(NeoTermPath.EKS_DEFAULT_FILE)
if (!defaultFile.exists()) {
extractDefaultConfig(App.get())
}
loadConfigure()
}
private fun extractDefaultConfig(context: Context) {
try {
AssetsUtils.extractAssetsDir(context, "eks", NeoTermPath.EKS_PATH)
} catch (e: Exception) {
NLog.e("ExtraKey", "Failed to extract configure: ${e.localizedMessage}")
}
}
private fun loadConfigure() {
val configDir = File(NeoTermPath.EKS_PATH)
configDir.mkdirs()
// TODO: Load configure.
configDir.listFiles(FILTER).forEach {
if (it.absolutePath != NeoTermPath.EKS_DEFAULT_FILE) {
val extraKey = NeoExtraKey()
if (extraKey.loadConfigure(it)) {
registerShortcutKeys(extraKey)
}
}
}
}
}

View File

@ -1,10 +0,0 @@
package io.neoterm.customize.eks
import io.neoterm.view.eks.ExtraKeysView
/**
* @author kiva
*/
interface IExtraKey {
fun applyShortcutKeys(extraKeysView: ExtraKeysView)
}

View File

@ -4,6 +4,7 @@ import io.neolang.visitor.ConfigVisitor
import io.neoterm.customize.config.ConfigureService
import io.neoterm.frontend.logging.NLog
import io.neoterm.frontend.service.ServiceManager
import io.neoterm.view.eks.ExtraKeysView
import io.neoterm.view.eks.button.IExtraButton
import io.neoterm.view.eks.button.TextButton
import java.io.File
@ -31,6 +32,15 @@ class NeoExtraKey {
val shortcutKeys: MutableList<IExtraButton> = mutableListOf()
var withDefaultKeys: Boolean = true
fun applyExtraKeys(extraKeysView: ExtraKeysView) {
if (withDefaultKeys) {
extraKeysView.loadDefaultUserKeys()
}
for (button in shortcutKeys) {
extraKeysView.addUserKey(button)
}
}
fun loadConfigure(file: File): Boolean {
val loaderService = ServiceManager.getService<ConfigureService>()
val configure = loaderService.newLoader(file).loadConfigure()

View File

@ -7,6 +7,7 @@ import io.neoterm.R
import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.frontend.service.NeoService
import io.neoterm.utils.AssetsUtils
import io.neoterm.utils.FileUtils
import java.io.File
@ -85,15 +86,7 @@ class FontService : NeoService {
private fun extractDefaultFont(context: Context): Boolean {
try {
val assets = context.assets
assets.list("fonts")
.map { File(NeoTermPath.FONT_PATH, it) }
.takeWhile { !it.exists() }
.forEach { file ->
assets.open("fonts/${file.name}").use {
FileUtils.writeFile(file, it)
}
}
AssetsUtils.extractAssetsDir(context, "fonts", NeoTermPath.FONT_PATH)
return true
} catch (e: Exception) {
return false

View File

@ -16,7 +16,7 @@ object NeoTermPath {
const val CUSTOM_PATH = "$HOME_PATH/.neoterm"
const val NEOTERM_SHELL_PATH = "$CUSTOM_PATH/shell"
const val EKS_PATH = "$CUSTOM_PATH/eks"
const val EKS_DEFAULT_FILE = "$EKS_PATH/default.eks"
const val EKS_DEFAULT_FILE = "$EKS_PATH/default.nl"
const val FONT_PATH = "$CUSTOM_PATH/font"
const val COLORS_PATH = "$CUSTOM_PATH/color"
const val USER_SCRIPT_PATH = "$CUSTOM_PATH/script"

View File

@ -0,0 +1,21 @@
package io.neoterm.utils
import android.content.Context
import java.io.File
/**
* @author kiva
*/
object AssetsUtils {
fun extractAssetsDir(context: Context, dirName: String, extractDir: String) {
val assets = context.assets
assets.list(dirName)
.map { File(extractDir, it) }
.takeWhile { !it.exists() }
.forEach { file ->
assets.open("$dirName/${file.name}").use {
FileUtils.writeFile(file, it)
}
}
}
}

View File

@ -7,13 +7,16 @@ import android.view.*
import android.widget.GridLayout
import android.widget.LinearLayout
import io.neoterm.R
import io.neoterm.customize.eks.NeoExtraKey
import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.ui.term.event.ToggleImeEvent
import io.neoterm.view.eks.button.ControlButton
import io.neoterm.view.eks.button.IExtraButton
import io.neoterm.view.eks.button.StatedControlButton
import io.neoterm.view.eks.impl.ArrowButton
import org.greenrobot.eventbus.EventBus
import java.io.File
class ExtraKeysView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
@ -112,18 +115,11 @@ class ExtraKeysView(context: Context, attrs: AttributeSet) : LinearLayout(contex
}
fun loadDefaultUserKeys() {
// val defaultFile = ExtraKeysUtils.getDefaultFile()
clearUserKeys()
// TODO: loadDefaultUserKeys
// try {
// val parser = ExtraKeyConfigParser()
// parser.setInput(defaultFile)
// val config = parser.parse()
// userKeys.addAll(config.shortcutKeys)
// } catch (e: Exception) {
// e.printStackTrace()
// }
val defaultConfig = NeoExtraKey()
if (defaultConfig.loadConfigure(File(NeoTermPath.EKS_DEFAULT_FILE))) {
userKeys.addAll(defaultConfig.shortcutKeys)
}
}
fun updateButtons() {

View File

@ -1,5 +1,6 @@
package io.neoterm
import io.neolang.main.Main
import io.neolang.visitor.ConfigVisitor
import io.neoterm.customize.color.NeoColorScheme
import io.neoterm.customize.config.ConfigureService
@ -48,7 +49,7 @@ class ConfigureFileTest {
}
val extraKey = NeoExtraKey()
if (extraKey.loadConfigure(File("NeoLang/example/extra-key.nl"))) {
if (extraKey.loadConfigure(File("/Users/kiva/Documents/NeoTerm/app/src/main/assets/eks/vim.nl"))) {
println("programs: ${extraKey.programNames}")
println("version: ${extraKey.version}")
println("with-default: ${extraKey.withDefaultKeys}")