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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! `NcCell` methods and associated functions.

use crate::{
    c_api::{self, nccell_load, NcChannels_u64, NCRESULT_ERR},
    cstring, error, rstring, NcAlpha, NcCell, NcChannel, NcChannels, NcError, NcPaletteIndex,
    NcPlane, NcResult, NcRgb, NcStyle,
};

/// # NcCell constructors
impl NcCell {
    /// New `NcCell`, expects a 7-bit [`char`].
    #[inline]
    #[allow(clippy::unnecessary_cast)]
    pub fn from_char7b(ch: char) -> NcResult<Self> {
        if !ch.is_ascii() {
            return Err(NcError::new());
        }
        Ok(NcCell {
            gcluster: (ch as u32).to_le(),
            gcluster_backstop: 0,
            width: 0_u8,
            stylemask: NcStyle::None.into(),
            channels: 0 as NcChannels_u64,
        })
    }

    /// New `NcCell`, from a [`char`].
    ///
    /// Expects a plane where to save the extra data if it's greater than 4 bytes.
    #[inline]
    pub fn from_char(plane: &mut NcPlane, ch: char) -> NcResult<Self> {
        let mut cell = Self::new();
        let cs = cstring![ch.to_string()];
        let res = unsafe { nccell_load(plane, &mut cell, cs.as_ptr()) };
        if res == NCRESULT_ERR {
            return Err(NcError::new());
        }
        Ok(cell)
    }

    /// New `NcCell`, from a [`&str`].
    ///
    /// Expects a plane where to save the extra data if it's greater than 4 bytes.
    #[inline]
    pub fn from_str(plane: &mut NcPlane, string: &str) -> NcResult<Self> {
        let mut cell = Self::new();
        let cs = cstring![string];
        let res = unsafe { nccell_load(plane, &mut cell, cs.as_ptr()) };
        if res == NCRESULT_ERR {
            return Err(NcError::new());
        }
        Ok(cell)
    }

    /// New empty `NcCell`.
    #[inline]
    pub fn new() -> Self {
        Self::from_char7b(0 as char).unwrap()
    }

    /// Breaks the UTF-8 string in `egc` down, setting up this `NcCell`,
    /// and returns the number of bytes copied out of `egc`.
    ///
    /// The styling of the cell is left untouched, but any resources are released.
    /// *C style function: [nccell_load()][c_api::nccell_load].*
    pub fn load(plane: &mut NcPlane, cell: &mut NcCell, egc: &str) -> NcResult<u32> {
        let cs = cstring![egc];
        let bytes = unsafe { c_api::nccell_load(plane, cell, cs.as_ptr()) };
        error![
            bytes,
            &format!["NcCell.load(NcPlane, NcCell, {:?})", egc],
            bytes as u32
        ]
    }

    /// Same as [load][NcCell#method.load], plus blasts the styling with
    /// `style` and `channels`.
    ///
    /// - Breaks the UTF-8 string in `gcluster` down, setting up this NcCell.
    /// - Returns the number of bytes copied out of `gcluster`.
    /// - Any resources are released.
    /// - Blasts the styling with `style` and `channels`.
    ///
    /// *C style function: [nccell_prime()][c_api::nccell_prime].*
    pub fn prime(
        plane: &mut NcPlane,
        cell: &mut NcCell,
        gcluster: &str,
        style: impl Into<NcStyle>,
        channels: impl Into<NcChannels>,
    ) -> NcResult<u32> {
        let bytes = c_api::nccell_prime(plane, cell, gcluster, style.into().0, channels.into().0);
        error![bytes, "", bytes as u32]
    }

    /// Duplicate this `NcCell` into another one.
    ///
    /// Both must be or will be bound to `common_plane`.
    ///
    /// *C style function: [nccell_duplicate()][c_api::nccell_duplicate].*
    pub fn duplicate(&self, common_plane: &mut NcPlane) -> NcResult<NcCell> {
        let mut target = NcCell::new();
        let res = unsafe { c_api::nccell_duplicate(common_plane, &mut target, self) };
        error![res, "NcCell.duplicate()", target]
    }

    /// Initializes (zeroes out) this `NcCell`.
    ///
    /// *C style function: [nccell_init()][c_api::nccell_init].*
    #[inline]
    pub fn init(&mut self) {
        c_api::nccell_init(self);
    }

    /// Releases resources held by the current cell in the [NcPlane] `plane`.
    ///
    /// *C style function: [nccell_release()][c_api::nccell_release].*
    pub fn release(&mut self, plane: &mut NcPlane) {
        unsafe {
            c_api::nccell_release(plane, self);
        }
    }
}

