(options: IntegerOptions)
| 211 | } |
| 212 | |
| 213 | function handleProcessInteger(options: IntegerOptions) { |
| 214 | return (input: Terminal.UserInput, state: State) => { |
| 215 | switch (input.key.name) { |
| 216 | case "backspace": { |
| 217 | return processBackspace(state) |
| 218 | } |
| 219 | case "k": |
| 220 | case "up": { |
| 221 | return Effect.succeed(Action.NextFrame({ |
| 222 | state: { |
| 223 | ...state, |
| 224 | value: state.value === "" || state.value === "-" |
| 225 | ? `${options.incrementBy}` |
| 226 | : `${Number.parseInt(state.value) + options.incrementBy}`, |
| 227 | error: Option.none() |
| 228 | } |
| 229 | })) |
| 230 | } |
| 231 | case "j": |
| 232 | case "down": { |
| 233 | return Effect.succeed(Action.NextFrame({ |
| 234 | state: { |
| 235 | ...state, |
| 236 | value: state.value === "" || state.value === "-" |
| 237 | ? `-${options.decrementBy}` |
| 238 | : `${Number.parseInt(state.value) - options.decrementBy}`, |
| 239 | error: Option.none() |
| 240 | } |
| 241 | })) |
| 242 | } |
| 243 | case "enter": |
| 244 | case "return": { |
| 245 | return Effect.matchEffect(parseInt(state.value), { |
| 246 | onFailure: () => |
| 247 | Effect.succeed(Action.NextFrame({ |
| 248 | state: { |
| 249 | ...state, |
| 250 | error: Option.some("Must provide an integer value") |
| 251 | } |
| 252 | })), |
| 253 | onSuccess: (n) => |
| 254 | Effect.match(options.validate(n), { |
| 255 | onFailure: (error) => |
| 256 | Action.NextFrame({ |
| 257 | state: { |
| 258 | ...state, |
| 259 | error: Option.some(error) |
| 260 | } |
| 261 | }), |
| 262 | onSuccess: (value) => Action.Submit({ value }) |
| 263 | }) |
| 264 | }) |
| 265 | } |
| 266 | default: { |
| 267 | const value = Option.getOrElse(input.input, () => "") |
| 268 | return defaultIntProcessor(state, value) |
| 269 | } |
| 270 | } |
no test coverage detected