-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05-attributes.swift
62 lines (56 loc) · 1.82 KB
/
05-attributes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//=======================================================================
// Attributes Example
//
// Written by: Jonas Everaert
// Based on: https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/attrib.html
//=======================================================================
import SwiftCurses
import Foundation
/// Example 5. A Simple Attributes example
///
/// This example will show the contents of a file, and mark any multiline
/// comment (/**/) in bold (passed in as a parameter;
/// `swift run SwiftCursesExamples attributes [filename]`)
func attributesExample() throws {
let arg: String
if CommandLine.argc == 3 {
arg = CommandLine.arguments[2]
} else if CommandLine.argc == 2 {
arg = CommandLine.arguments[1]
} else {
print("Usage: \(CommandLine.arguments[0]) <a c file name>")
return
}
let url = URL(fileURLWithPath: arg)
let contents = try String(contentsOf: url)
try initScreen(
settings: [],
windowSettings: []
) { scr in
let (row, _) = scr.maxYX.tuple // Boundary of the screen
var prev: Character = "\0"
try contents.forEach { ch in
var (y, x) = scr.yx.tuple // current curser position
if (y == (row - 1)) { // are we at the end of the screen
try scr.print("<-Press Any Key->")
try scr.getChar()
scr.clear() // clear the screen
try scr.move(row: 0, col: 0)// start at the beginning of the screen
}
if prev == "/" && ch == "*" { // if it is / and * then only switch bold on
scr.attrOn(.bold) // turn bold on
(y, x) = scr.yx.tuple
try scr.move(row: y, col: x - 1) // back up ine cursor position
try scr.print("/\(ch)") // the actual printing is done here
} else {
try scr.print(ch)
}
scr.refresh()
if prev == "*" && ch == "/" {
scr.attrOff(.bold) // switch if off once we got * and then /
}
prev = ch
}
try scr.getChar()
}
}