zsv 1.3.0

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.
Files changed (240) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +36 -0
  3. data/LICENSE +21 -0
  4. data/README.md +311 -0
  5. data/ext/zsv/common.h +34 -0
  6. data/ext/zsv/extconf.rb +137 -0
  7. data/ext/zsv/options.c +126 -0
  8. data/ext/zsv/options.h +31 -0
  9. data/ext/zsv/options_internal.h +8 -0
  10. data/ext/zsv/parser.c +300 -0
  11. data/ext/zsv/parser.h +62 -0
  12. data/ext/zsv/row.c +122 -0
  13. data/ext/zsv/row.h +39 -0
  14. data/ext/zsv/vendor/zsv-1.3.0/app/2db.c +756 -0
  15. data/ext/zsv/vendor/zsv-1.3.0/app/2json.c +381 -0
  16. data/ext/zsv/vendor/zsv-1.3.0/app/2tsv.c +228 -0
  17. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/help.c +123 -0
  18. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/license.c +39 -0
  19. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/register.c +104 -0
  20. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/thirdparty.c +41 -0
  21. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/unregister.c +1 -0
  22. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/version.c +14 -0
  23. data/ext/zsv/vendor/zsv-1.3.0/app/check/simdutf_wrapper.h +19 -0
  24. data/ext/zsv/vendor/zsv-1.3.0/app/check/utf8.c +116 -0
  25. data/ext/zsv/vendor/zsv-1.3.0/app/check.c +194 -0
  26. data/ext/zsv/vendor/zsv-1.3.0/app/cli.c +796 -0
  27. data/ext/zsv/vendor/zsv-1.3.0/app/cli_const.h +41 -0
  28. data/ext/zsv/vendor/zsv-1.3.0/app/cli_export.h +16 -0
  29. data/ext/zsv/vendor/zsv-1.3.0/app/cli_ini.c +280 -0
  30. data/ext/zsv/vendor/zsv-1.3.0/app/cli_internal.h +36 -0
  31. data/ext/zsv/vendor/zsv-1.3.0/app/compare.c +913 -0
  32. data/ext/zsv/vendor/zsv-1.3.0/app/compare.h +23 -0
  33. data/ext/zsv/vendor/zsv-1.3.0/app/compare_added_column.c +20 -0
  34. data/ext/zsv/vendor/zsv-1.3.0/app/compare_internal.h +140 -0
  35. data/ext/zsv/vendor/zsv-1.3.0/app/compare_sort.c +91 -0
  36. data/ext/zsv/vendor/zsv-1.3.0/app/compare_unique_colname.c +81 -0
  37. data/ext/zsv/vendor/zsv-1.3.0/app/count-pull.c +82 -0
  38. data/ext/zsv/vendor/zsv-1.3.0/app/count.c +404 -0
  39. data/ext/zsv/vendor/zsv-1.3.0/app/desc.c +569 -0
  40. data/ext/zsv/vendor/zsv-1.3.0/app/echo.c +365 -0
  41. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/my_extension.c +366 -0
  42. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/mysheet_extension.c +341 -0
  43. data/ext/zsv/vendor/zsv-1.3.0/app/ext_template/YOUR_EXTENSION_zsvext.c +263 -0
  44. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.c +298 -0
  45. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.h +157 -0
  46. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/json_numeric.c +177 -0
  47. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.c +444 -0
  48. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.h +145 -0
  49. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/utils.c +110 -0
  50. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/include/memfile.h +15 -0
  51. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/src/memfile.c +64 -0
  52. data/ext/zsv/vendor/zsv-1.3.0/app/external/sglib/sglib.h +1955 -0
  53. data/ext/zsv/vendor/zsv-1.3.0/app/external/simdutf/simdutf.h +6802 -0
  54. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.c +230517 -0
  55. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.h +12174 -0
  56. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_and_csv_vtab.c +2 -0
  57. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.c +142 -0
  58. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.h +49 -0
  59. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-zsv.c +485 -0
  60. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab.c +1015 -0
  61. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3ext.h +663 -0
  62. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/vtab_helper.c +85 -0
  63. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_common.h +75 -0
  64. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_gen.h +167 -0
  65. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_parse.h +228 -0
  66. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_tree.h +186 -0
  67. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_version.h +23 -0
  68. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_common.h +76 -0
  69. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_gen.h +167 -0
  70. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_parse.h +238 -0
  71. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_tree.h +186 -0
  72. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl.c +184 -0
  73. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.c +52 -0
  74. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.h +34 -0
  75. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.c +103 -0
  76. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.h +57 -0
  77. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_bytestack.h +69 -0
  78. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.c +220 -0
  79. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.h +34 -0
  80. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_gen.c +362 -0
  81. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.c +764 -0
  82. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.h +117 -0
  83. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.c +508 -0
  84. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.h +78 -0
  85. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_tree.c +505 -0
  86. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_version.c +7 -0
  87. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/json_value.h +59 -0
  88. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/yajl_helper.h +208 -0
  89. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper.c +795 -0
  90. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper_internal.h +28 -0
  91. data/ext/zsv/vendor/zsv-1.3.0/app/flatten.c +851 -0
  92. data/ext/zsv/vendor/zsv-1.3.0/app/jq.c +106 -0
  93. data/ext/zsv/vendor/zsv-1.3.0/app/jq.h +6 -0
  94. data/ext/zsv/vendor/zsv-1.3.0/app/mv.c +113 -0
  95. data/ext/zsv/vendor/zsv-1.3.0/app/noop.c +90 -0
  96. data/ext/zsv/vendor/zsv-1.3.0/app/overwrite.c +295 -0
  97. data/ext/zsv/vendor/zsv-1.3.0/app/paste.c +175 -0
  98. data/ext/zsv/vendor/zsv-1.3.0/app/pretty.c +693 -0
  99. data/ext/zsv/vendor/zsv-1.3.0/app/prop.c +980 -0
  100. data/ext/zsv/vendor/zsv-1.3.0/app/rm.c +131 -0
  101. data/ext/zsv/vendor/zsv-1.3.0/app/select/fixed.c +130 -0
  102. data/ext/zsv/vendor/zsv-1.3.0/app/select/internal.h +118 -0
  103. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.c +45 -0
  104. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.h +41 -0
  105. data/ext/zsv/vendor/zsv-1.3.0/app/select/processing.c +107 -0
  106. data/ext/zsv/vendor/zsv-1.3.0/app/select/rand.c +20 -0
  107. data/ext/zsv/vendor/zsv-1.3.0/app/select/regex.c +61 -0
  108. data/ext/zsv/vendor/zsv-1.3.0/app/select/search.c +14 -0
  109. data/ext/zsv/vendor/zsv-1.3.0/app/select/selection.c +192 -0
  110. data/ext/zsv/vendor/zsv-1.3.0/app/select/usage.c +72 -0
  111. data/ext/zsv/vendor/zsv-1.3.0/app/select-pull.c +812 -0
  112. data/ext/zsv/vendor/zsv-1.3.0/app/select.c +753 -0
  113. data/ext/zsv/vendor/zsv-1.3.0/app/serialize.c +372 -0
  114. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/curses.h +15 -0
  115. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/cursor.c +119 -0
  116. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/errors.c +45 -0
  117. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.c +63 -0
  118. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.h +12 -0
  119. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/filter.c +166 -0
  120. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers.c +214 -0
  121. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers_internal.h +128 -0
  122. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/help.c +43 -0
  123. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.c +81 -0
  124. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.h +25 -0
  125. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.c +325 -0
  126. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.h +73 -0
  127. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/lexer.c +203 -0
  128. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/newline_handler.c +7 -0
  129. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/pivot.c +318 -0
  130. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.c +134 -0
  131. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.h +119 -0
  132. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/read-data.c +322 -0
  133. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.c +203 -0
  134. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.h +36 -0
  135. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet-sql.c +167 -0
  136. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet_internal.h +36 -0
  137. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sqlfilter.c +153 -0
  138. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/terminfo.c +32 -0
  139. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.c +312 -0
  140. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.h +29 -0
  141. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/ui_buffer.c +266 -0
  142. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/usage.c +9 -0
  143. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/utf8-width.c +60 -0
  144. data/ext/zsv/vendor/zsv-1.3.0/app/sheet.c +1007 -0
  145. data/ext/zsv/vendor/zsv-1.3.0/app/sql.c +453 -0
  146. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.c +101 -0
  147. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.h +49 -0
  148. data/ext/zsv/vendor/zsv-1.3.0/app/stack.c +393 -0
  149. data/ext/zsv/vendor/zsv-1.3.0/app/utils/arg.c +322 -0
  150. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cache.c +228 -0
  151. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cat.c +91 -0
  152. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.c +240 -0
  153. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.h +63 -0
  154. data/ext/zsv/vendor/zsv-1.3.0/app/utils/clock.c +57 -0
  155. data/ext/zsv/vendor/zsv-1.3.0/app/utils/db.c +148 -0
  156. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs-no-jq.c +2 -0
  157. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs.c +427 -0
  158. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_from_json.c +253 -0
  159. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_to_json.c +121 -0
  160. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dl.c +20 -0
  161. data/ext/zsv/vendor/zsv-1.3.0/app/utils/emcc/fs_api.c +159 -0
  162. data/ext/zsv/vendor/zsv-1.3.0/app/utils/err.c +24 -0
  163. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file-mem.c +180 -0
  164. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file.c +256 -0
  165. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.c +197 -0
  166. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.h +49 -0
  167. data/ext/zsv/vendor/zsv-1.3.0/app/utils/jq.c +400 -0
  168. data/ext/zsv/vendor/zsv-1.3.0/app/utils/json.c +120 -0
  169. data/ext/zsv/vendor/zsv-1.3.0/app/utils/mem.c +18 -0
  170. data/ext/zsv/vendor/zsv-1.3.0/app/utils/memmem.c +132 -0
  171. data/ext/zsv/vendor/zsv-1.3.0/app/utils/os.c +178 -0
  172. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite.c +258 -0
  173. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite_writer.c +246 -0
  174. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8-test.c +123 -0
  175. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.c +153 -0
  176. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.h +54 -0
  177. data/ext/zsv/vendor/zsv-1.3.0/app/utils/prop.c +267 -0
  178. data/ext/zsv/vendor/zsv-1.3.0/app/utils/signal.c +53 -0
  179. data/ext/zsv/vendor/zsv-1.3.0/app/utils/string.c +357 -0
  180. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dir_exists_longpath.c +83 -0
  181. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dl.c +33 -0
  182. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/fopen_longpath.c +184 -0
  183. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/foreach_dirent_longpath.c +292 -0
  184. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.c +259 -0
  185. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.h +13 -0
  186. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/mkdir_longpath.c +255 -0
  187. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/remove_longpath.c +96 -0
  188. data/ext/zsv/vendor/zsv-1.3.0/app/utils/writer.c +361 -0
  189. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command.h +40 -0
  190. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command_standalone.c +16 -0
  191. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_main.h +44 -0
  192. data/ext/zsv/vendor/zsv-1.3.0/examples/js/zsv_parser_api_dummy.c +3 -0
  193. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/parse_by_chunk.c +100 -0
  194. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/print_my_column.c +143 -0
  195. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/pull.c +89 -0
  196. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/simple.c +123 -0
  197. data/ext/zsv/vendor/zsv-1.3.0/fuzz/fuzz.c +16 -0
  198. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/api.h +336 -0
  199. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/common.h +361 -0
  200. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation.h +62 -0
  201. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation_private.h +113 -0
  202. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/sheet.h +73 -0
  203. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext.h +329 -0
  204. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/arg.h +90 -0
  205. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/cache.h +49 -0
  206. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/clock.h +36 -0
  207. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/compiler.h +58 -0
  208. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/db.h +19 -0
  209. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dirs.h +147 -0
  210. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dl.h +22 -0
  211. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/emcc/fs_api.h +28 -0
  212. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/err.h +22 -0
  213. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file-mem.h +17 -0
  214. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file.h +99 -0
  215. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/jq.h +65 -0
  216. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/json.h +19 -0
  217. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/mem.h +19 -0
  218. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/memmem.h +13 -0
  219. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/os.h +54 -0
  220. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite.h +71 -0
  221. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite_writer.h +53 -0
  222. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/prop.h +107 -0
  223. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/signal.h +18 -0
  224. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/sql.h +11 -0
  225. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/string.h +148 -0
  226. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/utf8.h +41 -0
  227. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/win/dl.h +25 -0
  228. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/writer.h +101 -0
  229. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/zsv_export.h +33 -0
  230. data/ext/zsv/vendor/zsv-1.3.0/include/zsv.h +20 -0
  231. data/ext/zsv/vendor/zsv-1.3.0/src/vector_delim.c +60 -0
  232. data/ext/zsv/vendor/zsv-1.3.0/src/zsv.c +484 -0
  233. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_internal.c +731 -0
  234. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_delim.c +285 -0
  235. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_fixed.c +88 -0
  236. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_strencode.c +51 -0
  237. data/ext/zsv/zsv_ext.c +343 -0
  238. data/lib/zsv/version.rb +5 -0
  239. data/lib/zsv.rb +81 -0
  240. metadata +340 -0
