Skip to content

Commit

Permalink
handle sigint to cleanup ncurses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jomy10 committed Dec 29, 2024
1 parent b461057 commit bdaa972
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "signalhandler",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Genaro-Chris/SignalHandler.git",
"state" : {
"branch" : "main",
"revision" : "bb52b9c5df6eba21adb6d540a19a2da6e6fab2fa"
}
}
],
"version" : 2
}
50 changes: 6 additions & 44 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var targets: [Target] = [
name: "SwiftCurses",
dependencies: [
"C_ncurses",
"C_ncursesBinds"
"C_ncursesBinds",
.product(name: "SignalHandler", package: "signalhandler")
],
swiftSettings: []),
.executableTarget(
Expand Down Expand Up @@ -60,50 +61,9 @@ targets
.append("C_ncursesw")
#endif

//#if canImport(FoundationNetworking)
//targets = [


//]
//#else
//targets = [
// .systemLibrary(
// name: "C_ncurses",
// providers: [
// .brew(["ncurses"]),
// .apt(["libncurses5-dev", "libncursesw5-dev"]),
// .yum(["ncurses-devel"])
// ]),

// .target(
// name: "SwiftCurses",
// dependencies: [
// "C_ncurses",
// "C_ncursesBinds"
// ],
// swiftSettings: [
// .define("SWIFTCURSES_OPAQUE")
// ]),

// .executableTarget(
// name: "Examples",
// dependencies: ["SwiftCurses"],
// exclude: ["README.md"],
// swiftSettings: [.unsafeFlags([
// "-Xfrontend",
// "-warn-long-function-bodies=100",
// "-Xfrontend",
// "-warn-long-expression-type-checking=100"
// ])]),

// .testTarget(
// name: "ncursesTests",
// dependencies: ["SwiftCurses"]),
//]
//#endif

let package = Package(
name: "SwiftCurses",
platforms: [.macOS(.v10_15)],
products: [
.library(
name: "SwiftCurses",
Expand All @@ -112,5 +72,7 @@ let package = Package(
name: "Examples",
targets: ["Examples"])
],
dependencies: [],
dependencies: [
.package(url: "https://github.com/Genaro-Chris/SignalHandler.git", branch: "main")
],
targets: targets)
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ Always looking at improving this library, feel free to leave suggestions/pull re
- https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/otherlib.html
- fill in the todos


## macOS version compatibility

| macOS version | library version |
|---------------|-----------------|
| < 10.15 | <= 1.0.3 |
| >= 10.15 | latest |

## License

Since the original is licensed under the MIT license, this library is also
Expand Down
22 changes: 22 additions & 0 deletions Sources/SwiftCurses/initScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import Musl
import ncursesw
#endif

#if !os(Windows)
import SignalHandler
#endif

/// Start a new curses mode terminal
///
/// - Parameters:
Expand All @@ -26,6 +30,16 @@ public func initScreen(
guard let _scr = initscr() else { // start curses mode
throw CursesError(.cannotCreateWindow)
}
// Assure cleanup on interrupt
#if !os(Windows)
let exitHandler = SignalHandler(signals: .SIGINT) { _ in
endwin()
exit(0)
}
Task {
await exitHandler.start()
}
#endif
defer {
endwin() // end curses mode
}
Expand Down Expand Up @@ -55,6 +69,14 @@ public func initScreenAsync(
guard let _scr = initscr() else { // start curses mode
throw CursesError(.cannotCreateWindow)
}
// Assure cleanup on interrupt
#if !os(Windows)
let exitHandler = SignalHandler(signals: .SIGINT) { _ in
endwin()
exit(0)
}
await exitHandler.start()
#endif
defer {
endwin() // end curses mode
}
Expand Down

0 comments on commit bdaa972

Please sign in to comment.