Skip to content

Commit ace0c97

Browse files
committed
Update
1 parent 2feceaa commit ace0c97

5 files changed

Lines changed: 109 additions & 7 deletions

File tree

app/src/main/assets/home/etc/tool-tree.bash

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fi
6060
}
6161

6262
shell_bash() {
63-
echo '<group><editor title="'$home_text_5'" desc="'$more_text_9'" file="home/usr/run_'$1'.bash" placeholder="#!/data/data/com.tool.tree/files/home/bin/bash" icon="'`urlpng shell`'"/></group>'
63+
echo '<group><editor title="'$home_text_5'" desc="'$more_text_9'" file="home/usr/run_'$1'.bash" need-input="true" placeholder="#!/data/data/com.tool.tree/files/home/bin/bash" icon="'`urlpng shell`'"/></group>'
6464
}
6565

6666

@@ -361,7 +361,7 @@ fi
361361
</group>
362362
363363
<group>
364-
<editor title="'$home_text_5'" desc="'$home_text_6'" file="home/usr/run_shella.bash" placeholder="#!/data/data/com.tool.tree/files/home/bin/bash" icon="'`urlpng shella`'"/>
364+
<editor title="'$home_text_5'" desc="'$home_text_6'" file="home/usr/run_shella.bash" need-input="true" placeholder="#!/data/data/com.tool.tree/files/home/bin/bash" icon="'`urlpng shella`'"/>
365365
</group>'
366366
}
367367

@@ -925,7 +925,7 @@ checktime
925925
<group>
926926
<page icon="'`urlpng generate`'" config-sh="$ETC/tool-tree.bash Generate">
927927
<title>'$synthetic_text'</title>
928-
<option type="checkbox" box="glog hide_show_generate" silent="true" id="v1" auto-off="true" reload="true">'$folder_text' ROM</option>
928+
<option type="checkbox" box="glog hide_show_generate" silent="true" id="v1" auto-off="true" reload="true">'$input_folder_text'</option>
929929
<handler>
930930
if [ "$menu_id" == "v1" ]; then
931931
[ "$(glog hide_show_generate)" == 1 ] && slog hide_show_generate 0 || slog hide_show_generate 1

app/src/main/java/com/omarea/krscript/config/PageConfigReader.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,15 @@ class PageConfigReader {
680680
}
681681
}
682682

683-
// Đọc thuộc tính riêng của thẻ <editor file="" title="" desc="" wrap="" />
683+
// Đọc thuộc tính riêng của thẻ
684+
// <editor file="" title="" desc="" wrap="" readonly="" need-input="" value="" value-sh="" />
685+
// - readonly="true": chỉ xem, không cho sửa/lưu
686+
// - need-input="true": khi chạy thử script (nút Run) mà script có gọi lệnh `read` để chờ
687+
// nhập dữ liệu, giống hệt thuộc tính need-input của thẻ <action> -> hiện ô nhập liệu
688+
// trong lúc script đang chạy
689+
// - value / value-sh: nội dung khởi tạo, CHỈ áp dụng khi file chưa tồn tại (tạo file mới
690+
// và điền nội dung này vào); nếu file đã tồn tại thì giữ nguyên, không điền gì thêm.
691+
// value-sh (nếu có) được ưu tiên hơn value (kết quả script sẽ được dùng làm nội dung).
684692
private fun editorNode(editor: EditorNode, parser: XmlPullParser): EditorNode {
685693
for (attrIndex in 0 until parser.attributeCount) {
686694
val attrName = parser.getAttributeName(attrIndex)
@@ -689,6 +697,10 @@ class PageConfigReader {
689697
"file", "path" -> editor.file = attrValue.trim()
690698
"wrap" -> editor.wrap = !(attrValue == "0" || attrValue == "false" || attrValue == "off" || attrValue == "no-wrap")
691699
"placeholder" -> editor.placeholder = attrValue
700+
"readonly" -> editor.readonly = (attrValue == "true" || attrValue == "1")
701+
"need-input" -> editor.needInput = (attrValue == "true" || attrValue == "1")
702+
"value-sh" -> editor.valueSh = attrValue
703+
"value" -> editor.value = attrValue
692704
}
693705
}
694706
return editor
@@ -1304,6 +1316,10 @@ class PageConfigReader {
13041316
tomlGet(table, "file", "path")?.let { editor.file = it.trim() }
13051317
tomlGet(table, "wrap")?.let { editor.wrap = !(it == "0" || it == "false" || it == "off" || it == "no-wrap") }
13061318
tomlGet(table, "placeholder")?.let { editor.placeholder = it }
1319+
tomlGet(table, "readonly")?.let { editor.readonly = (it == "true" || it == "1") }
1320+
tomlGet(table, "need-input")?.let { editor.needInput = (it == "true" || it == "1") }
1321+
tomlGet(table, "value-sh")?.let { editor.valueSh = it }
1322+
tomlGet(table, "value")?.let { editor.value = it }
13071323
return editor
13081324
}
13091325

app/src/main/java/com/omarea/krscript/model/EditorNode.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,20 @@ class EditorNode(currentConfigXml: String) : ClickableNode(currentConfigXml) {
1313

1414
// Trạng thái ngắt dòng mặc định khi mở trang soạn thảo (mặc định: bật ngắt dòng)
1515
var wrap: Boolean = true
16+
17+
// Chỉ đọc: true thì không cho phép chỉnh sửa/lưu nội dung (chỉ xem)
18+
var readonly: Boolean = false
19+
20+
// Khi bấm nút Run để chạy thử script đang soạn thảo: true nghĩa là script có gọi lệnh
21+
// `read` để chờ người dùng nhập dữ liệu qua bàn phím trong lúc chạy (giống thuộc tính
22+
// need-input của thẻ <action>) -> ứng dụng sẽ hiện ô nhập liệu trong lúc thực thi.
23+
var needInput: Boolean = false
24+
25+
// Nội dung khởi tạo (script): kết quả trả về sẽ được dùng làm nội dung ban đầu
26+
// nếu file chưa tồn tại. Ưu tiên cao hơn "value" nếu cả hai đều có.
27+
var valueSh: String = ""
28+
29+
// Nội dung khởi tạo (tĩnh): chỉ được điền vào khi file CHƯA tồn tại.
30+
// Nếu file đã tồn tại thì giữ nguyên nội dung hiện có, không điền gì thêm.
31+
var value: String = ""
1632
}

app/src/main/java/com/omarea/krscript/ui/ActionListFragment.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ class ActionListFragment : androidx.fragment.app.Fragment(), PageLayoutRender.On
215215
Toast.makeText(context, getString(R.string.editor_file_missing), Toast.LENGTH_SHORT).show()
216216
return
217217
}
218-
com.tool.tree.TextEditorActivity.start(context, item.file, item.title, item.desc, item.wrap, item.pageConfigDir, item.placeholder)
218+
com.tool.tree.TextEditorActivity.start(
219+
context, item.file, item.title, item.desc, item.wrap, item.pageConfigDir, item.placeholder,
220+
item.readonly, item.needInput, item.value, item.valueSh
221+
)
219222
onCompleted.run()
220223
}
221224
}