// -----------------------------------------------------------------------------
/// ## NcCell methods: bg|fg `NcChannel`s manipulation.
impl NcCell {
    /// Gets the background alpha and coloring bits from the cell [`NcChannels`]
    /// as an [`NcChannel`].
    ///
    /// *C style function: [nccell_bchannel()][c_api::nccell_bchannel].*
    pub fn bchannel(&self) -> NcChannel {
        c_api::nccell_bchannel(self).into()
    }

    /// Gets the foreground alpha and coloring bits from the cell [`NcChannels`]
    /// as an [`NcChannel`].
    ///
    /// *C style function: [nccell_fchannel()][c_api::nccell_fchannel].*
    pub fn fchannel(&self) -> NcChannel {
        c_api::nccell_fchannel(self).into()
    }

    /// Gets the alpha and coloring bits from the cell [`NcChannels`].
    ///
    /// *C style function: [nccell_channels()][c_api::nccell_channels].*
    pub fn channels(&self) -> NcChannels {
        c_api::nccell_channels(self).into()
    }

    /// Sets the background alpha and coloring bits of the cell [`NcChannels`],
    /// returning the new [`NcChannels_u64`].
    ///
    /// *C style function: [nccell_bchannel()][c_api::nccell_bchannel].*
    pub fn set_bchannel(&mut self, from: impl Into<NcChannel>) -> NcChannels {
        c_api::nccell_set_bchannel(self, from.into().0).into()
    }

    /// Sets the foreground alpha and coloring bits from the cell [`NcChannels`],
    /// returning the new [`NcChannels_u64`].
    ///
    /// *C style function: [nccell_set_fchannel()][c_api::nccell_set_fchannel].*
    pub fn set_fchannel(&mut self, from: impl Into<NcChannel>) -> NcChannels {
        c_api::nccell_set_fchannel(self, from.into().0).into()
    }

    /// Sets the alpha and coloring bits of the cell [`NcChannels`]
    /// from another [`NcChannels`], returning the new [`NcChannels`].
    ///
    /// *C style function: [nccell_set_channels()][c_api::nccell_set_channels].*
    pub fn set_channels(&mut self, from: impl Into<NcChannels>) -> NcChannels {
        c_api::nccell_set_channels(self, from.into().0).into()
    }

    /// Gets the background [`NcAlpha`] (shifted to LSBs).
    ///
    /// *C style function: [nccell_bg_alpha()][c_api::nccell_bg_alpha].*
    pub fn bg_alpha(&self) -> NcAlpha {
        c_api::nccell_bg_alpha(self).into()
    }

    /// Is the background [`NcChannel`] using the "default background color"?
    ///
    /// *C style function: [nccell_bg_default_p()][c_api::nccell_bg_default_p].*
    pub fn bg_default_p(&self) -> bool {
        c_api::nccell_bg_default_p(self)
    }

    /// Gets the [`NcPaletteIndex`] of the background [`NcChannel`].
    ///
    /// *C style function: [nccell_bg_palindex()][c_api::nccell_bg_palindex].*
    pub fn bg_palindex(&self) -> NcPaletteIndex {
        c_api::nccell_bg_palindex(self)
    }

    /// Is the background [`NcChannel`] using an [`NcPaletteIndex`] indexed
    /// [`NcPalette`][crate::NcPalette] color?
    ///
    /// *C style function: [nccell_bg_palindex_p()][c_api::nccell_bg_palindex_p].*
    pub fn bg_palindex_p(&self) -> bool {
        c_api::nccell_bg_palindex_p(self)
    }

    /// Gets the background [`NcRgb`] (shifted to LSBs).
    ///
    /// *C style function: [nccell_bg_rgb()][c_api::nccell_bg_rgb].*
    pub fn bg_rgb(&self) -> NcRgb {
        c_api::nccell_bg_rgb(self).into()
    }

    /// Gets the foreground [`NcAlpha`] (shifted to LSBs).
    ///
    /// *C style function: [nccell_fg_alpha()][c_api::nccell_fg_alpha].*
    pub fn fg_alpha(&self) -> NcAlpha {
        c_api::nccell_fg_alpha(self).into()
    }

    /// Is the foreground [`NcChannel`] using the "default foreground color"?
    ///
    /// *C style function: [nccell_fg_default_p()][c_api::nccell_fg_default_p].*
    pub fn fg_default_p(&self) -> bool {
        c_api::nccell_fg_default_p(self)
    }

