io-console 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8485f98825214ba70388cfaf023a5380201fc1de70b87ffddb36a9a2603115c
4
- data.tar.gz: db5deeb27f6bc90121cdb8ad6005aca2615f5a81976f97f09f25e63caed7070e
3
+ metadata.gz: 0ab38381fd5d6bef3376d2595a67edac67ef212198692b5f884624e28f592e11
4
+ data.tar.gz: 48437c0bceff0ca47aea6425123d9733804fdd9e4fc232971a5ea53e39a60066
5
5
  SHA512:
6
- metadata.gz: fcafaead8dd6a72e82e852ad8cf57bff49fb8fd8959520b8522c3fbf79bd97f1b218dd341f1e66656500c2a5ccd34045260655af948d86031e8f01ce19887df7
7
- data.tar.gz: f8ebf55a9fef2383adda797b62824cc7b9cc9458c98fd9025bab2945111851651df9a44469ec10549d4ea063469870ba2cc32a4a25d3e68dc57206531bce6c54
6
+ metadata.gz: c9afdd89ddcb3f8fc3afa906368b2bab7c46feac3b68bae363f5c4e09fc1e3df65d124839257528776b920ba1287b9944d5fa56e0126aedfc4d9224e2ca7272a
7
+ data.tar.gz: e62f4dc698b9add150b3a546ff72fa7842459978edf58346c98929e6fd33e860729e709a29756d69cab08505f2234bda468daa6534d8d4c8aa67a717fec90106
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  static const char *const
7
- IO_CONSOLE_VERSION = "0.8.0";
7
+ IO_CONSOLE_VERSION = "0.8.2";
8
8
 
9
9
  #include "ruby.h"
10
10
  #include "ruby/io.h"
@@ -840,6 +840,9 @@ console_winsize(VALUE io)
840
840
  return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws)));
841
841
  }
842
842
 
843
+ static VALUE console_scroll(VALUE io, int line);
844
+ static VALUE console_goto(VALUE io, VALUE y, VALUE x);
845
+
843
846
  /*
844
847
  * call-seq:
845
848
  * io.winsize = [rows, columns]
@@ -856,6 +859,8 @@ console_set_winsize(VALUE io, VALUE size)
856
859
  #if defined _WIN32
857
860
  HANDLE wh;
858
861
  int newrow, newcol;
862
+ COORD oldsize;
863
+ SMALL_RECT oldwindow;
859
864
  BOOL ret;
860
865
  #endif
861
866
  VALUE row, col, xpixel, ypixel;
@@ -891,18 +896,62 @@ console_set_winsize(VALUE io, VALUE size)
891
896
  if (!GetConsoleScreenBufferInfo(wh, &ws)) {
892
897
  rb_syserr_fail(LAST_ERROR, "GetConsoleScreenBufferInfo");
893
898
  }
899
+ oldsize = ws.dwSize;
900
+ oldwindow = ws.srWindow;
901
+ if (ws.srWindow.Right + 1 < newcol) {
902
+ ws.dwSize.X = newcol;
903
+ }
904
+ if (ws.dwSize.Y < newrow) {
905
+ ws.dwSize.Y = newrow;
906
+ }
907
+ /* expand screen buffer first if needed */
908
+ if (!SetConsoleScreenBufferSize(wh, ws.dwSize)) {
909
+ rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo");
910
+ }
911
+ /* refresh ws for new dwMaximumWindowSize */
912
+ if (!GetConsoleScreenBufferInfo(wh, &ws)) {
913
+ rb_syserr_fail(LAST_ERROR, "GetConsoleScreenBufferInfo");
914
+ }
915
+ /* check new size before modifying buffer content */
916
+ if (newrow <= 0 || newcol <= 0 ||
917
+ newrow > ws.dwMaximumWindowSize.Y ||
918
+ newcol > ws.dwMaximumWindowSize.X) {
919
+ SetConsoleScreenBufferSize(wh, oldsize);
920
+ /* remove scrollbar if possible */
921
+ SetConsoleWindowInfo(wh, TRUE, &oldwindow);
922
+ rb_raise(rb_eArgError, "out of range winsize: [%d, %d]", newrow, newcol);
923
+ }
924
+ /* shrink screen buffer width */
894
925
  ws.dwSize.X = newcol;
895
- ret = SetConsoleScreenBufferSize(wh, ws.dwSize);
926
+ /* shrink screen buffer height if window height were the same */
927
+ if (oldsize.Y == ws.srWindow.Bottom - ws.srWindow.Top + 1) {
928
+ ws.dwSize.Y = newrow;
929
+ }
896
930
  ws.srWindow.Left = 0;
897
- ws.srWindow.Top = 0;
898
- ws.srWindow.Right = newcol-1;
899
- ws.srWindow.Bottom = newrow-1;
931
+ ws.srWindow.Right = newcol - 1;
932
+ ws.srWindow.Bottom = ws.srWindow.Top + newrow -1;
933
+ if (ws.dwCursorPosition.Y > ws.srWindow.Bottom) {
934
+ console_scroll(io, ws.dwCursorPosition.Y - ws.srWindow.Bottom);
935
+ ws.dwCursorPosition.Y = ws.srWindow.Bottom;
936
+ console_goto(io, INT2FIX(ws.dwCursorPosition.Y), INT2FIX(ws.dwCursorPosition.X));
937
+ }
938
+ if (ws.srWindow.Bottom > ws.dwSize.Y - 1) {
939
+ console_scroll(io, ws.srWindow.Bottom - (ws.dwSize.Y - 1));
940
+ ws.dwCursorPosition.Y -= ws.srWindow.Bottom - (ws.dwSize.Y - 1);
941
+ console_goto(io, INT2FIX(ws.dwCursorPosition.Y), INT2FIX(ws.dwCursorPosition.X));
942
+ ws.srWindow.Bottom = ws.dwSize.Y - 1;
943
+ }
944
+ ws.srWindow.Top = ws.srWindow.Bottom - (newrow - 1);
945
+ /* perform changes to winsize */
900
946
  if (!SetConsoleWindowInfo(wh, TRUE, &ws.srWindow)) {
901
- rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo");
947
+ int last_error = LAST_ERROR;
948
+ SetConsoleScreenBufferSize(wh, oldsize);
949
+ rb_syserr_fail(last_error, "SetConsoleWindowInfo");
902
950
  }
