在ncurses中获得收盘胜利的信号[Linux]

时间:2017-08-09 04:54:43

标签: swift

在关于如何使用带有swift的ncurses的教程中,我遇到了错误:

50:5: error: missing argument label 'signum:' in call
trap(.INT) { signal in
    ^
     signum: 

在这个小小的尝试中捕获关闭终端的信号(据我所知):

trap(.INT) { signal in
  endwin()
  exit(0)
}

问题

  • 如何在linux中工作时找到trap()实际需要的参数?参考这种方法也很有用。

整个参考代码:

import Foundation
import CNCURSES
import Glibc

enum Signal:Int32 {
case INT   = 2
case WINCH = 28
}

typealias SignalHandler = __sighandler_t

func trap(signum:Signal, action:SignalHandler) {
  signal(signum.rawValue, action)
}

func getmaxyx(window:UnsafeMutablePointer<WINDOW>, y:inout Int32, x:inout Int32) {
  x = getmaxx(window)
  y = getmaxy(window)
}

func getcuryx(window:UnsafeMutablePointer<WINDOW>, y:inout Int32, x:inout Int32) {
  x = getcurx(window)
  y = getcury(window)
}

func drawbox(numlines:Int32, numcols:Int32) {
  for y in 0...numlines-1 {
    for x in 0...numcols {
      move(y, x)
      if y == 0 || y == numlines-1 {
        addch(UInt("*"))
      } else {
        if x == 0 || x == numcols {
          addch(UInt("*"))
        }
      }
    }
  }
  refresh()
}

func centerText(text:String, numlines:Int32, numcols:Int32) {
  let cy:Int32 = numlines/2
  let cx:Int32 = (numcols - Int32(text.characters.count))/2
  move(cy,cx)
  addstr(text)
  refresh()
}

trap(.INT) { signal in
  endwin()
  exit(0)
}

var maxy:Int32     = 0
var maxx:Int32     = 0

trap(.WINCH) { signal in
  endwin()
  refresh() 
  initscr()
  clear()

  getmaxyx(stdscr, y:&maxy, x:&maxx)
  drawbox(maxy, numcols:maxx)
  centerText("Hello world!", numlines:maxy, numcols:maxx)
}


initscr()
noecho()
curs_set(0)
getmaxyx(stdscr, y:&maxy, x:&maxx)
drawbox(maxy, numcols:maxx)
centerText("Hello world!", numlines:maxy, numcols:maxx)

while true {
  select(0, nil, nil, nil, nil)
}

1 个答案:

答案 0 :(得分:1)

您应该在函数调用中命名参数signum。将trap(.INT)替换为

trap(signum:.Int)