开始

最近在研究mc服务器,一般启动时使用bat文件就可以,但是我想在终端中运行而不是在cmd中。
查了wt和pwsh的文档写出了下面的脚本。作用仅仅是在wt中运行java这条命令。
脚本包括一些变量,其中:

  • reuse表示是否打开一个新的wt实例。
  • headC表示在调用jar前运行的powershell命令。
  • javaC中可以修改为合适的jvm参数。

导入的Path脚本作用和nodejs中的Path作用相似,这里使用了join。

#Requires AutoHotkey v2.0

#Include G:\AHK\gitee_ahk2\common\Path.ahk

title := '"MC_Server-1.19.4"'
server_type := 'vanilla'
tab_color := "'#f59218'"
reuse := false

jarPath := Path.Join(A_ScriptDir, 'server.jar')
headC := 'echo start_' title '_vanilla. ' '&& Get-Date'
startC := 'java -Xms1g -Xmx1g -jar ' jarPath ' --nogui'

cmd := 'wt'
  . (reuse ? ' -w 1' : ' -w -1')
  . ' new-tab --title ' title ' --tabColor ' tab_color ' --startingDirectory ' A_ScriptDir
  . ' pwsh -NoExit -c ' headC '&&' startC

Run cmd

2024/08/03补充

在前段时间,我写了一个脚本来实现wt的简单调用,这样就不用手动拼接字符串了。

位于仓库:ahk-lib/util/wrRunner.ahk

#Requires AutoHotkey v2.0

#Include ..\Extend.ahk

/**
 * 详细参数参阅:https://learn.microsoft.com/zh-cn/windows/terminal/command-line-arguments?tabs=windows
 */

/**@example
 * WtRunner.Builder()
 *   .Window(-1)
 *   .NewTab('Test', , '#97d1f3', A_ScriptDir, 'echo Hello_WtRunner')
 *   .SplitPane('sp1', WtRunner.Profiles.GitBash, , , '#c68bc6')
 *   .SplitPane('sp2', WtRunner.Profiles.GitBash, '-H', , '#9fcac8', , 'echo SplitPane2!')
 *   .Build()
 *   .RunCmd()
 */
class WtRunner {

  static Profiles := {
    Default: 'Command Prompt',
    GitBash: 'Git Bash',
    PowerShell7: 'PowerShell7',
    Cmd: 'Command Prompt'
  }

  cmd := 'wt'

  __New(_builder) { ; do conact
    if IsTrue(w := _builder.globalParam.Window)
      this.cmd .= ' -w ' w
    if IsTrue(size := _builder.globalParam.size)
      this.cmd .= ' --size ' size[1] ',' size[2]
    if IsTrue(pos := _builder.globalParam.pos)
      this.cmd .= ' --pos ' pos[1] ',' pos[2]
    if IsTrue(_builder.globalParam.focus)
      this.cmd .= ' --focus '
    this.cmd .= ' ' _builder.tabs.Join(';')
  }
  ; Min Max Hide
  RunCmd(flag?) => Run(this.cmd, , flag?)

  class Builder {

    globalParam := { window: '', size: '', pos: '', focus: false }, tabs := []

    Chain(exp) => this
    ; -1 new | 0 last
    Window(idOrName) => this.Chain(this.globalParam.window := idOrName)
    Size(r, c) => this.Chain(this.globalParam.size := [r, c])
    Pos(x, y) => this.Chain(this.globalParam.pos := [x, y])
    Focus(v) => this.Chain(this.globalParam.focus := v)
    NewTab(title, profile?, tabColor?, startDirectory?, command?, preCmd := ' pwsh -NoExit -c ') {
      _cmd := 'nt --title "' title '"'
      if IsSet(profile)
        _cmd .= ' --profile "' profile '"'
      if IsSet(tabColor)
        _cmd .= ' --tabColor ' tabColor ''
      if IsSet(startDirectory)
        _cmd .= ' -d "' startDirectory '"'
      if IsSet(command)
        _cmd .= preCmd command
      this.tabs.Push(_cmd)
      return this
    }
    ; The param with 'f' prefix specify that it is a flag, means that without value
    SplitPane(title, profile?, fDirection := '-V', size := 0.5, tabColor?, startDirectory?, command?, preCmd := ' pwsh -NoExit -c ') {
      _cmd := 'sp --title "' title '"'
      if IsSet(profile)
        _cmd .= ' --profile "' profile '"'
      if IsSet(tabColor)
        _cmd .= ' --tabColor ' tabColor ''
      if IsSet(startDirectory)
        _cmd .= ' -d "' startDirectory '"'
      _cmd .= ' ' fDirection ' --size ' size
      if IsSet(command)
        _cmd .= preCmd command
      this.tabs.Push(_cmd)
      return this
    }

    Build() => WtRunner(this)
  }
}
posted on 2024-01-22 14:54  落寞的雪  阅读(49)  评论(0编辑  收藏  举报