taro/taro-ctl.cr

111 lines
2.4 KiB
Crystal
Raw Normal View History

require "process"
require "io/memory"
require "io"
module Taro
enum WinType
LIST
READER
COMPOSE
end
class ChildWindow
@status : Channel(String)
@stdout_w : IO::FileDescriptor
@stdout_r : IO::FileDescriptor
@stdin_w : IO::FileDescriptor
@stdin_r : IO::FileDescriptor
def initialize(w : WinType = WinType::LIST)
@stdout_r, @stdout_w = IO.pipe
@stdin_r, @stdin_w = IO.pipe
@status = Channel(String).new
uxnrom : String = ""
case w
when WinType::LIST then uxnrom = "taro-ls"
when WinType::READER then uxnrom = "taro-reader"
when WinType::COMPOSE then uxnrom = "taro-compose"
end
uxnargs = ["-s", "1", "#{uxnrom}.rom"]
spawn do
Process.run(command: "uxnemu", args: uxnargs, input: @stdin_r, output: @stdout_w, error: Process::Redirect::Inherit)
@status.send("ls closed")
end
end
def lifetime
return @status
end
def write_msg(msgtype : UInt8, data : Slice)
msgsz = UInt16.new(data.size > 16384 ? 16384 : data.size)
msgtype.to_io(@stdin_w, IO::ByteFormat::BigEndian)
msgsz.to_io(@stdin_w, IO::ByteFormat::BigEndian)
if msgsz == 16384
@stdin_w.write(data[0..msgsz - 2])
10_u8.to_io(@stdin_w, IO::ByteFormat::BigEndian)
else
@stdin_w.write(data[0..msgsz - 1])
end
end
def read_msg()
slc = Slice.new(16384)
@stdout_r.read(slc)
return slc[0].to_u8, slc[1..slc.size - 1]
end
end
class MblazeProxy
def list_mail(box : String = "INBOX")
mailCmd = "
mbox=${MBOX_ROOT}/#{box}
mdirs ${mbox} | xargs minc > /dev/null
mlist ${mbox} | msort -dr | mseq -S | mscan"
io = IO::Memory.new
Process.run(mailCmd, shell: true, output: io)
return io.to_s
end
end
class TaroCtl
@mblaze : MblazeProxy
@lsWin : ChildWindow
@readWins : Array(ChildWindow)
@composeWins : Array(ChildWindow)
def initialize
@lsWin = ChildWindow.new
@readWins = Array(ChildWindow).new
@composeWins = Array(ChildWindow).new
@mblaze = MblazeProxy.new
list = @mblaze.list_mail
@lsWin.write_msg(2_u8, list.to_slice)
end
def mainWindow
@lsWin
end
end
end
taro = Taro::TaroCtl.new
loop do
select
when taro.mainWindow.lifetime.receive?
break
end
end