Profile: Support profile name

This commit is contained in:
zt515 2017-12-28 00:08:00 +08:00
parent ccaf6048cb
commit c4b7f06b7c
3 changed files with 31 additions and 18 deletions

View File

@ -1,3 +1,4 @@
profile-shell: {
name: "Simple Profile"
bell: true
}

View File

@ -1,5 +1,6 @@
package io.neoterm.component.profile
import io.neolang.visitor.ConfigVisitor
import io.neoterm.component.codegen.CodeGenParameter
import io.neoterm.component.codegen.generators.NeoProfileGenerator
import io.neoterm.component.codegen.interfaces.CodeGenObject
@ -10,9 +11,37 @@ import io.neoterm.frontend.component.helper.ConfigFileBasedObject
* @author kiva
*/
abstract class NeoProfile : CodeGenObject, ConfigFileBasedObject {
companion object {
private const val PROFILE_NAME = "name"
}
abstract val profileMetaName: String
private val profileMetaPath
get() = arrayOf(profileMetaName)
var profileName = "Unknown Profile"
override fun onConfigLoaded(configVisitor: ConfigVisitor) {
profileName = configVisitor.getProfileString(PROFILE_NAME, profileName)
}
override fun getCodeGenerator(parameter: CodeGenParameter): CodeGenerator {
return NeoProfileGenerator(parameter)
}
protected fun ConfigVisitor.getProfileString(key: String, fallback: String): String {
return getProfileString(key) ?: fallback
}
protected fun ConfigVisitor.getProfileBoolean(key: String, fallback: Boolean): Boolean {
return getProfileBoolean(key) ?: fallback
}
protected fun ConfigVisitor.getProfileString(key: String): String? {
return this.getStringValue(profileMetaPath, key)
}
protected fun ConfigVisitor.getProfileBoolean(key: String): Boolean? {
return this.getBooleanValue(profileMetaPath, key)
}
}

View File

@ -27,8 +27,6 @@ class ShellProfile : NeoProfile() {
private const val FONT = "font"
private const val COLOR_SCHEME = "color-scheme"
private const val WORD_BASED_IME = "word-based-ime"
private val PROFILE_META_PATH = arrayOf(PROFILE_META_NAME)
}
override val profileMetaName = PROFILE_META_NAME
@ -68,6 +66,7 @@ class ShellProfile : NeoProfile() {
}
override fun onConfigLoaded(configVisitor: ConfigVisitor) {
super.onConfigLoaded(configVisitor)
loginShell = configVisitor.getProfileString(LOGIN_SHELL, loginShell)
initialCommand = configVisitor.getProfileString(INITIAL_COMMAND, initialCommand)
enableBell = configVisitor.getProfileBoolean(BELL, enableBell)
@ -81,20 +80,4 @@ class ShellProfile : NeoProfile() {
profileFont = configVisitor.getProfileString(FONT, profileFont)
profileColorScheme = configVisitor.getProfileString(COLOR_SCHEME, profileColorScheme)
}
private fun ConfigVisitor.getProfileString(key: String, fallback: String): String {
return getProfileString(key) ?: fallback
}
private fun ConfigVisitor.getProfileBoolean(key: String, fallback: Boolean): Boolean {
return getProfileBoolean(key) ?: fallback
}
private fun ConfigVisitor.getProfileString(key: String): String? {
return this.getStringValue(PROFILE_META_PATH, key)
}
private fun ConfigVisitor.getProfileBoolean(key: String): Boolean? {
return this.getBooleanValue(PROFILE_META_PATH, key)
}
}