app/src/main/java/com/tool/tree/TextEditorActivity.kt

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class TextEditorActivity : AppCompatActivity() {
5353
private const val EXTRA_WRAP = "wrap"
5454
private const val EXTRA_DIR = "dir"
5555
private const val EXTRA_PLACEHOLDER = "placeholder"
56+
private const val EXTRA_READONLY = "readonly"
57+
private const val EXTRA_NEED_INPUT = "need_input"
58+
private const val EXTRA_VALUE = "value"
59+
private const val EXTRA_VALUE_SH = "value_sh"
5660

5761
// Tối ưu RAM: Giảm Undo Stack từ 200 xuống 50 để tránh OutOfMemory
5862
private const val UNDO_HISTORY_LIMIT = 50
@@ -85,7 +89,11 @@ class TextEditorActivity : AppCompatActivity() {
8589
desc: String? = null,
8690
wrap: Boolean = true,
8791
dir: String? = null,
88-
placeholder: String? = null
92+
placeholder: String? = null,
93+
readonly: Boolean = false,
94+
needInput: Boolean = false,
95+
value: String? = null,
96+
valueSh: String? = null
8997
) {
9098
val intent = Intent(context, TextEditorActivity::class.java).apply {
9199
putExtra(EXTRA_FILE, file)
@@ -94,6 +102,10 @@ class TextEditorActivity : AppCompatActivity() {
94102
putExtra(EXTRA_WRAP, wrap)
95103
putExtra(EXTRA_DIR, dir ?: "")
96104
putExtra(EXTRA_PLACEHOLDER, placeholder ?: "")
105+
putExtra(EXTRA_READONLY, readonly)
106+
putExtra(EXTRA_NEED_INPUT, needInput)
107+
putExtra(EXTRA_VALUE, value ?: "")
108+
putExtra(EXTRA_VALUE_SH, valueSh ?: "")
97109
if (context !is AppCompatActivity) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
98110
}
99111
context.startActivity(intent)
@@ -113,6 +125,10 @@ class TextEditorActivity : AppCompatActivity() {
113125
private var syntaxHighlighter: SyntaxHighlighter? = null
114126
private var placeholderText: String = ""
115127
private var titleText: String = ""
128+
private var readonlyMode: Boolean = false
129+
private var needInput: Boolean = false
130+
private var initialValue: String = ""
131+
private var initialValueSh: String = ""
116132
private var lastLanguageOverride: String? = null
117133
private var lastLineCount = -1
118134

@@ -166,12 +182,17 @@ class TextEditorActivity : AppCompatActivity() {
166182
wrapEnabled = intent.getBooleanExtra(EXTRA_WRAP, true)
167183
placeholderText = intent.getStringExtra(EXTRA_PLACEHOLDER).orEmpty()
168184
titleText = intent.getStringExtra(EXTRA_TITLE).orEmpty()
185+
readonlyMode = intent.getBooleanExtra(EXTRA_READONLY, false)
186+
needInput = intent.getBooleanExtra(EXTRA_NEED_INPUT, false)
187+
initialValue = intent.getStringExtra(EXTRA_VALUE).orEmpty()
188+
initialValueSh = intent.getStringExtra(EXTRA_VALUE_SH).orEmpty()
169189

170190
applyWrapState()
171191
applyMonospaceState()
172192
setupUnifiedTextWatcher()
173193
setupSpecialCharsBar()
174194
setupEditorTouchAndFocus()
195+
applyReadonlyState()
175196
loadFileContent()
176197
}
177198

@@ -616,6 +637,31 @@ class TextEditorActivity : AppCompatActivity() {
616637
} catch (_: Exception) {
617638
}
618639

640+
// Chỉ điền nội dung khởi tạo (value / value-sh) khi file CHƯA tồn tại.
641+
// Nếu file đã tồn tại thì giữ nguyên, không điền/ghi đè gì thêm.
642+
if (newFile) {
643+
val computedValue = if (initialValueSh.isNotEmpty()) {
644+
try {
645+
com.omarea.krscript.executor.ScriptEnvironmen.executeResultRoot(
646+
applicationContext,
647+
initialValueSh,
648+
com.omarea.krscript.model.NodeInfoBase("")
649+
)
650+
} catch (_: Exception) {
651+
""
652+
}
653+
} else {
654+
initialValue
655+
}
656+
657+
if (computedValue.isNotEmpty()) {
658+
content = computedValue
659+
if (writeFileContent(absoluteFilePath, content)) {
660+
newFile = false
661+
}
662+
}
663+
}
664+
619665
withContext(Dispatchers.Main) {
620666
savedContent = content
621667
isApplyingHistory = true
@@ -644,6 +690,15 @@ class TextEditorActivity : AppCompatActivity() {
644690
}
645691
}
646692

693+
// Áp dụng trạng thái chỉ đọc (readonly="true"): không cho phép gõ/sửa/dán nội dung
694+
private fun applyReadonlyState() {
695+
if (!readonlyMode) return
696+
binding.editorContent.keyListener = null
697+
binding.editorContent.isFocusable = true
698+
binding.editorContent.isFocusableInTouchMode = true
699+
binding.editorContent.isLongClickable = true
700+
}
701+
647702
private fun applyWrapState() {
648703
val editText = binding.editorContent
649704
val container = binding.editorContentContainer
@@ -705,6 +760,10 @@ class TextEditorActivity : AppCompatActivity() {
705760

706761
private fun attemptClose() {
707762
if (isSaving) return
763+
if (readonlyMode) {
764+
finish()
765+
return
766+
}
708767
commitPendingUndoSnapshot()
709768

710769
if (!hasUnsavedChanges()) {
@@ -738,7 +797,13 @@ class TextEditorActivity : AppCompatActivity() {
738797

739798
menu.findItem(R.id.editor_menu_wrap)?.isChecked = wrapEnabled
740799
menu.findItem(R.id.editor_menu_monospace)?.isChecked = monospaceEnabled
741-
menu.findItem(R.id.editor_menu_run)?.isVisible = runnableInterpreter() != null
800+
menu.findItem(R.id.editor_menu_run)?.isVisible = !readonlyMode && runnableInterpreter() != null
801+
802+
if (readonlyMode) {
803+
menu.findItem(R.id.editor_menu_save)?.isVisible = false
804+
menu.findItem(R.id.editor_menu_undo)?.isVisible = false
805+
menu.findItem(R.id.editor_menu_redo)?.isVisible = false
806+
}
742807

743808
refreshToolbarButtons()
744809
return true
@@ -787,6 +852,7 @@ class TextEditorActivity : AppCompatActivity() {
787852
onResult: ((Boolean) -> Unit)? = null
788853
) {
789854
if (isSaving || !hasUnsavedChanges()) return
855+
if (readonlyMode) return
790856
isSaving = true
791857
commitPendingUndoSnapshot()
792858

@@ -869,6 +935,7 @@ class TextEditorActivity : AppCompatActivity() {
869935
title = titleText.ifEmpty { File(absoluteFilePath).name }
870936
interruptable = true
871937
shell = RunnableNode.shellModeDefault
938+
needInput = this@TextEditorActivity.needInput
872939
}
873940
val script = "chmod 755 \"$absoluteFilePath\" 2>/dev/null\n$interpreter \"$absoluteFilePath\"\n"
874941
DialogLogFragment.create(

0 commit comments

Comments
 (0)