Skip to content

Commit

Permalink
fix some bug🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaobaishushu25 committed Nov 10, 2023
1 parent 28daab6 commit 0b8c177
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .idea/KotlinSceneConfigState.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/main/kotlin/xbss/config/AppVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ package xbss.config
* 0.5.9 -- 2023年10月22日20:50:28 :解决了bug10.
* 0.6.0 -- 2023年11月8日13:16:18:终端增加了对按键ESC的支持(Vi模式常用);终端界面支持中文输入。
* 0.6.0.1 -- 2023年11月10日16:45:39:终端界面支持中文输入后有bug,暂时退回。
* 0.6.1 -- 2023年11月10日21:38:07:创建文件夹失败会给出提示。
*
*
*
*/
object AppVersion {
const val VERSION = "0.6.0.1"
const val VERSION = "0.6.1"
}
1 change: 1 addition & 0 deletions src/main/kotlin/xbss/config/ImageIcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ object ImageIcon {
val PASTE16 = Image(Image::class.java.getResourceAsStream("/img/paste16.png"))
val DONATE24 = Image(Image::class.java.getResourceAsStream("/img/donate24.png"))
val PAY = Image(Image::class.java.getResourceAsStream("/img/pay.png"))
val NEW24 = Image(Image::class.java.getResourceAsStream("/img/new24.png"))
}
1 change: 1 addition & 0 deletions src/main/kotlin/xbss/controller/FileCommandTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class FileCommandTask(private val ssh: SSH,private val mainWindow: MainWindow) {
when(type){
FileCommandPane.FileCommandType.DELETE -> mainWindow.treeArea.refreshFatherItem()
FileCommandPane.FileCommandType.COPY -> mainWindow.treeArea.refreshItem()
FileCommandPane.FileCommandType.CREATE -> TODO()
}
mainWindow.writeInfoLog("${javaClass.simpleName}完成")
}
Expand Down
32 changes: 25 additions & 7 deletions src/main/kotlin/xbss/ssh/SSH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,47 +159,64 @@ open class SSH(val account: Account) {
fun releaseChannel(channel: ChannelSftp) {
chSftpPool.releaseChannel(channel)
}
fun execCommand(command:String):String{
//todo :释放的有没有问题? https://blog.csdn.net/weixin_43898952/article/details/119967566
/**执行单个命令的结果,返回exitCode和message
* exitCode为0表示成功,其他值(大部分是1)表示失败
*/
data class ExecResponse(val exitCode: Int, val message: String)

// fun execCommand(command:String):String{
fun execCommand(command: String): ExecResponse {
// println("执行命令 $command")
val result = StringBuilder()
var exitStatus = 0 //退出码
execChannel = session.openChannel("exec") as ChannelExec
execChannel.setCommand(command)
execChannel.inputStream = null
// 错误信息输出流,用于输出错误的信息,当exitstatus<0的时候
execChannel.setErrStream(System.err)
// execChannel.setErrStream(System.err)

try {
// 执行命令,等待执行结果
execChannel.connect()
}catch (e:Exception){
println("发生异常$e")
isConnectProperty.value = false
execChannel.disconnect()
close()
return ""
// return ""
return ExecResponse(1, "")
}

// 获取命令执行结果
val inputStream = execChannel.inputStream
val errStream = execChannel.errStream
// 错误信息输出流,用于输出错误的信息,当exitstatus<0的时候
/**
* 通过channel获取信息的方式,采用官方Demo代码
*/
val tmp = ByteArray(1024)
while (true) {
// println("还在循环"+execChannel.isClosed+" "+execChannel.isConnected)
while (inputStream.available() > 0) {
val i: Int = inputStream.read(tmp, 0, 1024)
if (i < 0) {
break
}
result.append(String(tmp, 0, i))
}
while (errStream.available() > 0) {
val i: Int = errStream.read(tmp, 0, 1024)
if (i < 0) {
break
}
result.append(String(tmp, 0, i))
}
// 从channel获取全部信息之后,channel会自动关闭
if (execChannel.isClosed) {
if (inputStream.available() > 0) {
continue
}
exitStatus = execChannel.exitStatus
exitStatus = execChannel.exitStatus//正常是0 错误是1
execChannel.disconnect()
break
}
}
Expand Down Expand Up @@ -256,7 +273,8 @@ open class SSH(val account: Account) {
// isConnectProperty.value = false
// close()
// }
return result.toString()
// return result.toString()
return ExecResponse(exitStatus, result.toString())
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/main/kotlin/xbss/utils/FileCommandPane.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ class FileCommandPane(
private lateinit var statusListener: ChangeListener<Number>

enum class FileCommandType {
COPY, DELETE
COPY, DELETE, CREATE,
}
init {
describe = when (type) {
FileCommandType.COPY -> "复制"
FileCommandType.DELETE -> "删除"
FileCommandType.CREATE -> "创建"
}
initView()
}
Expand Down Expand Up @@ -98,6 +99,11 @@ class FileCommandPane(
imageView.image = ImageIcon.DELETE24
title = "删除${totalNum}个文件"
}

FileCommandType.CREATE -> {
imageView.image = ImageIcon.NEW24
title = "创建${totalNum}$sumTitle"
}
}
titleHBox = HBox(10.0, imageView, Label(title).apply {
style = "-fx-font-size: 13"
Expand Down Expand Up @@ -147,7 +153,10 @@ class FileCommandPane(
HBox(10.0, ImageView(ImageIcon.CLOSE24), Label("${describe}出错!").apply { style = "-fx-font-size:19px" }),
Label().apply {
textProperty().bind(name.map { "($it)" })
translateX = 40.0
// translateX = 40.0
tooltip = Tooltip(this.text).apply {
style = "-fx-background-color: #FFFACD;-fx-text-fill:black;-fx-font-size: 13"
}
})
edArea.translateX = 30.0
centerBox.children.clear()
Expand Down
22 changes: 17 additions & 5 deletions src/main/kotlin/xbss/utils/SystemData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import javafx.application.Platform
import javafx.geometry.Insets
import javafx.scene.layout.HBox
import xbss.MainAPP
import xbss.config.GlobalLog
import xbss.ssh.SSH
import java.util.*
import kotlin.concurrent.timerTask
Expand Down Expand Up @@ -70,7 +71,7 @@ class SystemData(private val ssh: SSH):HBox() {
//// this.children.add(s)
// }
}
val gpuNum = ssh.execCommand("nvidia-smi --list-gpus | wc -l").replace("\n", "").toInt()
val gpuNum = ssh.execCommand("nvidia-smi --list-gpus | wc -l").message.replace("\n", "").toInt()
// Platform.runLater {
// for (i in 0 until gpuNum){
// val messageBar = MessageBar("GPU${i}", MessageBar.Type.GPU)
Expand Down Expand Up @@ -132,13 +133,16 @@ class SystemData(private val ssh: SSH):HBox() {
private fun getTimerTask():TimerTask{
return timerTask {
dataList.clear()
val cpuResult = ssh.execCommand("top -b -n 1 | grep \"Cpu(s)\" | awk '{print \$2+\$4 \"%\"}'").replace("\n", "")
val cpuResult =
ssh.execCommand("top -b -n 1 | grep \"Cpu(s)\" | awk '{print \$2+\$4 \"%\"}'").message.replace("\n", "")
//28.2%
dataList.add(Pair(cpuResult.removeSuffix("%").toDouble(),""))
//7.7G 2.4G
//15G 643M
//这里有可能是两个单位,需要判断换算
val memoryResult = ssh.execCommand("free -h | awk 'NR==2{print \$2,\$3}'").replace("\n", "").replace("\r", "").split(" ")
val memoryResult =
ssh.execCommand("free -h | awk 'NR==2{print \$2,\$3}'").message.replace("\n", "").replace("\r", "")
.split(" ")
val memoryPer = if (memoryResult[1].last()=='M'){
val value1 = memoryResult[1].removeSuffix("M")
compute(value1.toDouble().div(1024).reserveOne(),memoryResult[0].removeSuffix("G"))
Expand All @@ -148,7 +152,11 @@ class SystemData(private val ssh: SSH):HBox() {
// val memoryPer = compute(memoryResult[1].removeSuffix("G"),memoryResult[0].removeSuffix("G"))

dataList.add(Pair(memoryPer,"${memoryResult[1]}/${memoryResult[0]}"))
val gpuResult = ssh.execCommand("nvidia-smi --query-gpu=index,memory.used,memory.total --format=csv | tail -n+2").replace(" ","").split("\n")
val gpuResult =
ssh.execCommand("nvidia-smi --query-gpu=index,memory.used,memory.total --format=csv | tail -n+2").message.replace(
" ",
""
).split("\n")
// 3, 21177 MiB, 24576 MiB

for (s in gpuResult){
Expand All @@ -161,7 +169,11 @@ class SystemData(private val ssh: SSH):HBox() {
}
Platform.runLater {
for ((index,value) in uiList.withIndex()){
value.update(dataList[index].first,dataList[index].second)
try {
value.update(dataList[index].first, dataList[index].second)
} catch (e: Exception) {
GlobalLog.writeErrorLog("${e} ${dataList} index是${index}")
}
}
}
}
Expand Down
38 changes: 36 additions & 2 deletions src/main/kotlin/xbss/utils/TreeAreaRightMenu.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package xbss.utils

import javafx.beans.property.ReadOnlyStringProperty
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleStringProperty
import javafx.geometry.Pos
import javafx.scene.Node
import javafx.scene.control.*
Expand All @@ -13,6 +16,7 @@ import javafx.stage.Stage
import xbss.config.FileType
import xbss.config.ImageIcon
import xbss.config.Setting
import xbss.view.MainWindow
import xbss.view.TreeArea
import java.io.File

Expand Down Expand Up @@ -156,8 +160,38 @@ class TreeAreaRightMenu(private val treeArea: TreeArea): ContextMenu() {
PopName().apply {
textObservable.addListener { _,_,newValue ->
if (newValue.isNotEmpty()){
ssh.execCommand("mkdir "+item!!.path+"/"+newValue)
treeArea.refreshItem()

val status = SimpleIntegerProperty(0)
val name = SimpleStringProperty("")
treeArea.mainWindow.addMessage(
FileCommandPane(
FileCommandPane.FileCommandType.CREATE,
"创建文件夹",
name,
SimpleIntegerProperty(1),
1,
status
)
)
val result = ssh.execCommand("mkdir " + item!!.path + "/" + newValue)
if (result.exitCode == 0) {
name.value = newValue
status.value = 4
treeArea.refreshItem()
} else {
name.value = result.message
status.value = 5
}
// else{
// treeArea.mainWindow.addMessage(FileCommandPane(
// FileCommandPane.FileCommandType.CREATE,
// "创建文件夹",
// SimpleStringProperty(newValue),
// SimpleIntegerProperty(1),
// 1,
// SimpleIntegerProperty(result.exitCode)
// ))
// }
}
}
start(Stage())
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/xbss/view/TreeArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import java.io.ByteArrayInputStream
* @version 1.0
* @describe
*/
class TreeArea(mainWindow: MainWindow,val taskHandler: FileTaskHandler,private var defaultPath:String = "/"): VBox() {
class TreeArea(val mainWindow: MainWindow, val taskHandler: FileTaskHandler, private var defaultPath: String = "/") :
VBox() {
val ssh = mainWindow.ssh

/**
Expand Down
Binary file added src/main/resources/img/new24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0b8c177

Please sign in to comment.