    /// Gets the [`NcPaletteIndex`] of the foreground [`NcChannel`].
    ///
    /// *C style function: [nccell_fg_palindex()][c_api::nccell_fg_palindex].*
    pub fn fg_palindex(&self) -> NcPaletteIndex {
        c_api::nccell_fg_palindex(self)
    }

    /// Is the foreground [`NcChannel`] using an [`NcPaletteIndex`] indexed
    /// [`NcPalette`][crate::NcPalette] color?
    ///
    /// *C style function: [nccell_fg_palindex_p()][c_api::nccell_fg_palindex_p].*
    pub fn fg_palindex_p(&self) -> bool {
        c_api::nccell_fg_palindex_p(self)
    }

    /// Gets the foreground [`NcRgb`] (shifted to LSBs).
    ///
    /// *C style function: [nccell_fg_rgb()][c_api::nccell_fg_rgb].*
    pub fn fg_rgb(&self) -> NcRgb {
        c_api::nccell_fg_rgb(self).into()
    }

    /// Sets the background [`NcAlpha`].
    ///
    /// *C style function: [nccell_set_bg_alpha()][c_api::nccell_set_bg_alpha].*
    pub fn set_bg_alpha(&mut self, alpha: impl Into<NcAlpha>) {
        c_api::nccell_set_bg_alpha(self, alpha.into());
    }

    /// Indicates to use the "default color" for the background [`NcChannel`].
    ///
    /// *C style function: [nccell_set_bg_default()][c_api::nccell_set_bg_default].*
    pub fn set_bg_default(&mut self) {
        c_api::nccell_set_bg_default(self);
    }

    /// Sets the background [`NcPaletteIndex`].
    ///
    /// Also sets
    /// [`NcChannels::BG_PALETTE_MASK`][crate::NcChannels#associatedconstant.BG_PALETTE_MASK]
    /// and
    /// [`NcAlpha::OPAQUE`][NcAlpha#associatedconstant.OPAQUE], and clears out
    /// [`NcChannels::BG_DEFAULT_MASK`][NcChannels#associatedconstant.BG_DEFAULT_MASK].
    ///
    /// *C style function: [nccell_set_bg_palindex()][c_api::nccell_set_bg_palindex].*
    pub fn set_bg_palindex(&mut self, index: impl Into<NcPaletteIndex>) {
        c_api::nccell_set_bg_palindex(self, index.into());
    }

    /// Sets the background [`NcRgb`] and marks it as not using the default color.
    ///
    /// *C style function: [nccell_set_bg_rgb()][c_api::nccell_set_bg_rgb].*
    pub fn set_bg_rgb(&mut self, rgb: impl Into<NcRgb>) {
        c_api::nccell_set_bg_rgb(self, rgb.into().0);
    }

    /// Sets the foreground [`NcAlpha`].
    ///
    /// *C style function: [nccell_set_fg_alpha()][c_api::nccell_set_fg_alpha].*
    pub fn set_fg_alpha(&mut self, alpha: impl Into<NcAlpha>) {
        c_api::nccell_set_fg_alpha(self, alpha.into());
    }

    /// Indicates to use the "default color" for the foreground [`NcChannel`].
    ///
    /// *C style function: [nccell_set_fg_default()][c_api::nccell_set_fg_default].*
    pub fn set_fg_default(&mut self) {
        c_api::nccell_set_fg_default(self);
    }

    /// Sets the foreground [`NcPaletteIndex`].
    ///
    /// Also sets
    /// [`NcChannels::FG_PALETTE_MASK`][crate::NcChannels#associatedconstant.FG_PALETTE_MASK]
    /// and
    /// [`NcAlpha::OPAQUE`][NcAlpha#associatedconstant.OPAQUE], and clears out
    /// [`NcChannels::FG_DEFAULT_MASK`][NcChannels#associatedconstant.FG_DEFAULT_MASK].
    ///
    /// *C style function: [nccell_set_fg_palindex()][c_api::nccell_set_fg_palindex].*
    pub fn set_fg_palindex(&mut self, index: impl Into<NcPaletteIndex>) {
        c_api::nccell_set_fg_palindex(self, index.into());
    }

    /// Sets the foreground [`NcRgb`] and marks it as not using the default color.
    ///
    /// *C style function: [nccell_set_fg_rgb()][c_api::nccell_set_fg_rgb].*
    pub fn set_fg_rgb(&mut self, rgb: impl Into<NcRgb>) {
        c_api::nccell_set_fg_rgb(self, rgb.into().0);
    }
}