903
- /* retry when shrinking buffer after shrunk window */
904
- if (!ret && !SetConsoleScreenBufferSize(wh, ws.dwSize)) {
905
- rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo");
951
+ /* perform screen buffer shrinking if necessary */
952
+ if ((ws.dwSize.Y < oldsize.Y || ws.dwSize.X < oldsize.X) &&
953
+ !SetConsoleScreenBufferSize(wh, ws.dwSize)) {
954
+ rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo");
906
955
  }
907
956
  /* remove scrollbar if possible */
908
957
  if (!SetConsoleWindowInfo(wh, TRUE, &ws.srWindow)) {
@@ -1221,7 +1270,7 @@ console_cursor_pos(VALUE io)
1221
1270
  if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) {
1222
1271
  rb_syserr_fail(LAST_ERROR, 0);
1223
1272
  }
1224
- return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.Y), UINT2NUM(ws.dwCursorPosition.X));
1273
+ return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.Y - ws.srWindow.Top), UINT2NUM(ws.dwCursorPosition.X));
1225
1274
  #else
1226
1275
  static const struct query_args query = {"\033[6n", 0};
1227
1276
  VALUE resp = console_vt_response(0, 0, io, &query);
@@ -1254,11 +1303,17 @@ static VALUE
1254
1303
  console_goto(VALUE io, VALUE y, VALUE x)
1255
1304
  {
1256
1305
  #ifdef _WIN32
1257
- COORD pos;
1258
- int fd = GetWriteFD(io);
1259
- pos.X = NUM2UINT(x);
1260
- pos.Y = NUM2UINT(y);
1261
- if (!SetConsoleCursorPosition((HANDLE)rb_w32_get_osfhandle(fd), pos)) {
1306
+ HANDLE h;
1307
+ rb_console_size_t ws;
1308
+ COORD *pos = &ws.dwCursorPosition;
1309
+
1310
+ h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
1311
+ if (!GetConsoleScreenBufferInfo(h, &ws)) {
1312
+ rb_syserr_fail(LAST_ERROR, 0);
1313
+ }
1314
+ pos->X = NUM2UINT(x);
1315
+ pos->Y = ws.srWindow.Top + NUM2UINT(y);
1316
+ if (!SetConsoleCursorPosition(h, *pos)) {
1262
1317
  rb_syserr_fail(LAST_ERROR, 0);
1263
1318
  }
1264
1319
  #else
@@ -1651,8 +1706,6 @@ console_dev(int argc, VALUE *argv, VALUE klass)
1651
1706
  VALUE con = 0;
1652
1707
  VALUE sym = 0;
1653
1708
 
1654
- rb_check_arity(argc, 0, UNLIMITED_ARGUMENTS);
1655
-
1656
1709
  if (argc) {
1657
1710
  Check_Type(sym = argv[0], T_SYMBOL);
1658
1711
  }
@@ -10,11 +10,11 @@ have_func("rb_syserr_new_str(0, Qnil)") or
10
10
  abort
11
11
 
12
12
  have_func("rb_interned_str_cstr")
13
- have_func("rb_io_path")
14
- have_func("rb_io_descriptor")
15
- have_func("rb_io_get_write_io")
16
- have_func("rb_io_closed_p")
17
- have_func("rb_io_open_descriptor")
13
+ have_func("rb_io_path", "ruby/io.h")
14
+ have_func("rb_io_descriptor", "ruby/io.h")
15
+ have_func("rb_io_get_write_io", "ruby/io.h")
16
+ have_func("rb_io_closed_p", "ruby/io.h")
17
+ have_func("rb_io_open_descriptor", "ruby/io.h")
18
18
  have_func("rb_ractor_local_storage_value_newkey")
19
19
 
20
20
  is_wasi = /wasi/ =~ MakeMakefile::RbConfig::CONFIG["platform"]
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-03 00:00:00.000000000 Z
10
+ date: 2025-12-14 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: add console capabilities to IO instances.
14
13
  email: nobu@ruby-lang.org
@@ -32,7 +31,6 @@ licenses:
32
31
  metadata:
33
32
  source_code_url: https://github.com/ruby/io-console
34
33
  changelog_uri: https://github.com/ruby/io-console/releases
35
- post_install_message:
36
34
  rdoc_options: []
37
35
  require_paths:
38
36
  - lib
@@ -47,8 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
45
  - !ruby/object:Gem::Version
48
46
  version: '0'
49
47
  requirements: []
50
- rubygems_version: 3.5.11
51
- signing_key:
48
+ rubygems_version: 3.6.7
52
49
  specification_version: 4
53
50
  summary: Console interface
54
51
  test_files: []