用 HTML 制作一个简单的终端

qwe*_*weq 3 html javascript css jquery twitter-bootstrap

我想使用 stdin 和 stdout 制作一个简单的控制台终端。基本上我有一个功能,可以输入一些东西并在聊天中检索一些东西。

如您所知,它会说类似的内容root@192.168.2.1:/,然后我只能在该行中输入。当我按回车/提交时,它会出现另一行root@192.168.2.1:/供我再次输入。

我如何使它只能在最后一行和前面输入root@192.168.2.1:/(基本上这是锁定的)?

有什么更好的想法来实现这个而不是 textarea 吗?

$('#btnSubmit').click(function() {
  var terminal = $('#terminal');
  var stdin = terminal.val();
  console.log(stdin);
  //Get stdout (FAKE FOR THE EXAMPLE)    
  terminal.append(stdout() + '<br>');
});


function stdout(stdin) {
  return "root@192.168.2.1:/"
}
Run Code Online (Sandbox Code Playgroud)
.terminal {
  background-color: #000;
  color: #fff
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<textarea class="terminal" rows="20" id="terminal"></textarea>
<button class="btn btn-primary" id="btnSubmit" type="button">Send</button>
Run Code Online (Sandbox Code Playgroud)

Jas*_*elf 5

就像你说的,你真的只需要写一行。你可以把那一行作为输入字段,然后在输入字段上方的 div 中输出“历史”。

这是一个例子,唯一看起来不像终端的东西是滚动的方式,但我想这应该可以用一些额外的代码来修复。

$(function() {
    $('.terminal').on('click', function() {
        $('#input').focus();
    });

    $('#input').on('keydown', function search(e) {
        if (e.keyCode == 13) {
            // append your output to the history,
            // here I just append the input
            $('#history').append($(this).val() + '<br/>');

            // you can change the path if you want
            // crappy implementation here, but you get the idea
            if ($(this).val().substring(0, 3) === 'cd ') {
                $('#path').html($(this).val().substring(3) + '&nbsp;>&nbsp;');
            }

            // clear the input
            $('#input').val('');

        }
    });
});
Run Code Online (Sandbox Code Playgroud)
.terminal{
  background: black;
  color: white;
  font: courier;
  padding: 10px;
  height: 100px;
  overflow: hidden;
}

.line{
  display: table;
  width: 100%;
}

.terminal span{
  display:table-cell; 
  width:1px;
}

.terminal input{
  display:table-cell; 
  width:100%;
  border: none;
  background: black;
  color: white;
  outline: none;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="terminal">
  <div id="history">
    
  </div>
  <div class="line">
    <span id="path">c:/&nbsp;>&nbsp;</span>
    <input type="text" id="input">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Lev*_*ano 1

您可以尝试插入一个可编辑的 div。

JSFiddle 终端示例

我所做的就是创建一个纯 CSS 解决方案。我创建了一个包装器,其中并排flex有静态和可编辑。div这将确保显示的任何数字输出都不会干扰用户输入。

HTML:

<section>
    <div class="static">IP ADDRESS</div>
    <div class="input" contenteditable="true"></div>
</section>
Run Code Online (Sandbox Code Playgroud)

CSS:

section {
    display: flex;
    flex-flow: row nowrap;
    width: 500px;
    height: auto;
    margin: 0 auto;
    background-color: lightgrey;
    font-family: monospace;
}
.static {
    width: auto;
    padding-right: 20px;
}
.input {
    flex: 1;
}
Run Code Online (Sandbox Code Playgroud)