Skip to content

Commit

Permalink
UI: Improve UserScript
Browse files Browse the repository at this point in the history
  • Loading branch information
imkiva committed Aug 16, 2017
1 parent f6cfe6d commit 4378792
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 16 deletions.
57 changes: 57 additions & 0 deletions app/src/main/assets/scripts/extract-archive
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions app/src/main/assets/scripts/open-in-vim
Original file line number Diff line number Diff line change
@@ -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 "$@"
Original file line number Diff line number Diff line change
@@ -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<UserScript>

override fun onServiceInit() {
userScripts = mutableListOf()
reloadScripts()
checkForFiles()
}

override fun onServiceDestroy() {
}

override fun onServiceObtained() {
reloadScripts()
checkForFiles()
}

lateinit var userScripts: MutableList<UserScript>
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class ColorSchemeActivity : BaseCustomizeActivity() {
} else {
try {
colorSchemeComponent.saveColorScheme(colorScheme)
colorSchemeComponent.reloadColorSchemes()
colorSchemeComponent.setCurrentColorScheme(colorScheme)
changed = false

Expand Down
16 changes: 7 additions & 9 deletions app/src/main/res/layout/ui_user_script_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/text_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:orientation="vertical">

Expand All @@ -26,9 +25,9 @@
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/text_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/text_margin"
android:layout_weight="1.0"
android:orientation="vertical">

Expand All @@ -42,7 +41,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />


</LinearLayout>

</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<string name="exit">退出</string>
<string name="term_here">在此处打开终端</string>
<string name="user_script">用户脚本</string>
<string name="no_user_script_found">没有找到可用的用户脚本</string>
<string name="no_user_script_found">没有找到可用的用户脚本 (是否忘记设置可执行权限?)</string>
<string name="no_files_selected">没有文件被选择</string>
<string name="files_to_handle">处理的文件</string>
<string name="available_user_scripts">可用的用户脚本</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<string name="term_here">Term Here</string>
<string name="user_script">User Script</string>
<string name="command_shortcut">Command Shortcut</string>
<string name="no_user_script_found">No user script found</string>
<string name="no_user_script_found">No user script found (Did you forget to set executable permissions?)</string>
<string name="no_files_selected">No files selected</string>
<string name="files_to_handle">Files to Handle</string>
<string name="available_user_scripts">Available User Scripts</string>
Expand Down

0 comments on commit 4378792

Please sign in to comment.