--- nono/lib/textscreen.cpp 2026/04/29 17:04:28 1.1 +++ nono/lib/textscreen.cpp 2026/04/29 17:04:34 1.1.1.3 @@ -1,9 +1,9 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "header.h" #include "textscreen.h" // @@ -24,9 +24,6 @@ TextScreen::TextScreen(int arg_col, int // デストラクタ TextScreen::~TextScreen() { - if (textbuf) { - delete[] textbuf; - } } // 初期化 @@ -36,7 +33,7 @@ TextScreen::Init(int arg_col, int arg_ro col = arg_col; row = arg_row; - textbuf = new uint16 [col * row]; + textbuf.reset(new uint16 [col * row]); X = 0; Y = 0; @@ -50,12 +47,18 @@ TextScreen::Clear() for (int i = 0; i < col * row; i++) { textbuf[i] = 0x0020; } + + // 折り返しモードならクリアでホームポジションへ + if (foldmode) { + Locate(0, 0); + } } // 現在のカーソル位置に1文字出力する。 // カーソルは一つ移動する。 -// 右もしくは下にはみ出した場合は無視する。 -// XXX どっかで拾えてもいいような +// foldmode = false なら、移動後右もしくは下にはみ出しても何もしない。 +// foldmode = true なら、右にはみ出した場合は次行左端に移動、 +// 下にはみ出した場合はカーソル位置はそのままで上に1行スクロールさせる。 void TextScreen::Putc(uint16 ch) { @@ -63,8 +66,17 @@ TextScreen::Putc(uint16 ch) return; } - textbuf[(Y * col) + X] = ch; - X++; + if (ch == '\n') { + CRLF(); + } else { + textbuf[(Y * col) + X] = ch; + X++; + + // 折り返しモード + if (foldmode && X >= col) { + CRLF(); + } + } } // テキストスクリーンに文字列を出力する。 @@ -81,13 +93,27 @@ TextScreen::Puts(const char *str) // テキストスクリーンに文字列を出力する。 // 右もしくは下にはみ出した場合は無視する。 void -TextScreen::Puts(uint attr, const char *str) +TextScreen::Puts(TA attr, const char *str) { while (*str != '\0') { - Putc(attr | *str++); + Putc(((uint)attr) | *str++); } } +// テキストスクリーンの現在位置から書式付き文字列を出力する。 +void +TextScreen::Print(const char *fmt, ...) +{ + char buf[1024]; + va_list ap; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + Puts(buf); +} + // テキストスクリーンに座標を指定して書式付き文字列を出力する。 void TextScreen::Print(int x, int y, const char *fmt, ...) @@ -99,14 +125,13 @@ TextScreen::Print(int x, int y, const ch vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - X = x; - Y = y; + Locate(x, y); Puts(buf); } // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。 void -TextScreen::Print(int x, int y, uint attr, const char *fmt, ...) +TextScreen::Print(int x, int y, TA attr, const char *fmt, ...) { char buf[1024]; va_list ap; @@ -115,7 +140,22 @@ TextScreen::Print(int x, int y, uint att vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - X = x; - Y = y; + Locate(x, y); Puts(attr, buf); } + +// 改行 +void +TextScreen::CRLF() +{ + X = 0; + Y++; + + if (Y >= row) { + // スクロール + memmove(&textbuf[0], &textbuf[col], + (col - 1) * row * sizeof(textbuf[0])); + // カーソル位置は最下行 + Y = row - 1; + } +}