/// # `NcCell` methods: other components
impl NcCell {
    /// Returns true if the two cells have distinct `EGC`s, attributes,
    /// or [`NcChannel`]s.
    ///
    /// The actual egcpool index needn't be the same--indeed, the planes
    /// needn't even be the same. Only the expanded `EGC` must be bit-equal.
    ///
    /// *C style function: [nccellcmp()][c_api::nccellcmp].*
    pub fn compare(plane1: &NcPlane, cell1: &NcCell, plane2: &NcPlane, cell2: &NcCell) -> bool {
        c_api::nccellcmp(plane1, cell1, plane2, cell2)
    }

    /// Saves the [`NcStyle`] and the [`NcChannels`], and returns the duplicatd `EGC`.
    ///
    /// *C style function: [nccell_fg_alpha()][c_api::nccell_fg_alpha].*
    pub fn extract(
        &self,
        plane: &mut NcPlane,
        styles: &mut NcStyle,
        channels: &mut NcChannels,
    ) -> String {
        c_api::nccell_extract(plane, self, styles.into(), channels.into())
    }

    /// Returns the [`NcStyle`] bits.
    ///
    /// *C style function: [nccell_styles()][c_api::nccell_styles].*
    pub fn styles(&self) -> NcStyle {
        c_api::nccell_styles(self).into()
    }

    /// Removes the specified [`NcStyle`] bits.
    ///
    /// *C style function: [nccell_off_styles()][c_api::nccell_off_styles].*
    pub fn styles_off(&mut self, stylebits: impl Into<NcStyle>) {
        c_api::nccell_off_styles(self, stylebits.into().0)
    }

    /// Adds the specified [`NcStyle`] bits.
    ///
    /// *C style function: [nccell_on_styles()][c_api::nccell_on_styles].*
    pub fn styles_on(&mut self, stylebits: impl Into<NcStyle>) {
        c_api::nccell_on_styles(self, stylebits.into().0)
    }

    /// Sets just the specified [`NcStyle`] bits.
    ///
    /// *C style function: [nccell_set_styles()][c_api::nccell_set_styles].*
    pub fn styles_set(&mut self, stylebits: impl Into<NcStyle>) {
        c_api::nccell_set_styles(self, stylebits.into().0)
    }
}

/// # `NcCell` methods: text
impl NcCell {
    /// Returns the number of columns occupied by the cell.
    ///
    /// See [`ncstrwidth`][c_api::ncstrwidth] for an equivalent for multiple EGCs.
    ///
    /// *C style function: [nccell_cols()][c_api::nccell_cols].*
    pub const fn cols(&self) -> u8 {
        c_api::nccell_cols(self)
    }

    // TODO: add ncstrwidth associated method

    /// Returns a pointer to the `EGC` of this NcCell in the `plane`.
    ///
    /// This pointer can be invalidated by any further operation on the referred
    /// plane, so… watch out!
    ///
    /// *C style function: [nccell_extended_gcluster()][c_api::nccell_wide_left_p].*
    pub fn egc(&self, plane: &NcPlane) -> &str {
        let egcpointer = unsafe { c_api::nccell_extended_gcluster(plane, self) };
        rstring![egcpointer]
    }

    /// Copies the UTF8-encoded `EGC` out of this NcCell,
    /// whether simple or complex.
    ///
    /// The result is not tied to the [NcPlane],
    /// and persists across erases and destruction.
    ///
    /// *C style function: [nccell_strdup()][c_api::nccell_strdup].*
    pub fn strdup(&self, plane: &NcPlane) -> String {
        c_api::nccell_strdup(plane, self)
    }

    /// Does this NcCell contain a wide codepoint?
    ///
    /// *C style function: [nccell_double_wide_p()][c_api::nccell_double_wide_p].*
    pub fn double_wide_p(&self) -> bool {
        c_api::nccell_double_wide_p(self)
    }

    /// Is this the left half of a wide character?
    ///
    /// *C style function: [nccell_wide_left_p()][c_api::nccell_wide_left_p].*
    pub fn wide_left_p(&self) -> bool {
        c_api::nccell_wide_right_p(self)
    }

    /// Is this the right side of a wide character?
    ///
    /// *C style function: [nccell_wide_right_p()][c_api::nccell_wide_right_p].*
    pub fn wide_right_p(&self) -> bool {
        c_api::nccell_wide_right_p(self)
    }
}

