-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshout.lua
More file actions
49 lines (38 loc) · 1.19 KB
/
shout.lua
File metadata and controls
49 lines (38 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
VERSION = "0.0.2"
local micro = import("micro")
local config = import("micro/config")
local shell = import("micro/shell")
local buffer = import("micro/buffer")
function ShOut(bp)
local lineNum = bp.Cursor.Y
local line = bp.Buf:Line(lineNum)
if line:match("^%s*$") then
micro.InfoBar():Error("Line is empty")
return
end
micro.InfoBar():Message("Running: " .. line)
local output = shell.RunCommand(line) or ""
bp.Cursor:End()
local atEndOfBuffer = bp.Cursor.Loc.X == bp.Buf:End().X and bp.Cursor.Loc.Y == bp.Buf:End().Y
if atEndOfBuffer then
output = "\n" .. output
end
local insertLoc = buffer.Loc(0, lineNum + 1)
bp.Buf:Insert(insertLoc, output)
if not atEndOfBuffer then
local outputLines = {}
for line in output:gmatch("([^\n]*)\n?") do
table.insert(outputLines, line)
end
local lineOffset = #outputLines - 1
local lastLine = outputLines[#outputLines]
local finalX = #lastLine
local finalY = lineNum + 1 + lineOffset
bp.Cursor:GotoLoc(buffer.Loc(finalX, finalY))
end
end
function init()
config.MakeCommand("shout", ShOut, config.NoComplete)
config.TryBindKey("Ctrl-Alt-b", "lua:shout.ShOut", false)
config.AddRuntimeFile("shout", config.RTHelp, "help/shout.md")
end