Skip to content

A tiny project to power kotlin projects with *nix console control

License

Notifications You must be signed in to change notification settings

WuYuntaoTheGreat/ansipipe

Repository files navigation

ANSI Pipe

A tiny project to power kotlin projects with *nix console control

Purpose

The purpose of this project is to enable kotlin console programs to:

  • Detect single key press, e.g: key, direction keys
  • Output colorful texts
  • Control the position of console print.

The old solutions are:

  1. To include a native library (JNI) in java library, to control console input.
  2. To call native programs
  3. To create your own console GUI

The problems of those solutions are:

  1. Very complicated
  2. Have compatibility problems

Solution

This solution includes 2 parts:

  1. A shell script, responsible to read raw key press.
  2. A Kotlin program, implementing the main feature.

These 2 parts are connected by a pipe

+---------+       +--------------+      +------+      +-----------------+
|         | ----> |    Script    | ---> | Pipe | ---> |(stdin)          |
|         |       |(read raw key)|      |      |      |                 |
| Console |       +--------------+      +------+      | Kotlin Program  |
|         |                                           |                 |
|         | <---------------------------------------- |(stdout)         |
+---------+                                           +-----------------+
  1. The loops to read one key press
  2. When user press anything, the script will send the key to pipe
  3. The Kotlin program reads the pipe
  4. Then the Kotlin program print output to console, with ANSI code.

Precondition

  1. This library only support *nix shell environment (Linux, Mac OS X, Cygwin, MinGW, etc.). Windows cmd is not supported.
  2. Currently, only bash and zsh are supported. Other shells are not tested.

Demo

In project root, type following:

./script/demo.sh

Screenshot: screenshot

How to use

In build.gradle:

sourceControl {
    gitRepository("[email protected]:WuYuntaoTheGreat/ansipipe.git") {
        producesModule("cn.wuyatang.ansipipe:ansipipe")
    }
}

dependencies {
    ...

    implementation 'cn.wuyatang.ansipipe:ansipipe:v0.1.1'
}

About

A tiny project to power kotlin projects with *nix console control

Resources

License

Stars

Watchers

Forks

Packages

No packages published