Skip to content

Commit

Permalink
"help list-commands and "history" working
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Oct 25, 2019
1 parent 55080bd commit 2b01219
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 8 deletions.
5 changes: 4 additions & 1 deletion Blink/Commands/clear.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@

#include <stdio.h>
#include "ios_system/ios_system.h"
#include "ios_error.h"
#include "MCPSession.h"

int clear_main(int argc, char *argv[]) {
return [[(__bridge MCPSession *)thread_context repl] clear_main:argc argv:argv];
// return [[(__bridge MCPSession *)thread_context repl] clear_main:argc argv:argv];
puts("\033c\033[H\033[2J\033[0m");
return 0;
}
16 changes: 16 additions & 0 deletions Blink/Commands/help.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

#include "ios_system/ios_system.h"
#include "ios_error.h"
#include <Blink-Swift.h>
#include "MCPSession.h"

NSString *__shortVersionString()
{
Expand All @@ -48,9 +50,23 @@
appDisplayName, majorVersion, minorVersion, compileDate];
}

void __print_commands() {
MCPSession *session = (__bridge MCPSession *)thread_context;
if (!session) {
return;
}

NSString *formattedCommands = [CompleteClass formattedCommandsWithWidth: session.device.cols];
puts(formattedCommands.UTF8String);
}


int help_main(int argc, char *argv[]) {

if (argc == 2 && [@"list-commands" isEqual: @(argv[1])]) {
__print_commands();
return 0;
}
NSString *help = [@[
@"",
__shortVersionString(),
Expand Down
41 changes: 40 additions & 1 deletion Blink/Commands/history.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,48 @@
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include "BlinkPaths.h"
#include "ios_system/ios_system.h"
#include "ios_error.h"
#include "MCPSession.h"
#include <Blink-Swift.h>

int history_main(int argc, char *argv[]) {
return [[(__bridge MCPSession *)thread_context repl] history_main:argc argv:argv];
NSString *args = @"";
if (argc == 2) {
args = [NSString stringWithUTF8String:argv[1]];
}
NSInteger number = [args integerValue];
if (number != 0) {
NSString *history = [NSString stringWithContentsOfFile:[BlinkPaths historyFile]
encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [history componentsSeparatedByString:@"\n"];
if (!lines) {
return 1;
}
lines = [lines filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self != ''"]];

NSInteger len = lines.count;
NSInteger start = 0;
if (number > 0) {
len = MIN(len, number);
} else {
start = MAX(len + number , 0);
}

for (NSInteger i = start; i < len; i++) {
puts([NSString stringWithFormat:@"% 4li %@", i + 1, lines[i]].UTF8String);
}
} else if ([args isEqualToString:@"-c"]) {
[HistoryObj clear];
} else {
NSString *usage = [@[
@"history usage:",
@"history <number> - Show history (can be negative)",
@"history -c - Clear history",
@""
] componentsJoinedByString:@"\n"];
puts(usage.UTF8String);
}
return 1;
}
22 changes: 22 additions & 0 deletions Blink/Complete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,25 @@ struct Complete {
}

}

@objc class CompleteClass: NSObject {
@objc static func formattedCommands(width: Int) -> String {
let commands = Complete._allCommands().sorted()
let count = commands.count
guard let maxStr = commands.max(by: {$0.count < $1.count}) else {
return ""
}

let columns = max(width / (maxStr.count + 2), 1)

var lines:[String] = []
for i in stride(from: 0, to: count, by: columns) {
var line = ""
for cmd in commands[i..<min(i + columns, count)] {
line += cmd.padding(toLength: maxStr.count + 2, withPad: " ", startingAt: 0)
}
lines.append(line)
}
return lines.joined(separator: "\n")
}
}
10 changes: 10 additions & 0 deletions Blink/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ struct History {
_lines = lines
}

static func clear() {
_historyQueue.async {
self._saveLines([])
}
}

private static func _getLines() -> [String] {
if let lines = _lines {
// Keep history for more time
Expand Down Expand Up @@ -201,4 +207,8 @@ struct History {
@objc static func appendIfNeeded(command: String) {
History.appendIfNeeded(command: command + "") // mosh overwrites internals of the string!
}

@objc static func clear() {
History.clear()
}
}
2 changes: 1 addition & 1 deletion Resources/blinkCommandsDictionary.plist
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<array>
<string>MAIN</string>
<string>history_main</string>
<string></string>
<string>c</string>
<string>no</string>
</array>
<key>geo</key>
Expand Down
3 changes: 1 addition & 2 deletions Sessions/MCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#import <Foundation/Foundation.h>

#import "Repl.h"

#import "Session.h"
#import "SSHClient.h"

Expand All @@ -40,7 +40,6 @@
@interface MCPSession : Session

@property (strong) MCPParams *sessionParams;
@property (strong, readonly) Repl *repl;

- (void)registerSSHClient:(SSHClient *)sshClient;
- (void)unregisterSSHClient:(SSHClient *)sshClient;
Expand Down
3 changes: 0 additions & 3 deletions Sessions/MCPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ - (void)_runSSHWithArgs:(NSString *)args

- (void)sigwinch
{
[_repl sigwinch];
[_childSession sigwinch];
for (WeakSSHClient *client in _sshClients) {
[client.value sigwinch];
Expand All @@ -279,8 +278,6 @@ - (void)sigwinch

- (void)kill
{
[_repl forceExit];

if (_sshClients.count > 0) {
for (WeakSSHClient *client in _sshClients) {
[client.value kill];
Expand Down

0 comments on commit 2b01219

Please sign in to comment.