/// # `NcCell` methods: boxes
impl NcCell {
    /// Loads up six cells with the `EGC`s necessary to draw a box.
    ///
    /// On error, any [`NcCell`]s this function might have loaded before the error
    /// are [release][NcCell#method.release]d.
    /// There must be at least six `EGC`s in `gcluster`.
    ///
    /// *C style function: [nccells_load_box()][c_api::nccells_load_box].*
    pub fn load_box(
        plane: &mut NcPlane,
        style: impl Into<NcStyle>,
        channels: impl Into<NcChannels>,
        ul: &mut NcCell,
        ur: &mut NcCell,
        ll: &mut NcCell,
        lr: &mut NcCell,
        hl: &mut NcCell,
        vl: &mut NcCell,
        gcluster: &str,
    ) -> NcResult<()> {
        let (style, channels) = (style.into(), channels.into());
        error![c_api::nccells_load_box(
            plane, style, channels, ul, ur, ll, lr, hl, vl, gcluster
        )]
    }

    /// NcCell.[load_box()][NcCell#method.box] with the double box-drawing characters.
    ///
    /// *C style function: [nccells_double_box()][c_api::nccells_double_box].*
    pub fn double_box(
        plane: &mut NcPlane,
        style: impl Into<NcStyle>,
        channels: impl Into<NcChannels>,
        ul: &mut NcCell,
        ur: &mut NcCell,
        ll: &mut NcCell,
        lr: &mut NcCell,
        hl: &mut NcCell,
        vl: &mut NcCell,
    ) -> NcResult<()> {
        error![c_api::nccells_double_box(
            plane,
            style.into().0,
            channels.into().0,
            ul,
            ur,
            ll,
            lr,
            hl,
            vl
        )]
    }

    /// NcCell.[load_box()][NcCell#method.box] with the rounded box-drawing characters.
    ///
    /// *C style function: [nccells_rounded_box()][c_api::nccells_double_box].*
    pub fn rounded_box(
        plane: &mut NcPlane,
        style: impl Into<NcStyle>,
        channels: impl Into<NcChannels>,
        ul: &mut NcCell,
        ur: &mut NcCell,
        ll: &mut NcCell,
        lr: &mut NcCell,
        hl: &mut NcCell,
        vl: &mut NcCell,
    ) -> NcResult<()> {
        error![c_api::nccells_rounded_box(
            plane,
            style.into().0,
            channels.into().0,
            ul,
            ur,
            ll,
            lr,
            hl,
            vl
        )]
    }

    /// NcCell.[load_box()][NcCell#method.box] with ASCII characters.
    ///
    /// *C style function: [nccells_ascii_box()][c_api::nccells_ascii_box].*
    pub fn ascii_box(
        plane: &mut NcPlane,
        style: impl Into<NcStyle>,
        channels: impl Into<NcChannels>,
        ul: &mut NcCell,
        ur: &mut NcCell,
        ll: &mut NcCell,
        lr: &mut NcCell,
        hl: &mut NcCell,
        vl: &mut NcCell,
    ) -> NcResult<()> {
        error![c_api::nccells_ascii_box(
            plane,
            style.into().0,
            channels.into().0,
            ul,
            ur,
            ll,
            lr,
            hl,
            vl
        )]
    }
    /// NcCell.[load_box()][NcCell#method.box] with the heavy line
    /// box-drawing characters.
    ///
    /// *C style function: [nccells_heavy_box()][c_api::nccells_heavy_box].*
    pub fn heavy_box(
        plane: &mut NcPlane,
        style: impl Into<NcStyle>,
        channels: impl Into<NcChannels>,
        ul: &mut NcCell,
        ur: &mut NcCell,
        ll: &mut NcCell,
        lr: &mut NcCell,
        hl: &mut NcCell,
        vl: &mut NcCell,
    ) -> NcResult<()> {
        error![c_api::nccells_heavy_box(
            plane,
            style.into().0,
            channels.into().0,
            ul,
            ur,
            ll,
            lr,
            hl,
            vl
        )]
    }

    /// NcCell.[load_box()][NcCell#method.box] with the light line
    /// box-drawing characters.
    ///
    /// *C style function: [nccells_light_box()][c_api::nccells_light_box].*
    pub fn light_box(
        plane: &mut NcPlane,
        style: impl Into<NcStyle>,
        channels: impl Into<NcChannels>,
        ul: &mut NcCell,
        ur: &mut NcCell,
        ll: &mut NcCell,
        lr: &mut NcCell,
        hl: &mut NcCell,
        vl: &mut NcCell,
    ) -> NcResult<()> {
        error![c_api::nccells_light_box(
            plane,
            style.into().0,
            channels.into().0,
            ul,
            ur,
            ll,
            lr,
            hl,
            vl
        )]
    }
}