hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/kernel/tty.rv
blob: 7ea24925b614ae52afdc6a3724182ec5bee55014 (plain)
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
# tty layer as opposed to a future window management server bullshit thingy
# this is just tty0 it adds nice shit like ^B and stuff
# also has history and shit

global tty = {
  line = "", cursor = 0, esc = 0,
  history = {}, hist_index = 0,

  fn back(self, n) do
    let i = 0
    while (i < n) do
      puts("\x08")
      i += 1
    end
  end,

  fn blanks(self, n) do
    let i = 0
    while (i < n) do
      puts(" ")
      i += 1
    end
  end,

  fn insert(self, character) do
    const pre = self.line[0..self.cursor]
    const post = self.line[self.cursor..]
    self.line = pre ~ character ~ post
    puts(character ~ post)
    self:back(post:len())
    self.cursor += 1
  end,

  fn backspace(self) do
    if (self.cursor == 0)
      return :nil

    const pre = self.line[0..(self.cursor - 1)]
    const post = self.line[self.cursor..]
    self.line = pre ~ post
    self.cursor -= 1

    puts("\x08")
    puts(post ~ " ")
    self:back(post:len() + 1)
  end,

  fn move_left(self) do
    if self.cursor > 0 do
      puts("\x08")
      self.cursor -= 1
    end
  end,

  fn move_right(self) do
    if self.cursor < self.line:len() do
      puts(self.line[self.cursor])
      self.cursor += 1
    end
  end,

  fn home(self) do
    self:back(self.cursor)
    self.cursor = 0
  end,

  fn to_end(self) do
    puts(self.line[self.cursor..])
    self.cursor = self.line:len()
  end,

  fn kill(self) do
    self:back(self.cursor)
    self:blanks(self.line:len())
    self:back(self.line:len())
    self.line = ""
    self.cursor = 0
  end,

  fn erase_word(self) do
    if (self.cursor == 0)
      return :nil

    let at = self.cursor
    while (at > 0 and self.line[at - 1] == " ")
      at -= 1
    while (at > 0 and self.line[at - 1] != " ")
      at -= 1

    let count = self.cursor - at
    while (count > 0) do
      self:backspace()
      count -= 1
    end
  end,

  fn replace_line(self, text) do
    self:kill()
    self.line = text
    puts(text)
    self.cursor = text:len()
  end,

  fn history_prev(self) do
    if (self.history:len() == 0)
      return :nil
    if (self.hist_index > 0)
      self.hist_index -= 1

    self:replace_line(self.history[self.hist_index])
  end,

  fn history_next(self) do
    if (self.history:len() == 0)
      return :nil

    if self.hist_index < self.history:len() - 1 do
      self.hist_index += 1
      self:replace_line(self.history[self.hist_index])
    end else do
      self.hist_index = self.history:len()
      self:replace_line("")
    end
  end,

  fn submit(self) do
    puts("\n")
    const entered = self.line
    if (entered:len() > 0)
      self.history:push(entered)

    self.hist_index = self.history:len()
    self.line = ""
    self.cursor = 0
    run(entered)
  end,

  fn cancel(self) do
    puts("^C\n")
    self.line = ""
    self.cursor = 0
    self.hist_index = self.history:len()
    puts(prompt())
  end,

  fn clear_screen(self) do
    console:clear()
    puts(prompt())
    puts(self.line)
    self:back(self.line:len() - self.cursor)
  end,

  fn feed(self, byte) do
    if (stdin_deliver(byte))
      return :nil

    if self.esc == 1 do
      if byte == 91 do
        self.esc = 2
      end else do
        self.esc = 0
      end

      return :nil
    end

    if self.esc == 2 do
      self.esc = 0

      match byte
      | 65 => self:history_prev() # up
      | 66 => self:history_next() # down
      | 67 => self:move_right()   # right
      | 68 => self:move_left()    # left
      | 72 => self:home()         # home
      | 70 => self:to_end()       # end
      | _ => :nil

      return :nil
    end

    # ESC, start a sequence
    if byte == 27 do
      self.esc = 1
      return :nil
    end

    match byte
    | 10 => self:submit()        # enter
    | 8 => self:backspace()      # bs
    | 127 => self:erase_word()   # del (ctrl-bs)
    | 1 => self:home()           # ctrl-a
    | 5 => self:to_end()         # ctrl-e
    | 3 => self:cancel()         # ctrl-c
    | 12 => self:clear_screen()  # ctrl-l
    | 21 => self:kill()          # ctrl-u
    | 23 => self:erase_word()    # ctrl-w
    | _ => do
        # ctrl-d and other shits
        if (byte < 32)
          return :nil
        if (byte > 126)
          return :nil
        self:insert(string.of_ascii(byte))
      end
  end,
}