Skip to content

Commit

Permalink
Provide a status bar, fixes #286
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 14, 2018
1 parent 51c4621 commit 27de765
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
39 changes: 39 additions & 0 deletions demo/etc/gosh_profile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@ if { $.reader } {
source ((($.reader parser) class) getResource "/gosh_profile")
}

status = {
p1 = $1
if { "on" equals $p1 } {
__status_on
} {
if { "off" equals $p1 } {
($.terminal status) update null
} {
echo "Error: use 'status on' or 'status off'."
}
}
}

__status_on = {
sb = (new org.jline.utils.AttributedStringBuilder)
$sb style (($sb style) foreground 2)
cols = ($.terminal width)
each {1..$cols} { $sb append $'\u2500' }
s1 = ($sb toAttributedString)
$sb setLength 0
$sb style (($sb style) foreground 0)
$sb append " "
$sb style (($sb style) inverse)
$sb append "^L"
$sb style (($sb style) inverseOff)
$sb append " Clear "
$sb style (($sb style) inverse)
$sb append "^D"
$sb style (($sb style) inverseOff)
$sb append " Exit "
$sb style (($sb style) inverse)
$sb append "^C"
$sb style (($sb style) inverseOff)
$sb append " Interrupt "
s2 = ($sb toAttributedString)
($.terminal status) update [ $s1 $s2 ]
}


__load_class_from = {
(($1 class) classLoader) loadClass $2
}
Expand Down
10 changes: 10 additions & 0 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.jline.utils.InfoCmp.Capability;
import org.jline.utils.Levenshtein;
import org.jline.utils.Log;
import org.jline.utils.Status;
import org.jline.utils.WCWidth;

import static org.jline.keymap.KeyMap.alt;
Expand Down Expand Up @@ -3460,6 +3461,11 @@ protected synchronized void redisplay(boolean flush) {
return;
}

Status status = Status.getStatus(terminal, false);
if (status != null) {
status.redraw();
}

if (size.getRows() > 0 && size.getRows() < MIN_ROWS) {
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);

Expand Down Expand Up @@ -5180,6 +5186,10 @@ public boolean clear() {
*/
public boolean clearScreen() {
if (terminal.puts(Capability.clear_screen)) {
Status status = Status.getStatus(terminal, false);
if (status != null) {
status.reset();
}
redrawLine();
} else {
println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.jline.terminal.impl;

import java.io.IOError;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.nio.charset.Charset;
Expand All @@ -32,6 +31,7 @@
import org.jline.utils.InfoCmp;
import org.jline.utils.InfoCmp.Capability;
import org.jline.utils.Log;
import org.jline.utils.Status;

public abstract class AbstractTerminal implements Terminal {

Expand All @@ -42,6 +42,7 @@ public abstract class AbstractTerminal implements Terminal {
protected final Set<Capability> bools = new HashSet<>();
protected final Map<Capability, Integer> ints = new HashMap<>();
protected final Map<Capability, String> strings = new HashMap<>();
protected Status status;

public AbstractTerminal(String name, String type) throws IOException {
this(name, type, null, SignalHandler.SIG_DFL);
Expand All @@ -56,6 +57,17 @@ public AbstractTerminal(String name, String type, Charset encoding, SignalHandle
}
}

public Status getStatus() {
return getStatus(true);
}

public Status getStatus(boolean create) {
if (status == null && create) {
status = new Status(this);
}
return status;
}

public SignalHandler handle(Signal signal, SignalHandler handler) {
Objects.requireNonNull(signal);
Objects.requireNonNull(handler);
Expand All @@ -68,9 +80,16 @@ public void raise(Signal signal) {
if (handler != SignalHandler.SIG_DFL && handler != SignalHandler.SIG_IGN) {
handler.handle(signal);
}
if (status != null && signal == Signal.WINCH) {
status.resize();
}
}

public void close() throws IOException {
if (status != null) {
status.update(null);
flush();
}
}

protected void echoSignal(Signal signal) {
Expand Down
96 changes: 96 additions & 0 deletions terminal/src/main/java/org/jline/utils/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2002-2018, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package org.jline.utils;

import java.util.Objects;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import org.jline.terminal.Terminal;
import org.jline.terminal.Terminal.Signal;
import org.jline.terminal.Terminal.SignalHandler;
import org.jline.terminal.impl.AbstractTerminal;
import org.jline.utils.InfoCmp.Capability;
import org.jline.terminal.Size;

public class Status {

protected final AbstractTerminal terminal;
protected final boolean supported;
protected List<AttributedString> oldLines = Collections.emptyList();
protected int rows;
protected int columns;
protected boolean force;

public static Status getStatus(Terminal terminal) {
return getStatus(terminal, true);
}

public static Status getStatus(Terminal terminal, boolean create) {
return terminal instanceof AbstractTerminal
? ((AbstractTerminal) terminal).getStatus(create)
: null;
}


public Status(AbstractTerminal terminal) {
this.terminal = Objects.requireNonNull(terminal, "terminal can not be null");
this.supported = terminal.getStringCapability(Capability.change_scroll_region) != null
&& terminal.getStringCapability(Capability.save_cursor) != null
&& terminal.getStringCapability(Capability.restore_cursor) != null
&& terminal.getStringCapability(Capability.cursor_address) != null;
if (supported) {
resize();
}
}

public void resize() {
Size size = terminal.getSize();
this.rows = size.getRows();
this.columns = size.getColumns();
this.force = true;
}

public void reset() {
this.force = true;
}

public void redraw() {
update(oldLines);
}

public void update(List<AttributedString> lines) {
if (lines == null) {
lines = Collections.emptyList();
}
if (!supported || (oldLines.equals(lines) && !force)) {
return;
}
int nb = lines.size() - oldLines.size();
if (nb > 0) {
for (int i = 0; i < nb; i++) {
terminal.puts(Capability.cursor_down);
}
for (int i = 0; i < nb; i++) {
terminal.puts(Capability.cursor_up);
}
}
terminal.puts(Capability.save_cursor);
terminal.puts(Capability.clr_eos);
for (int i = 0; i < lines.size(); i++) {
terminal.puts(Capability.cursor_address, rows - lines.size() + i, 0);
terminal.writer().write(lines.get(i).columnSubSequence(0, columns).toAnsi(terminal));
}
terminal.puts(Capability.change_scroll_region, 0, rows - 1 - lines.size());
terminal.puts(Capability.restore_cursor);
terminal.flush();
oldLines = new ArrayList<>(lines);
force = false;
}
}

0 comments on commit 27de765

Please sign in to comment.