From 437879248c687fa7d78feb4ebed9ba3ab888673f Mon Sep 17 00:00:00 2001 From: zt515 Date: Thu, 17 Aug 2017 02:45:14 +0800 Subject: [PATCH] UI: Improve UserScript --- app/src/main/assets/scripts/extract-archive | 57 +++++++++++++++++++ app/src/main/assets/scripts/open-in-vim | 12 ++++ .../component/script/UserScriptComponent.kt | 35 ++++++++++-- .../ui/customize/ColorSchemeActivity.kt | 1 + .../main/res/layout/ui_user_script_list.xml | 16 +++--- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 7 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 app/src/main/assets/scripts/extract-archive create mode 100644 app/src/main/assets/scripts/open-in-vim diff --git a/app/src/main/assets/scripts/extract-archive b/app/src/main/assets/scripts/extract-archive new file mode 100644 index 00000000..adc8443b --- /dev/null +++ b/app/src/main/assets/scripts/extract-archive @@ -0,0 +1,57 @@ +#!/data/data/io.neoterm/files/usr/bin/bash + +function file_suffix() { + echo "${1##*.}" +} + +function detect_program() { + case "$1" in + *.tar.* | *.tar ) echo "tar xvf %s" ;; + *.7z ) echo "7za x %s" ;; + *.rar ) echo "unrar x %s" ;; + *.zip ) echo "unzip %s" ;; + * ) echo "" ;; + esac +} + +function do_extract() { + local file="$1" + local dir="$(dirname $file)" + + if [[ ! -f "$file" ]]; then + echo "$file: no such file or directory" + return 1 + fi + + local program="$(detect_program $file)" + + if [[ "$program" == "" ]]; then + echo "Unsupported format: $(file_suffix $file)" + return 1 + fi + + local command="$(printf "$program" "$file")" + + if [[ ! -w "$dir" || ! -r "$file" ]]; then + command="sudo $command" + fi + + cd "$dir" || { + echo "Failed to cd: $dir" + return 1 + } + + eval "$command" +} + +if [[ "$#" == 0 ]]; then + echo "You must specific at least a file to extract." + exit 1 +fi + +clear +while [[ "$#" != 0 ]]; do + file="$1"; shift + echo "[Extracting] $(basename $file)" + do_extract "$file" +done diff --git a/app/src/main/assets/scripts/open-in-vim b/app/src/main/assets/scripts/open-in-vim new file mode 100644 index 00000000..d80b0c97 --- /dev/null +++ b/app/src/main/assets/scripts/open-in-vim @@ -0,0 +1,12 @@ +#!/data/data/io.neoterm/files/usr/bin/bash + +set -e + +VIM="$(which vim)" + +if [[ "$VIM"x == ""x ]]; then + echo "Vim is not installed, now installing..." + apt update && apt install -y vim +fi + +$VIM "$@" diff --git a/app/src/main/java/io/neoterm/component/script/UserScriptComponent.kt b/app/src/main/java/io/neoterm/component/script/UserScriptComponent.kt index 1a0b8940..cf4bbcd0 100644 --- a/app/src/main/java/io/neoterm/component/script/UserScriptComponent.kt +++ b/app/src/main/java/io/neoterm/component/script/UserScriptComponent.kt @@ -1,27 +1,52 @@ package io.neoterm.component.script +import android.content.Context +import android.system.Os +import io.neoterm.App import io.neoterm.frontend.component.NeoComponent +import io.neoterm.frontend.logging.NLog import io.neoterm.frontend.preference.NeoTermPath +import io.neoterm.utils.AssetsUtils import java.io.File /** * @author kiva */ class UserScriptComponent : NeoComponent { + lateinit var userScripts: MutableList + override fun onServiceInit() { - userScripts = mutableListOf() - reloadScripts() + checkForFiles() } override fun onServiceDestroy() { } override fun onServiceObtained() { - reloadScripts() + checkForFiles() } - lateinit var userScripts: MutableList - private set + private fun extractDefaultScript(context: Context): Boolean { + try { + AssetsUtils.extractAssetsDir(context, "scripts", NeoTermPath.USER_SCRIPT_PATH) + File(NeoTermPath.USER_SCRIPT_PATH) + .listFiles().forEach { + Os.chmod(it.absolutePath, 448 /*Dec of 0700*/) + } + return true + } catch (e: Exception) { + NLog.e("UserScript", "Failed to extract default user scripts: ${e.localizedMessage}") + return false + } + } + + private fun checkForFiles() { + File(NeoTermPath.USER_SCRIPT_PATH).mkdirs() + userScripts = mutableListOf() + + extractDefaultScript(App.get()) + reloadScripts() + } fun reloadScripts() { val userScriptDir = File(NeoTermPath.USER_SCRIPT_PATH) diff --git a/app/src/main/java/io/neoterm/ui/customize/ColorSchemeActivity.kt b/app/src/main/java/io/neoterm/ui/customize/ColorSchemeActivity.kt index 79a199e9..6905e7a3 100644 --- a/app/src/main/java/io/neoterm/ui/customize/ColorSchemeActivity.kt +++ b/app/src/main/java/io/neoterm/ui/customize/ColorSchemeActivity.kt @@ -154,6 +154,7 @@ class ColorSchemeActivity : BaseCustomizeActivity() { } else { try { colorSchemeComponent.saveColorScheme(colorScheme) + colorSchemeComponent.reloadColorSchemes() colorSchemeComponent.setCurrentColorScheme(colorScheme) changed = false diff --git a/app/src/main/res/layout/ui_user_script_list.xml b/app/src/main/res/layout/ui_user_script_list.xml index 50755208..829470c3 100644 --- a/app/src/main/res/layout/ui_user_script_list.xml +++ b/app/src/main/res/layout/ui_user_script_list.xml @@ -3,13 +3,12 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="@dimen/text_margin" - android:orientation="horizontal" - android:baselineAligned="false"> + android:baselineAligned="false" + android:orientation="vertical"> @@ -26,9 +25,9 @@ @@ -42,7 +41,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> - \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index b1d2546a..a785d027 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -84,7 +84,7 @@ 退出 在此处打开终端 用户脚本 - 没有找到可用的用户脚本 + 没有找到可用的用户脚本 (是否忘记设置可执行权限?) 没有文件被选择 处理的文件 可用的用户脚本 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 11ac632a..a1356bc1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -87,7 +87,7 @@ Term Here User Script Command Shortcut - No user script found + No user script found (Did you forget to set executable permissions?) No files selected Files to Handle Available User Scripts