terminals.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import { TerminalPStratumImplType } from "../strata.js";
  20. export class BytesPStratumImpl {
  21. static TYPE = TerminalPStratumImplType
  22. constructor (bytes, opt_i) {
  23. this.bytes = bytes;
  24. this.i = opt_i ?? 0;
  25. }
  26. next () {
  27. if ( this.i === this.bytes.length ) {
  28. return { done: true, value: undefined };
  29. }
  30. const i = this.i++;
  31. return { done: false, value: this.bytes[i] };
  32. }
  33. fork () {
  34. return new BytesPStratumImpl(this.bytes, this.i);
  35. }
  36. join (api, forked) {
  37. this.i = forked.i;
  38. }
  39. reach (api, start, end) {
  40. return this.bytes.slice(start, end);
  41. }
  42. }
  43. export class StringPStratumImpl {
  44. static TYPE = TerminalPStratumImplType
  45. constructor (str) {
  46. const encoder = new TextEncoder();
  47. const bytes = encoder.encode(str);
  48. this.delegate = new BytesPStratumImpl(bytes);
  49. }
  50. // DRY: proxy methods
  51. next (...a) {
  52. return this.delegate.next(...a);
  53. }
  54. fork (...a) {
  55. return this.delegate.fork(...a);
  56. }
  57. join (...a) {
  58. return this.delegate.join(...a);
  59. }
  60. reach (...a) {
  61. return this.delegate.reach(...a);
  62. }
  63. }