@@ -0,0 +1,1007 @@
1
+ #ifndef _GNU_SOURCE
2
+ #define _GNU_SOURCE
3
+ #endif
4
+ #ifndef _XOPEN_SOURCE_EXTENDED
5
+ #define _XOPEN_SOURCE_EXTENDED
6
+ #endif
7
+ #include <assert.h>
8
+ #include <stdint.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+ #include <stdarg.h>
13
+ #include <ctype.h>
14
+ #include <errno.h>
15
+ #include <signal.h>
16
+
17
+ #include <zsv.h>
18
+
19
+ #include "sheet/curses.h"
20
+
21
+ #include <locale.h>
22
+ #include <wchar.h>
23
+ #include <pthread.h>
24
+
25
+ #define ZSV_COMMAND sheet
26
+ #include "zsv_command.h"
27
+
28
+ #include "../include/zsv/ext/sheet.h"
29
+ #include "../include/zsv/utils/string.h"
30
+ #include "sheet/sheet_internal.h"
31
+ #include "sheet/screen_buffer.c"
32
+ #include "sheet/lexer.c"
33
+ #include "sheet/procedure.c"
34
+
35
+ /* TODO: move this somewhere else like common or utils */
36
+ #define UNUSED(X) ((void)X)
37
+
38
+ struct zsvsheet_opts {
39
+ char hide_row_nums; // if 1, will load row nums
40
+ const char *find;
41
+ size_t find_specified_column_plus_1; // if 0, find in any column; else only search specified column (1-based)
42
+ char find_exact; // later: make this a bitfield with different options e.g. case-insensitive, regex etc
43
+ size_t found_rownum;
44
+ size_t found_colnum;
45
+ };
46
+
47
+ #include "sheet/utf8-width.c"
48
+ #include "sheet/ui_buffer.c"
49
+ #include "sheet/index.c"
50
+ #include "sheet/read-data.c"
51
+ #include "sheet/key-bindings.c"
52
+
53
+ #define ZSVSHEET_CELL_DISPLAY_MIN_WIDTH 10
54
+ static size_t zsvsheet_cell_display_width(struct zsvsheet_ui_buffer *ui_buffer,
55
+ struct zsvsheet_display_dimensions *ddims) {
56
+ size_t width = ddims->columns /
57
+ (ui_buffer->dimensions.col_count + (ui_buffer->rownum_col_offset && !ui_buffer->has_row_num ? 1 : 0));
58
+ return width < ZSVSHEET_CELL_DISPLAY_MIN_WIDTH ? ZSVSHEET_CELL_DISPLAY_MIN_WIDTH : width;
59
+ }
60
+
61
+ static void display_buffer_subtable(struct zsvsheet_ui_buffer *ui_buffer, size_t input_header_span,
62
+ struct zsvsheet_display_dimensions *ddims);
63
+
64
+ static void zsvsheet_priv_set_status(const struct zsvsheet_display_dimensions *ddims, int overwrite, const char *fmt,
65
+ ...);
66
+
67
+ struct zsvsheet_display_info {
68
+ int update_buffer;
69
+ struct zsvsheet_display_dimensions *dimensions;
70
+ size_t header_span;
71
+ struct {
72
+ struct zsvsheet_ui_buffer **base;
73
+ struct zsvsheet_ui_buffer **current;
74
+ } ui_buffers;
75
+ };
76
+
77
+ struct zsvsheet_sheet_context {
78
+ struct zsvsheet_display_info display_info;
79
+ char *find;
80
+ char *goto_column;
81
+ struct zsv_prop_handler *custom_prop_handler;
82
+ };
83
+
84
+ static void get_subcommand(const char *prompt, char *buff, size_t buffsize, int footer_row, const char *default_value) {
85
+ *buff = '\0';
86
+
87
+ // this is a hack to blank-out the currently-selected cell value
88
+ int max_screen_width = 256; // to do: don't assume this
89
+ for (int i = 0; i < max_screen_width; i++)
90
+ mvprintw(footer_row, i, "%c", ' ');
91
+
92
+ if (default_value && *default_value)
93
+ mvprintw(footer_row, 0, "%s: %s", prompt, default_value);
94
+ else
95
+ mvprintw(footer_row, 0, "%s: ", prompt);
96
+
97
+ int ch;
98
+ int y, x;
99
+ getyx(stdscr, y, x); // Get the current cursor position after the prompt
100
+ if (default_value) {
101
+ strncpy(buff, default_value, buffsize);
102
+ buff[buffsize - 1] = '\0';
103
+ }
104
+ size_t idx = default_value ? strlen(default_value) : 0;
105
+ if (default_value)
106
+ x -= strlen(default_value);
107
+ while (1) {
108
+ ch = getch(); // Read a character from the user
109
+ if (ch == 27) { // escape
110
+ buff[0] = '\0'; // Clear buffer & exit
111
+ break; // Exit the loop
112
+ } else if (ch == '\n' || ch == '\r') { // ENTER key
113
+ buff[idx] = '\0'; // Null-terminate the string
114
+ break; // Exit the loop
115
+ } else if (ch == ZSVSHEET_CTRL('A')) {
116
+ while (idx > 0) {
117
+ idx--;
118
+ buff[idx] = '\0';
119
+ mvwdelch(stdscr, y, x + idx); // Move cursor back to start
120
+ }
121
+ } else if (ch == KEY_BACKSPACE || ch == 127 || ch == '\b') { // BACKSPACE key
122
+ if (idx > 0) {
123
+ idx--;
124
+ buff[idx] = '\0';
125
+ mvwdelch(stdscr, y, x + idx); // Move cursor back and delete character
126
+ }
127
+ } else if (isprint(ch)) { // Printable character
128
+ if (idx < buffsize - 1) {
129
+ buff[idx++] = ch;
130
+ buff[idx] = '\0';
131
+ addch(ch); // Echo the character
132
+ }
133
+ }
134
+ // Ignore other keys
135
+ }
136
+ }
137
+
138
+ zsvsheet_status zsvsheet_ext_prompt(struct zsvsheet_proc_context *ctx, char *buffer, size_t bufsz, const char *fmt,
139
+ ...) {
140
+ struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
141
+ struct zsvsheet_display_info *di = &state->display_info;
142
+
143
+ int prompt_footer_row = (int)(di->dimensions->rows - di->dimensions->footer_span);
144
+ char prompt_buffer[256] = {0};
145
+
146
+ va_list argv;
147
+ va_start(argv, fmt);
148
+ int n = vsnprintf(prompt_buffer, sizeof(prompt_buffer), fmt, argv);
149
+ va_end(argv);
150
+
151
+ if (!(n > 0 && (size_t)n < sizeof(prompt_buffer)))
152
+ return zsvsheet_status_ok;
153
+
154
+ get_subcommand(prompt_buffer, buffer, bufsz, prompt_footer_row, NULL);
155
+ if (*prompt_buffer == '\0')
156
+ return zsvsheet_status_ok;
157
+
158
+ return zsvsheet_status_ok;
159
+ }
160
+
161
+ size_t zsvsheet_get_input_raw_row(struct zsvsheet_rowcol *input_offset, struct zsvsheet_rowcol *buff_offset,
162
+ size_t cursor_row) {
163
+ return input_offset->row + buff_offset->row + cursor_row;
164
+ }
165
+
166
+ #include "sheet/cursor.c"
167
+
168
+ struct zsvsheet_display_dimensions get_display_dimensions(size_t header_span, size_t footer_span) {
169
+ struct zsvsheet_display_dimensions dims = {0};
170
+ int max_y, max_x;
171
+ getmaxyx(stdscr, max_y, max_x);
172
+
173
+ if (max_y < (int)(header_span + footer_span + 1))
174
+ dims.rows = header_span + footer_span + 1;
175
+ else
176
+ dims.rows = (size_t)max_y;
177
+ if (max_x < 12)
178
+ dims.columns = 12;
179
+ else
180
+ dims.columns = (size_t)max_x;
181
+ dims.header_span = header_span;
182
+ dims.footer_span = footer_span;
183
+ return dims;
184
+ }
185
+
186
+ size_t display_data_rowcount(struct zsvsheet_display_dimensions *dims) {
187
+ return dims->rows - dims->footer_span - dims->header_span;
188
+ }
189
+
190
+ char zsvsheet_status_text[256] = {0};
191
+ static void zsvsheet_display_status_text(const struct zsvsheet_display_dimensions *ddims) {
192
+ // clear the entire line
193
+ mvprintw(ddims->rows - ddims->footer_span, 0, "%-*s", (int)sizeof(zsvsheet_status_text), "");
194
+
195
+ // add status, highlighted
196
+ attron(A_REVERSE);
197
+ mvprintw(ddims->rows - ddims->footer_span, 0, "%s", zsvsheet_status_text);
198
+ attroff(A_REVERSE);
199
+ }
200
+
201
+ static void zsvsheet_priv_set_status(const struct zsvsheet_display_dimensions *ddims, int overwrite, const char *fmt,
202
+ ...) {
203
+ if (overwrite || !*zsvsheet_status_text) {
204
+ va_list argv;
205
+ va_start(argv, fmt);
206
+ int n = vsnprintf(zsvsheet_status_text, sizeof(zsvsheet_status_text), fmt, argv);
207
+ if (n > 0 && (size_t)(n + 2) < sizeof(zsvsheet_status_text) && zsvsheet_status_text[n - 1] != ' ') {
208
+ zsvsheet_status_text[n] = ' ';
209
+ zsvsheet_status_text[n + 1] = '\0';
210
+ }
211
+ va_end(argv);
212
+ // note: if (n < (int)sizeof(zsvsheet_status_text)), then we just ignore
213
+ }
214
+ zsvsheet_display_status_text(ddims);
215
+ }
216
+
217
+ #include "sheet/terminfo.c"
218
+ #include "sheet/handlers_internal.h"
219
+ #include "sheet/handlers.c"
220
+ #include "sheet/file.c"
221
+ #include "sheet/usage.c"
222
+ #include "sheet/transformation.c"
223
+
224
+ struct zsvsheet_key_data *zsvsheet_key_handlers = NULL;
225
+ struct zsvsheet_key_data **zsvsheet_next_key_handler = &zsvsheet_key_handlers;
226
+
227
+ /* Common page movement function */
228
+ // TO DO: get rid of di->header_span. Just always assume it is 1
229
+ static zsvsheet_status zsvsheet_move_page(struct zsvsheet_display_info *di, bool up) {
230
+ size_t current, target;
231
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
232
+
233
+ if (up && current_ui_buffer->dimensions.row_count <= di->header_span)
234
+ return zsvsheet_status_ok; // no data
235
+
236
+ current = zsvsheet_get_input_raw_row(&current_ui_buffer->input_offset, &current_ui_buffer->buff_offset,
237
+ current_ui_buffer->cursor_row);
238
+
239
+ if (up) {
240
+ if (current <= display_data_rowcount(di->dimensions) + di->header_span) {
241
+ return zsvsheet_status_ok; // already at top
242
+ } else {
243
+ target = current - display_data_rowcount(di->dimensions);
244
+ if (target >= current_ui_buffer->dimensions.row_count)
245
+ target = current_ui_buffer->dimensions.row_count > 0 ? current_ui_buffer->dimensions.row_count - 1 : 0;
246
+ }
247
+ } else {
248
+ if (current >= current_ui_buffer->dimensions.row_count - display_data_rowcount(di->dimensions)) {
249
+ return zsvsheet_status_ok; // already at bottom
250
+ } else {
251
+ target = current + display_data_rowcount(di->dimensions);
252
+ }
253
+ }
254
+
255
+ di->update_buffer = zsvsheet_goto_input_raw_row(current_ui_buffer, target, di->header_span, di->dimensions,
256
+ current_ui_buffer->cursor_row);
257
+ return zsvsheet_status_ok;
258
+ }
259
+
260
+ /* Common vertical movement function */
261
+ static zsvsheet_status zsvsheet_move_ver(struct zsvsheet_display_info *di, bool up) {
262
+ size_t current;
263
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
264
+ current = zsvsheet_get_input_raw_row(&current_ui_buffer->input_offset, &current_ui_buffer->buff_offset,
265
+ current_ui_buffer->cursor_row);
266
+ if (up) {
267
+ if (current > di->header_span) {
268
+ di->update_buffer =
269
+ zsvsheet_goto_input_raw_row(current_ui_buffer, current - 1, di->header_span, di->dimensions,
270
+ current_ui_buffer->cursor_row > 0 ? current_ui_buffer->cursor_row - 1 : 0);
271
+ } else if (current_ui_buffer->cursor_row > 0) {
272
+ current_ui_buffer->cursor_row--;
273
+ }
274
+ } else {
275
+ if (current >= current_ui_buffer->dimensions.row_count - 1)
276
+ return zsvsheet_status_ok;
277
+ di->update_buffer = zsvsheet_goto_input_raw_row(current_ui_buffer, current + 1, di->header_span, di->dimensions,
278
+ current_ui_buffer->cursor_row + 1);
279
+ }
280
+ return zsvsheet_status_ok;
281
+ }
282
+
283
+ /* Common horizontal movement function */
284
+ static zsvsheet_status zsvsheet_move_hor(struct zsvsheet_display_info *di, bool right) {
285
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
286
+
287
+ if (right) {
288
+ cursor_right(di->dimensions->columns, zsvsheet_cell_display_width(current_ui_buffer, di->dimensions),
289
+ current_ui_buffer->dimensions.col_count + current_ui_buffer->rownum_col_offset >
290
+ zsvsheet_screen_buffer_cols(current_ui_buffer->buffer)
291
+ ? zsvsheet_screen_buffer_cols(current_ui_buffer->buffer)
292
+ : current_ui_buffer->dimensions.col_count + current_ui_buffer->rownum_col_offset,
293
+ &current_ui_buffer->cursor_col, &current_ui_buffer->buff_offset.col);
294
+ } else {
295
+ if (current_ui_buffer->cursor_col > 0) {
296
+ current_ui_buffer->cursor_col--;
297
+ } else if (current_ui_buffer->buff_offset.col > 0) {
298
+ current_ui_buffer->buff_offset.col--;
299
+ }
300
+ }
301
+ return zsvsheet_status_ok;
302
+ }
303
+
304
+ /* Common vertical movement between extremes */
305
+ static zsvsheet_status zsvsheet_move_ver_end(struct zsvsheet_display_info *di, bool top) {
306
+ size_t current;
307
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
308
+
309
+ current = zsvsheet_get_input_raw_row(&current_ui_buffer->input_offset, &current_ui_buffer->buff_offset,
310
+ current_ui_buffer->cursor_row);
311
+ UNUSED(current);
312
+
313
+ if (top) {
314
+ di->update_buffer =
315
+ zsvsheet_goto_input_raw_row(current_ui_buffer, 1, di->header_span, di->dimensions, di->dimensions->header_span);
316
+ } else {
317
+ if (current_ui_buffer->dimensions.row_count == 0)
318
+ return zsvsheet_status_ok;
319
+ if (current_ui_buffer->dimensions.row_count <= di->dimensions->rows - di->dimensions->footer_span) {
320
+ current_ui_buffer->cursor_row = current_ui_buffer->dimensions.row_count - 1;
321
+ } else {
322
+ di->update_buffer =
323
+ zsvsheet_goto_input_raw_row(current_ui_buffer, current_ui_buffer->dimensions.row_count - 1, di->header_span,
324
+ di->dimensions, di->dimensions->rows - di->dimensions->header_span - 1);
325
+ }
326
+ }
327
+ return zsvsheet_status_ok;
328
+ }
329
+
330
+ static zsvsheet_status zsvsheet_move_hor_to(struct zsvsheet_display_info *di, size_t move_to) {
331
+ // to do: directly set current_ui_buffer->cursor_col and buff_offset.col
332
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
333
+ enum zsvsheet_status stat = zsvsheet_status_ok;
334
+ size_t start_col = current_ui_buffer->buff_offset.col;
335
+ size_t current_absolute_col = start_col + current_ui_buffer->cursor_col;
336
+ if (current_absolute_col < move_to) {
337
+ for (size_t i = 0, j = move_to - current_absolute_col; i < j; i++) {
338
+ if ((stat = zsvsheet_move_hor(di, true)) != zsvsheet_status_ok)
339
+ break;
340
+ }
341
+ } else if (current_absolute_col > move_to) {
342
+ for (size_t i = 0, j = current_absolute_col - move_to; i < j; i++) {
343
+ if ((stat = zsvsheet_move_hor(di, false)) != zsvsheet_status_ok)
344
+ break;
345
+ }
346
+ }
347
+ return stat;
348
+ }
349
+
350
+ /* Common horizontal movement between extremes */
351
+ static zsvsheet_status zsvsheet_move_hor_end(struct zsvsheet_display_info *di, bool right) {
352
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
353
+ if (right)
354
+ zsvsheet_move_hor_to(di, current_ui_buffer->dimensions.col_count);
355
+ else
356
+ zsvsheet_move_hor_to(di, 0);
357
+ return zsvsheet_status_ok;
358
+ }
359
+
360
+ // zsvsheet_handle_find_next: return non-zero if a result was found
361
+ static char zsvsheet_handle_find_next(struct zsvsheet_display_info *di, struct zsvsheet_ui_buffer *uib,
362
+ const char *needle, size_t specified_column_plus_1, char find_exact,
363
+ size_t header_span, struct zsvsheet_display_dimensions *ddims, int *update_buffer,
364
+ struct zsv_prop_handler *custom_prop_handler) {
365
+ struct zsvsheet_opts zsvsheet_opts = {0};
366
+ zsvsheet_opts.find = needle;
367
+ zsvsheet_opts.find_specified_column_plus_1 = specified_column_plus_1;
368
+ zsvsheet_opts.find_exact = find_exact;
369
+ zsvsheet_opts.found_rownum = 0;
370
+ zsvsheet_opts.found_colnum = uib->cursor_col + uib->buff_offset.col;
371
+ if (zsvsheet_find_next(uib, &zsvsheet_opts, header_span, custom_prop_handler) > 0) {
372
+ *update_buffer = zsvsheet_goto_input_raw_row(uib, zsvsheet_opts.found_rownum, header_span, ddims, (size_t)-1);
373
+
374
+ // move to zsvsheet_opts->found_colnum; + 1 to skip the "Row #" column
375
+ zsvsheet_move_hor_to(di, zsvsheet_opts.found_colnum + 1);
376
+ return 1;
377
+ }
378
+ zsvsheet_priv_set_status(ddims, 1, "Not found");
379
+ return 0;
380
+ }
381
+
382
+ /* Find column handler: case-insensitive column header find */
383
+ static zsvsheet_status zsvsheet_goto_column(struct zsvsheet_sheet_context *state) {
384
+ struct zsvsheet_display_info *di = &state->display_info;
385
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
386
+ if (zsvsheet_buffer_data_filename(current_ui_buffer)) {
387
+ char prompt_buffer[256] = {0};
388
+ int prompt_footer_row = (int)(di->dimensions->rows - di->dimensions->footer_span);
389
+ get_subcommand("Find column", prompt_buffer, sizeof(prompt_buffer), prompt_footer_row, state->goto_column);
390
+ if (*prompt_buffer != '\0') {
391
+ free(state->goto_column);
392
+ state->goto_column = strdup(prompt_buffer);
393
+ if (state->goto_column) {
394
+ size_t len_lc = strlen(state->goto_column);
395
+ int err;
396
+ unsigned char *find_lc = zsv_strtolowercase_w_err((const unsigned char *)state->goto_column, &len_lc, &err);
397
+ if (find_lc && len_lc) {
398
+ zsvsheet_screen_buffer_t buffer = current_ui_buffer->buffer;
399
+ size_t colcount = zsvsheet_screen_buffer_cols(buffer);
400
+ size_t found = 0; // if found, will equal 1 + column index
401
+ for (size_t i = 0; !found && i < colcount; i++) {
402
+ const unsigned char *colname = zsvsheet_screen_buffer_cell_display(buffer, 0, i);
403
+ if (colname && *colname) {
404
+ size_t colname_len_lc = strlen((const char *)colname);
405
+ unsigned char *colname_lc = zsv_strtolowercase_w_err(colname, &colname_len_lc, &err);
406
+ if (colname_lc && colname_len_lc > 0 && memmem(colname_lc, colname_len_lc, find_lc, len_lc))
407
+ found = i + 1;
408
+ free(colname_lc);
409
+ }
410
+ }
411
+ free(find_lc);
412
+ if (found)
413
+ zsvsheet_move_hor_to(di, found - 1);
414
+ }
415
+ }
416
+ }
417
+ }
418
+ return 0;
419
+ }
420
+
421
+ /* Find and find-next handler */
422
+ static zsvsheet_status zsvsheet_find(struct zsvsheet_sheet_context *state, bool next) {
423
+ struct zsvsheet_display_info *di = &state->display_info;
424
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(di->ui_buffers.current);
425
+
426
+ if (!zsvsheet_buffer_data_filename(current_ui_buffer))
427
+ goto out;
428
+
429
+ if (!next) {
430
+ char prompt_buffer[256] = {0};
431
+ int prompt_footer_row = (int)(di->dimensions->rows - di->dimensions->footer_span);
432
+ // to do: support regex
433
+ get_subcommand("Find", prompt_buffer, sizeof(prompt_buffer), prompt_footer_row, NULL);
434
+ if (*prompt_buffer == '\0') {
435
+ goto out;
436
+ } else {
437
+ free(state->find);
438
+ state->find = strdup(prompt_buffer);
439
+ }
440
+ }
441
+
442
+ if (state->find) {
443
+ zsvsheet_handle_find_next(di, current_ui_buffer, state->find, 0, 0, // any column, non-exact
444
+ di->header_span, di->dimensions, &di->update_buffer, state->custom_prop_handler);
445
+ }
446
+
447
+ out:
448
+ return zsvsheet_status_ok;
449
+ }
450
+
451
+ static zsvsheet_status zsvsheet_open_file_handler(struct zsvsheet_proc_context *ctx) {
452
+ // TODO: should be PATH_MAX but that's going to be about a page and compiler
453
+ // might complain about stack being too large. Probably move to handler
454
+ // state or something.
455
+ // TODO: allow additional zsv options
456
+ char prompt_buffer[256] = {0};
457
+ struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
458
+ const char *filename;
459
+
460
+ struct zsvsheet_display_info *di = &state->display_info;
461
+ int prompt_footer_row = (int)(di->dimensions->rows - di->dimensions->footer_span);
462
+ int err;
463
+
464
+ UNUSED(ctx);
465
+
466
+ if (ctx->num_params > 0) {
467
+ filename = strdup(ctx->params[0].u.string);
468
+ } else {
469
+ if (!ctx->invocation.interactive)
470
+ return zsvsheet_status_error;
471
+ get_subcommand("File to open", prompt_buffer, sizeof(prompt_buffer), prompt_footer_row, NULL);
472
+ if (*prompt_buffer == '\0')
473
+ goto no_input;
474
+ filename = strdup(prompt_buffer);
475
+ }
476
+
477
+ if ((err = zsvsheet_ui_buffer_open_file(filename, NULL, state->custom_prop_handler, di->ui_buffers.base,
478
+ di->ui_buffers.current))) {
479
+ if (err > 0)
480
+ zsvsheet_priv_set_status(di->dimensions, 1, "%s: %s", filename, strerror(err));
481
+ else if (err < 0)
482
+ zsvsheet_priv_set_status(di->dimensions, 1, "Unexpected error");
483
+ else
484
+ zsvsheet_priv_set_status(di->dimensions, 1, "Not found: %s", filename);
485
+ return zsvsheet_status_ignore;
486
+ }
487
+ no_input:
488
+ return zsvsheet_status_ok;
489
+ }
490
+
491
+ #include "sheet/filter.c"
492
+
493
+ static zsvsheet_status zsvsheet_filter_handler(struct zsvsheet_proc_context *ctx) {
494
+ char prompt_buffer[256] = {0};
495
+ struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
496
+ struct zsvsheet_display_info *di = &state->display_info;
497
+ struct zsvsheet_ui_buffer *current_ui_buffer = *state->display_info.ui_buffers.current;
498
+ size_t single_column = 0;
499
+ if (ctx->proc_id == zsvsheet_builtin_proc_filter_this) {
500
+ struct zsvsheet_rowcol rc;
501
+ zsvsheet_buffer_t buff = zsvsheet_buffer_current(ctx);
502
+ if (zsvsheet_buffer_get_selected_cell(buff, &rc) != zsvsheet_status_ok)
503
+ return zsvsheet_status_error;
504
+ single_column = rc.col;
505
+ }
506
+ int prompt_footer_row = (int)(di->dimensions->rows - di->dimensions->footer_span);
507
+ struct zsvsheet_buffer_info_internal binfo = zsvsheet_buffer_info_internal(current_ui_buffer);
508
+ const char *filter;
509
+
510
+ if (binfo.write_in_progress && !binfo.write_done)
511
+ return zsvsheet_status_busy;
512
+
513
+ if (!zsvsheet_buffer_data_filename(current_ui_buffer))
514
+ goto out;
515
+
516
+ if (ctx->num_params > 0) {
517
+ filter = ctx->params[0].u.string;
518
+ } else {
519
+ if (!ctx->invocation.interactive)
520
+ return zsvsheet_status_error;
521
+ if (single_column)
522
+ get_subcommand("Filter (this column)", prompt_buffer, sizeof(prompt_buffer), prompt_footer_row, NULL);
523
+ else
524
+ get_subcommand("Filter", prompt_buffer, sizeof(prompt_buffer), prompt_footer_row, NULL);
525
+ if (*prompt_buffer == '\0')
526
+ goto out;
527
+ filter = prompt_buffer;
528
+ }
529
+
530
+ return zsvsheet_filter_file(ctx, filter, single_column);
531
+ out:
532
+ return zsvsheet_status_ok;
533
+ }
534
+
535
+ static zsvsheet_status zsvsheet_subcommand_handler(struct zsvsheet_proc_context *ctx) {
536
+ char prompt_buffer[256] = {0};
537
+ struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
538
+ struct zsvsheet_display_info *di = &state->display_info;
539
+ int prompt_footer_row = (int)(di->dimensions->rows - di->dimensions->footer_span);
540
+
541
+ get_subcommand("", prompt_buffer, sizeof(prompt_buffer), prompt_footer_row, NULL);
542
+ if (*prompt_buffer == '\0')
543
+ return zsvsheet_status_ok;
544
+
545
+ struct zsvsheet_proc_context context = {
546
+ .invocation.interactive = true,
547
+ .invocation.type = zsvsheet_proc_invocation_type_proc,
548
+ .invocation.u.proc.id = ctx->proc_id,
549
+ .subcommand_context = ctx->subcommand_context,
550
+ };
551
+ zsvsheet_status rc = zsvsheet_proc_invoke_from_command(prompt_buffer, &context);
552
+ return rc;
553
+ }
554
+
555
+ static zsvsheet_status zsvsheet_buffer_new_static(struct zsvsheet_proc_context *ctx, const size_t cols,
556
+ const unsigned char **(*get_header)(void *cbctx),
557
+ const unsigned char **(*get_row)(void *cbctx),
558
+ const unsigned char *(*get_status)(void *cbctx), void *cbctx,
559
+ struct zsvsheet_screen_buffer_opts *boptsp) {
560
+ struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
561
+ struct zsvsheet_display_info *di = &state->display_info;
562
+ struct zsvsheet_screen_buffer_opts bopts = {
563
+ .no_rownum_column = 1,
564
+ .cell_buff_len = 64,
565
+ .max_cell_len = 0,
566
+ .rows = 256,
567
+ };
568
+ if (boptsp)
569
+ bopts = *boptsp;
570
+ struct zsvsheet_ui_buffer_opts uibopts = {
571
+ .buff_opts = &bopts,
572
+ .filename = NULL,
573
+ .data_filename = NULL,
574
+ .no_rownum_col_offset = 1,
575
+ .write_after_open = 0,
576
+ };
577
+ struct zsvsheet_ui_buffer *uib = NULL;
578
+ zsvsheet_screen_buffer_t buffer;
579
+ enum zsvsheet_priv_status pstat;
580
+ enum zsvsheet_status stat = zsvsheet_status_error;
581
+
582
+ buffer = zsvsheet_screen_buffer_new(cols, &bopts, &pstat);
583
+ if (pstat != zsvsheet_priv_status_ok)
584
+ goto free_buffer;
585
+
586
+ uib = zsvsheet_ui_buffer_new(buffer, &uibopts);
587
+ if (!uib)
588
+ goto free_buffer;
589
+
590
+ const unsigned char **head = get_header(cbctx);
591
+ for (size_t j = 0; j < cols && head[j]; j++) {
592
+ if (*head[j])
593
+ pstat = zsvsheet_screen_buffer_write_cell(buffer, 0, j, head[j]);
594
+ if (pstat != zsvsheet_priv_status_ok)
595
+ goto free_buffer;
596
+ }
597
+
598
+ size_t row = 1;
599
+ const unsigned char **row_data;
600
+ while ((row_data = get_row(cbctx)) != NULL) {
601
+ for (size_t j = 0; j < cols && row_data[j]; j++) {
602
+ if (*row_data[j]) {
603
+ pstat = zsvsheet_screen_buffer_write_cell(buffer, row, j, row_data[j]);
604
+ if (pstat != zsvsheet_priv_status_ok)
605
+ goto free_buffer;
606
+ }
607
+ }
608
+ row++;
609
+ }
610
+
611
+ uib->dimensions.col_count = cols;
612
+ uib->dimensions.row_count = row;
613
+ uib->buff_used_rows = row;
614
+
615
+ const unsigned char *status = get_status(cbctx);
616
+ if (status)
617
+ zsvsheet_ui_buffer_set_status(uib, (const char *)status);
618
+
619
+ zsvsheet_ui_buffer_push(di->ui_buffers.base, di->ui_buffers.current, uib);
620
+ stat = zsvsheet_status_ok;
621
+ goto out;
622
+
623
+ free_buffer:
624
+ if (uib)
625
+ zsvsheet_ui_buffer_delete(uib);
626
+ else
627
+ zsvsheet_screen_buffer_delete(buffer);
628
+ out:
629
+ return stat;
630
+ }
631
+
632
+ #include "sheet/help.c"
633
+ #include "sheet/errors.c"
634
+
635
+ #include "sheet/pivot.c"
636
+ #include "sheet/sqlfilter.c"
637
+ #include "sheet/newline_handler.c"
638
+
639
+ /* We do most procedures in one handler. More complex procedures can be
640
+ * separated into their own handlers.
641
+ */
642
+ zsvsheet_status zsvsheet_builtin_proc_handler(struct zsvsheet_proc_context *ctx) {
643
+ struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
644
+ struct zsvsheet_ui_buffer *current_ui_buffer = *(state->display_info.ui_buffers.current);
645
+
646
+ switch (ctx->proc_id) {
647
+ case zsvsheet_builtin_proc_quit:
648
+ return zsvsheet_status_exit;
649
+ case zsvsheet_builtin_proc_resize:
650
+ *(state->display_info.dimensions) = get_display_dimensions(1, 1);
651
+ break;
652
+
653
+ case zsvsheet_builtin_proc_move_up:
654
+ return zsvsheet_move_ver(&state->display_info, true);
655
+ case zsvsheet_builtin_proc_move_down:
656
+ return zsvsheet_move_ver(&state->display_info, false);
657
+ case zsvsheet_builtin_proc_move_right:
658
+ return zsvsheet_move_hor(&state->display_info, true);
659
+ case zsvsheet_builtin_proc_move_left:
660
+ return zsvsheet_move_hor(&state->display_info, false);
661
+
662
+ case zsvsheet_builtin_proc_pg_up:
663
+ return zsvsheet_move_page(&state->display_info, true);
664
+ case zsvsheet_builtin_proc_pg_down:
665
+ return zsvsheet_move_page(&state->display_info, false);
666
+
667
+ case zsvsheet_builtin_proc_move_top:
668
+ return zsvsheet_move_ver_end(&state->display_info, true);
669
+ case zsvsheet_builtin_proc_move_bottom:
670
+ return zsvsheet_move_ver_end(&state->display_info, false);
671
+
672
+ case zsvsheet_key_move_last_col:
673
+ return zsvsheet_move_hor_end(&state->display_info, true);
674
+ case zsvsheet_key_move_first_col:
675
+ return zsvsheet_move_hor_end(&state->display_info, false);
676
+
677
+ case zsvsheet_builtin_proc_escape:
678
+ // current_ui_buffer is not the base/blank buffer
679
+ if (current_ui_buffer->prior) {
680
+ if (zsvsheet_ui_buffer_pop(state->display_info.ui_buffers.base, state->display_info.ui_buffers.current, NULL)) {
681
+ state->display_info.update_buffer = 1;
682
+ }
683
+ }
684
+ break;
685
+
686
+ case zsvsheet_builtin_proc_find:
687
+ return zsvsheet_find(state, false);
688
+ case zsvsheet_builtin_proc_find_next:
689
+ return zsvsheet_find(state, true);
690
+ case zsvsheet_builtin_proc_goto_column:
691
+ return zsvsheet_goto_column(state);
692
+ }
693
+
694
+ return zsvsheet_status_error;
695
+ }
696
+
697
+ /* clang-format off */
698
+ struct builtin_proc_desc {
699
+ int proc_id;
700
+ const char *name;
701
+ const char *description;
702
+ zsvsheet_proc_fn handler;
703
+ } builtin_procedures[] = {
704
+ { zsvsheet_builtin_proc_quit, "quit", "Exit the application", zsvsheet_builtin_proc_handler },
705
+ { zsvsheet_builtin_proc_escape, "escape", "Leave the current view or cancel a subcommand", zsvsheet_builtin_proc_handler },
706
+ { zsvsheet_builtin_proc_move_bottom, "bottom", "Jump to the last row", zsvsheet_builtin_proc_handler },
707
+ { zsvsheet_builtin_proc_move_top, "top", "Jump to the first row", zsvsheet_builtin_proc_handler },
708
+ { zsvsheet_builtin_proc_move_first_col, "first", "Jump to the first column", zsvsheet_builtin_proc_handler },
709
+ { zsvsheet_builtin_proc_pg_down, "pagedown", "Move down one page", zsvsheet_builtin_proc_handler },
710
+ { zsvsheet_builtin_proc_pg_up, "pageup", "Move up one page", zsvsheet_builtin_proc_handler },
711
+ { zsvsheet_builtin_proc_move_last_col, "last", "Jump to the last column", zsvsheet_builtin_proc_handler },
712
+ { zsvsheet_builtin_proc_move_up, "up", "Move up one row", zsvsheet_builtin_proc_handler },
713
+ { zsvsheet_builtin_proc_move_down, "down", "Move down one row", zsvsheet_builtin_proc_handler },
714
+ { zsvsheet_builtin_proc_move_left, "left", "Move left one column", zsvsheet_builtin_proc_handler },
715
+ { zsvsheet_builtin_proc_move_right, "right", "Move right one column", zsvsheet_builtin_proc_handler },
716
+ { zsvsheet_builtin_proc_find, "find", "Set a search term and jump to the first result after the cursor", zsvsheet_builtin_proc_handler },
717
+ { zsvsheet_builtin_proc_find_next, "next", "Jump to the next search result", zsvsheet_builtin_proc_handler },
718
+ { zsvsheet_builtin_proc_goto_column, "gotocolumn", "Go to column", zsvsheet_builtin_proc_handler },
719
+ { zsvsheet_builtin_proc_resize, "resize", "Resize the layout to fit new terminal dimensions", zsvsheet_builtin_proc_handler },
720
+ { zsvsheet_builtin_proc_open_file, "open", "Open another CSV file", zsvsheet_open_file_handler },
721
+ { zsvsheet_builtin_proc_filter, "filter", "Filter by specified text", zsvsheet_filter_handler },
722
+ { zsvsheet_builtin_proc_filter_this, "filtercol", "Filter by specified text only in current column", zsvsheet_filter_handler },
723
+ { zsvsheet_builtin_proc_sqlfilter, "where", "Filter by sql expression", zsvsheet_sqlfilter_handler },
724
+ { zsvsheet_builtin_proc_subcommand, "subcommand", "Editor subcommand", zsvsheet_subcommand_handler },
725
+ { zsvsheet_builtin_proc_help, "help", "Display a list of actions and key-bindings", zsvsheet_help_handler },
726
+ { zsvsheet_builtin_proc_newline, "<Enter>", "Follow hyperlink (if any)", zsvsheet_newline_handler },
727
+ { zsvsheet_builtin_proc_pivot_cur_col, "pivot", "Group rows by the column under the cursor", zsvsheet_pivot_handler },
728
+ { zsvsheet_builtin_proc_pivot_expr, "pivotexpr", "Group rows with group-by SQL expression", zsvsheet_pivot_handler },
729
+ { zsvsheet_builtin_proc_errors, "errors", "Show errors (if any)", zsvsheet_errors_handler },
730
+ { zsvsheet_builtin_proc_errors_clear, "errors-clear","Clear any/all errors", zsvsheet_errors_handler },
731
+ { -1, NULL, NULL, NULL }
732
+ };
733
+ /* clang-format on */
734
+
735
+ void zsvsheet_register_builtin_procedures(void) {
736
+ for (struct builtin_proc_desc *desc = builtin_procedures; desc->proc_id != -1; ++desc) {
737
+ if (zsvsheet_register_builtin_proc(desc->proc_id, desc->name, desc->description, desc->handler) < 0) {
738
+ fprintf(stderr, "Failed to register builtin procedure\n");
739
+ }
740
+ }
741
+ }
742
+
743
+ static void zsvsheet_check_buffer_worker_updates(struct zsvsheet_ui_buffer *ub,
744
+ struct zsvsheet_display_dimensions *display_dims,
745
+ struct zsvsheet_sheet_context *handler_state) {
746
+ pthread_mutex_lock(&ub->mutex);
747
+ if (ub->status) {
748
+ if (display_dims)
749
+ zsvsheet_priv_set_status(display_dims, 1, "%s", ub->status);
750
+ }
751
+ if (ub->index_ready && ub->dimensions.row_count != ub->index->row_count + 1) {
752
+ ub->dimensions.row_count = ub->index->row_count + 1;
753
+ if (handler_state)
754
+ handler_state->display_info.update_buffer = true;
755
+ }
756
+ pthread_mutex_unlock(&ub->mutex);
757
+ }
758
+
759
+ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *optsp,
760
+ struct zsv_prop_handler *custom_prop_handler) {
761
+ if (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
762
+ zsvsheet_usage();
763
+ return zsv_status_ok;
764
+ }
765
+
766
+ #if defined(WIN32) || defined(_WIN32)
767
+ if (setenv("TERM", "", 1) != 0)
768
+ fprintf(stderr, "Warning: unable to unset TERM env var\n");
769
+ #endif
770
+
771
+ const char *locale = setlocale(LC_ALL, "C.UTF-8");
772
+ if (!locale || strstr(locale, "UTF-8") == NULL)
773
+ locale = setlocale(LC_ALL, "");
774
+ #if !defined(WIN32) && !defined(_WIN32)
775
+ if (!locale || strstr(locale, "UTF-8") == NULL) {
776
+ fprintf(stderr, "Unable to set locale to UTF-8\n");
777
+ return 1; // TO DO: option to continue
778
+ }
779
+ #endif
780
+
781
+ if (!terminfo_ok()) {
782
+ fprintf(stderr, "Warning: unable to set or detect TERMINFO\n");
783
+ return 1;
784
+ }
785
+
786
+ struct zsvsheet_ui_buffer *ui_buffers = NULL;
787
+ struct zsvsheet_ui_buffer *current_ui_buffer = NULL;
788
+
789
+ size_t header_span = 0; // number of rows that comprise the header
790
+ int err;
791
+ {
792
+ struct zsvsheet_ui_buffer *tmp_ui_buffer = NULL;
793
+ zsvsheet_ui_buffer_new_blank(&tmp_ui_buffer);
794
+ if (!tmp_ui_buffer) {
795
+ fprintf(stderr, "Out of memory!\n");
796
+ return ENOMEM;
797
+ }
798
+ zsvsheet_ui_buffer_push(&ui_buffers, &current_ui_buffer, tmp_ui_buffer);
799
+ }
800
+
801
+ if (argc > 1) {
802
+ const char *filename = argv[1];
803
+ if ((err = zsvsheet_ui_buffer_open_file(filename, optsp, custom_prop_handler, &ui_buffers, &current_ui_buffer))) {
804
+ if (err > 0)
805
+ perror(filename);
806
+ else
807
+ fprintf(stderr, "%s: no data found", filename); // to do: change this to a base-buff status msg
808
+
809
+ err = -1;
810
+ goto zsvsheet_exit;
811
+ }
812
+ }
813
+
814
+ err = 0;
815
+ header_span = 1;
816
+ initscr();
817
+ noecho();
818
+ keypad(stdscr, TRUE);
819
+ cbreak();
820
+ set_escdelay(30);
821
+ struct zsvsheet_display_dimensions display_dims = get_display_dimensions(1, 1);
822
+
823
+ zsvsheet_register_builtin_procedures();
824
+
825
+ /* TODO: allow user to pass key binding choice and dmux here */
826
+ zsvsheet_register_vim_key_bindings();
827
+ // zsvsheet_register_emacs
828
+ //...
829
+ //
830
+
831
+ int ch;
832
+ struct zsvsheet_sheet_context handler_state = {
833
+ .display_info.ui_buffers.base = &ui_buffers,
834
+ .display_info.ui_buffers.current = &current_ui_buffer,
835
+ .display_info.dimensions = &display_dims,
836
+ .display_info.header_span = header_span,
837
+ .find = NULL,
838
+ .custom_prop_handler = custom_prop_handler,
839
+ };
840
+
841
+ zsvsheet_status status;
842
+
843
+ #if defined(WIN32) || defined(_WIN32)
844
+ // induce delay for index to complete before checking updates (observed under WSL)
845
+ // maybe, need a thread coordination strategy using condition variable to proceed?
846
+ napms(100);
847
+ #endif
848
+
849
+ zsvsheet_check_buffer_worker_updates(current_ui_buffer, &display_dims, &handler_state);
850
+ display_buffer_subtable(current_ui_buffer, header_span, &display_dims);
851
+
852
+ // now ncurses getch() will fire every 2-tenths of a second so we can check for status update
853
+ halfdelay(2);
854
+
855
+ while (true) {
856
+ ch = getch();
857
+
858
+ handler_state.display_info.update_buffer = false;
859
+ zsvsheet_priv_set_status(&display_dims, 1, "");
860
+
861
+ if (ch != ERR) {
862
+ status = zsvsheet_key_press(ch, &handler_state);
863
+ if (status == zsvsheet_status_exit)
864
+ break;
865
+ if (status != zsvsheet_status_ok)
866
+ continue;
867
+ }
868
+
869
+ struct zsvsheet_ui_buffer *ub = current_ui_buffer;
870
+ zsvsheet_check_buffer_worker_updates(ub, &display_dims, &handler_state);
871
+
872
+ if (handler_state.display_info.update_buffer && zsvsheet_buffer_data_filename(ub)) {
873
+ struct zsvsheet_opts zsvsheet_opts = {0};
874
+ if (read_data(&ub, NULL, current_ui_buffer->input_offset.row, current_ui_buffer->input_offset.col, header_span,
875
+ &zsvsheet_opts, custom_prop_handler)) {
876
+ zsvsheet_priv_set_status(&display_dims, 1, "Unexpected error!"); // to do: better error message
877
+ continue;
878
+ }
879
+ }
880
+
881
+ display_buffer_subtable(ub, header_span, &display_dims);
882
+ }
883
+
884
+ endwin();
885
+ free(handler_state.find);
886
+ free(handler_state.goto_column);
887
+ zsvsheet_exit:
888
+ zsvsheet_ui_buffers_delete(current_ui_buffer);
889
+ zsvsheet_key_handlers_delete(&zsvsheet_key_handlers, &zsvsheet_next_key_handler);
890
+ return err;
891
+ }
892
+
893
+ const char *display_cell(struct zsvsheet_screen_buffer *buff, size_t data_row, size_t data_col, int row, int col,
894
+ size_t cell_display_width) {
895
+ char *str = (char *)zsvsheet_screen_buffer_cell_display(buff, data_row, data_col);
896
+ size_t len = str ? strlen(str) : 0;
897
+ int attrs = zsvsheet_screen_buffer_cell_attrs(buff, data_row, data_col);
898
+ if (attrs)
899
+ attron(attrs);
900
+ if (len == 0 || has_multibyte_char(str, len < cell_display_width ? len : cell_display_width) == 0)
901
+ mvprintw(row, col * cell_display_width, "%-*.*s", (int)cell_display_width, (int)cell_display_width - 1, str);
902
+ else {
903
+ size_t used_width;
904
+ int err = 0;
905
+ unsigned char *s = (unsigned char *)str;
906
+ size_t nbytes = utf8_bytes_up_to_max_width_and_replace_newlines(s, len, cell_display_width - 2, &used_width, &err);
907
+
908
+ // convert the substring to wide characters
909
+ wchar_t wsubstring[256]; // Ensure this buffer is large enough
910
+ #if defined(WIN32) || defined(_WIN32)
911
+ // windows does not have mbsnrtowcs
912
+ char *p = (char *)str;
913
+ char tmp_ch = p[nbytes];
914
+ p[nbytes] = '\0';
915
+ size_t wlen = mbstowcs(wsubstring, p, sizeof(wsubstring) / sizeof(wchar_t));
916
+ p[nbytes] = tmp_ch;
917
+ #else
918
+ const char *p = str;
919
+ size_t wlen = mbsnrtowcs(wsubstring, &p, nbytes, sizeof(wsubstring) / sizeof(wchar_t), NULL);
920
+ #endif
921
+ if (wlen == (size_t)-1) {
922
+ fprintf(stderr, "Unable to convert to wide chars: %s\n", str);
923
+ goto out;
924
+ }
925
+
926
+ // move to the desired position
927
+ move(row, col * cell_display_width);
928
+
929
+ // print the wide-character string with right padding
930
+ addnwstr(wsubstring, wlen);
931
+ for (size_t k = used_width; k < cell_display_width; k++)
932
+ addch(' ');
933
+ }
934
+ out:
935
+ if (attrs)
936
+ attroff(attrs);
937
+ return str;
938
+ }
939
+
940
+ static size_t zsvsheet_max_buffer_cols(struct zsvsheet_ui_buffer *ui_buffer) {
941
+ size_t col_count = ui_buffer->dimensions.col_count + (ui_buffer->rownum_col_offset ? 1 : 0);
942
+ return col_count > zsvsheet_screen_buffer_cols(ui_buffer->buffer) ? zsvsheet_screen_buffer_cols(ui_buffer->buffer)
943
+ : col_count;
944
+ }
945
+
946
+ static void display_buffer_subtable(struct zsvsheet_ui_buffer *ui_buffer, size_t input_header_span,
947
+ struct zsvsheet_display_dimensions *ddims) {
948
+ struct zsvsheet_screen_buffer *buffer = ui_buffer->buffer;
949
+ size_t start_row = ui_buffer->buff_offset.row;
950
+ size_t buffer_used_row_count = ui_buffer->buff_used_rows;
951
+ size_t start_col = ui_buffer->buff_offset.col;
952
+ size_t max_col_count = zsvsheet_max_buffer_cols(ui_buffer);
953
+ size_t cursor_row = ui_buffer->cursor_row;
954
+ size_t cursor_col = ui_buffer->cursor_col;
955
+
956
+ erase(); // use erase() instead of clear() to avoid changes being saved to
957
+ // screen buffer history
958
+ const size_t DATA_START_ROW = input_header_span;
959
+ const size_t SCREEN_DATA_START_ROW = ddims->header_span;
960
+
961
+ size_t SCREEN_DATA_ROWS = ddims->rows > SCREEN_DATA_START_ROW + 1 ? ddims->rows - SCREEN_DATA_START_ROW - 1 : 2;
962
+
963
+ size_t end_row = start_row + SCREEN_DATA_ROWS + DATA_START_ROW;
964
+ if (end_row > buffer_used_row_count)
965
+ end_row = buffer_used_row_count;
966
+
967
+ size_t cell_display_width = zsvsheet_cell_display_width(ui_buffer, ddims);
968
+ size_t end_col = start_col + ddims->columns / cell_display_width;
969
+ if (end_col > max_col_count)
970
+ end_col = max_col_count;
971
+
972
+ const char *cursor_value = NULL;
973
+ // First, display header row (buffer[0]) at screen row 0
974
+ attron(A_REVERSE);
975
+ for (size_t j = start_col; j < end_col; j++) {
976
+ if (cursor_row == 0 && cursor_col + start_col == j) {
977
+ attroff(A_REVERSE);
978
+ cursor_value = display_cell(buffer, 0, j, 0, j - start_col, cell_display_width);
979
+ attron(A_REVERSE);
980
+ } else
981
+ display_cell(buffer, 0, j, 0, j - start_col, cell_display_width);
982
+ }
983
+ attroff(A_REVERSE);
984
+
985
+ // Then display data starting from start_row + DATA_START_ROW
986
+ size_t screen_row = SCREEN_DATA_START_ROW;
987
+ for (size_t i = start_row + DATA_START_ROW; i < end_row; i++, screen_row++) {
988
+ for (size_t j = start_col; j < end_col; j++) {
989
+ if (screen_row == cursor_row && j == cursor_col + start_col) {
990
+ attron(A_REVERSE);
991
+ cursor_value = display_cell(buffer, i, j, screen_row, j - start_col, cell_display_width);
992
+ attroff(A_REVERSE);
993
+ } else {
994
+ display_cell(buffer, i, j, screen_row, j - start_col, cell_display_width);
995
+ }
996
+ }
997
+ }
998
+
999
+ if (ui_buffer->parse_errs.count > 0)
1000
+ zsvsheet_priv_set_status(ddims, 0, "? for help, :errors for errors");
1001
+ else
1002
+ zsvsheet_priv_set_status(ddims, 0, "? for help");
1003
+
1004
+ if (cursor_value)
1005
+ mvprintw(ddims->rows - ddims->footer_span, strlen(zsvsheet_status_text), "%s", cursor_value);
1006
+ refresh();
1007
+ }