Swift命令行工具当前窗口大小 上下文

Swift命令行工具当前窗口大小 上下文,swift,bash,terminal,command-line-interface,Swift,Bash,Terminal,Command Line Interface,我正在尝试编写一个交互式swift命令行工具。其中一个关键部分是有一个“writeToScreen”函数,该函数将一系列页眉、页脚和正文作为参数,并将它们很好地格式化为终端窗口的当前大小,将溢出折叠为一个“列表”选项。所以函数应该是这样的: func writeToScreen(_ headers: [String], _ footers: [String], _ body: String) { let (terminalWindowRows, terminalWindowCols) =

我正在尝试编写一个交互式swift命令行工具。其中一个关键部分是有一个“writeToScreen”函数,该函数将一系列页眉、页脚和正文作为参数,并将它们很好地格式化为终端窗口的当前大小,将溢出折叠为一个“列表”选项。所以函数应该是这样的:

func writeToScreen(_ headers: [String], _ footers: [String], _ body: String) {
    let (terminalWindowRows, terminalWindowCols) = getCurrentScreenSize()
    // perform formatting within this window size...
}

func getCurrentScreenSize() -> (Int, Int) {
    // perform some bash script like tput lines and tput cols and return result
}
22x7
_ _ _ _ _ _ _ _ _ _ _ _
|(1) h1, (2) list...  |
|                     |
| body...             |
|                     |
|                     |
|                     |
|(3) list...          |
_ _ _ _ _ _ _ _ _ _ _ _

28x7
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|(1) h1, (2) h2, (3) list...|
|                           |
| body...                   |
|                           |
|                           |
|                           |
|(4) longf1, (5) list...    |
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _
例如,
writeToScreen([“h1”、“h2”]、[“longf1”、“longf2”]、“body…”)等输入将为相应的屏幕大小生成以下内容:

func writeToScreen(_ headers: [String], _ footers: [String], _ body: String) {
    let (terminalWindowRows, terminalWindowCols) = getCurrentScreenSize()
    // perform formatting within this window size...
}

func getCurrentScreenSize() -> (Int, Int) {
    // perform some bash script like tput lines and tput cols and return result
}
22x7
_ _ _ _ _ _ _ _ _ _ _ _
|(1) h1, (2) list...  |
|                     |
| body...             |
|                     |
|                     |
|                     |
|(3) list...          |
_ _ _ _ _ _ _ _ _ _ _ _

28x7
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|(1) h1, (2) h2, (3) list...|
|                           |
| body...                   |
|                           |
|                           |
|                           |
|(4) longf1, (5) list...    |
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _
问题 我遇到的问题是,要获得终端窗口的大小,我需要至少运行一个bash脚本,比如
echo“$(tput cols)-(tput lines)”
,它将屏幕大小输出为
-
。但是,在swift中运行bash脚本需要使用
Process()
NSTask()
,在我能找到的每个用例中,它总是一个单独的进程,因此只返回默认的终端大小,而不考虑当前会话窗口的大小

我试过使用:

  • 运行(“tput”、“cols”)
    (无论窗口大小始终保持不变)
  • (与上述问题相同,只是在没有API的情况下暴露出来)
  • 问题: 要获取有关当前会话的信息或在当前窗口的上下文中运行bash进程,特别是有关窗口大小的信息,我需要做什么?


    我想尝试一些方法,列出当前的终端会话并在其中一个会话中运行bash脚本,但我不知道如何使其工作(比如bash
    who
    ,然后选择正确的会话并从那里开始工作。不确定这是否可行。):

    您可以使用此函数在bash中执行命令:

    func shell(_ command: String) -> String {
        let task = Process()
        task.launchPath = "/bin/bash"
        task.arguments = ["-c", command]
    
        let pipe = Pipe()
        task.standardOutput = pipe
        task.launch()
    
        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
    
        return output
    }
    
    然后简单地使用:

    let cols = shell("tput cols")
    let lines = shell("tput lines")
    
    它将作为字符串返回,因此您可能希望将输出转换为整数


    希望能有帮助。

    stty大小如何?
    ?你把“会话”和“终端”与“当前窗口”混为一谈了。