This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurses.html
110 lines (107 loc) · 6.03 KB
/
curses.html
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="description" content="Curses is a way to manipulate terminals mainly for text user interfaces in applications."><!-- Bing --><meta name="msvalidate.01" content="45CBBE1BD8265A2217DFDA630EB8F84A" /><title>Tiny Brain Fans - Curses</title><link rel="stylesheet" href="tinystyle.css"></head><body>
<main id="main"><article id="content"><h1 id="title">Curses</h1><p>Curses is a way to manipulate terminals mainly for text user interfaces in applications.</p>
<h2><code>ncurses.h</code></h2>
<p>The <code>ncurses.h</code> helper file in <a href="c.html">C</a> includes a lot of other libraries, as well, which means you don't need to include them, as they may bloat your final compiled program[2]:</p>
<ul>
<li><code>stdio.h</code></li>
<li><code>unctrl.h</code></li>
<li><code>stdarg.h</code></li>
<li><code>stddef.h</code></li>
</ul>
<h2>Example</h2>
<p>This is an example of ncurses, the most common curses library, from Dan Gookin's book[2] in <a href="c.html">C</a>.</p>
<p><strong>goodbye.c</strong></p>
<pre><code class="language-c">#include <ncurses.h> // Use the ncurses library
int main(void)
{
initscr(); // allocates memory for present window which is called stdscr[4]
addstr("Goodbye, cruel C programming!"); // add string to window
refresh(); // flushes current window to the screen
getch(); // waits for user input to exit
endwin(); // call to end or exit our curses program[5]
return 0; // ended program without errors
}
</code></pre>
<p><strong>Compiling</strong></p>
<pre><code class="language-bash">$ gcc -lncurses goodbye.c -o goodbye
# -lncurses : the curses linker
</code></pre>
<p><strong>Running</strong></p>
<pre><code class="language-bash">$ ./goodbye
</code></pre>
<h2>Functions</h2>
<h3>Cursor</h3>
<ul>
<li><code>move(row, column)</code> - Move the cursor to the Y,X point.</li>
</ul>
<h3>Printing Text</h3>
<ul>
<li><code>printw(str[, arg1, arg2, ...])</code> - <code>printw</code> is <code>printf</code> for ncurses. Uses standard string formatting.</li>
<li><code>addstr(str)</code> - Print the string at the cursor.</li>
<li><code>addch(char)</code> - Print the current character at the cursor.</li>
</ul>
<h3>Text Formatting</h3>
<h4>Styles</h4>
<ul>
<li><code>attron(attr[ | attr | ...])</code>/<code>attroff(attr[ | attr | ...])</code> - Set attributes on/off for the following text.</li>
<li><code>attrset(attr[ | attr | ...])</code> - Set attributes for the following text, resetting all previous attributes.</li>
</ul>
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>A_BOLD</code></td>
<td>Bright text, bold text, thick text (depending on terminal type)</td>
</tr>
<tr>
<td><code>A_DIM</code></td>
<td>Dimmed text (not as bright as regular text)</td>
</tr>
<tr>
<td><code>A_NORMAL</code></td>
<td>Normal text</td>
</tr>
<tr>
<td><code>A_REVERSE</code></td>
<td>Inverse text</td>
</tr>
<tr>
<td><code>A_STANDOUT</code></td>
<td>Same as <code>standout()</code></td>
</tr>
<tr>
<td><code>A_UNDERLINE</code></td>
<td>Underline text</td>
</tr>
</tbody></table><h4>Colors</h4>
<ul>
<li><code>int has_colors()</code> - Returns <code>TRUE</code> if terminal can render colors, and <code>FALSE</code> otherwise, using builtin defs for <code>TRUE</code> and <code>FALSE</code>.</li>
<li><code>int start_color()</code> - Allows colors to be used. Returns <code>OK</code> if allowed. Usually used right after <code>initscr</code>.</li>
</ul>
<h3>User Input</h3>
<ul>
<li><code>int getch()</code> - Waits for user input and returns the inputted character.</li>
<li><code>getnstr(str, int n)</code> - Get user input up to <code>n</code> characters. On newline, stores input character buffer in <code>str</code>.</li>
</ul>
<h3>Display</h3>
<ul>
<li><code>clrtoeol()</code> - Clears everything that is on the current line under the cursor[7].</li>
<li><code>clear()</code> - Clears the screen.</li>
<li><code>curs_set(0)</code> - Make cursor invisible on screen.</li>
</ul>
<h2>References</h2>
<ol>
<li><a href="https://en.wikipedia.org/wiki/Curses_%28programming_library%29" target="_blank">https://en.wikipedia.org/wiki/Curses_%28programming_library%29</a></li>
<li><a href="https://www.goodreads.com/book/show/4460597-programmer-s-guide-to-ncurses" target="_blank">https://www.goodreads.com/book/show/4460597-programmer-s-guide-to-ncurses</a></li>
<li><a href="https://www.linuxhowtos.org/manpages/3x/ncurses.htm" target="_blank">https://www.linuxhowtos.org/manpages/3x/ncurses.htm</a></li>
<li><a href="https://www.sbarjatiya.com/notes_wiki/index.php/Using_ncurses_library_with_C" target="_blank">https://www.sbarjatiya.com/notes_wiki/index.php/Using_ncurses_library_with_C</a></li>
<li><a href="https://linux.die.net/man/3/endwin" target="_blank">https://linux.die.net/man/3/endwin</a></li>
<li><a href="https://manpages.debian.org/testing/ncurses-doc/start_color.3ncurses.en.html" target="_blank">https://manpages.debian.org/testing/ncurses-doc/start_color.3ncurses.en.html</a></li>
<li><a href="https://stackoverflow.com/questions/5072881/how-to-clear-a-specific-line-with-ncurses" target="_blank">https://stackoverflow.com/questions/5072881/how-to-clear-a-specific-line-with-ncurses</a></li>
</ol>
<section id="backlinks"><details><summary>Backlinks</summary><ul><li><a href="command-line-arguments-in-c.html">Command Line Arguments in C</a></li></ul></details></section><p class="last-modified">Last modified: 202206101419</p></article></main><footer><nav><a href="index.html">Sitemap</a></nav><div class="social"><p>Built using <a href="http://codeberg.org/milofultz/swiki" target="_blank" rel="noopener noreferrer">{{SWIKI}}</a></p><p><a href="http://codeberg.org/milofultz/" target="_blank" rel="noopener noreferrer">Codeberg</a></p><p><a href="http://milofultz.com/" target="_blank" rel="noopener noreferrer">milofultz.com</a></p><p><a href="https://merveilles.town/@milofultz" target="_blank" rel="me noopener noreferrer">Mastodon</a></p></div></footer></body></html>