wmtools/base.nim
2022-04-29 09:59:28 +01:00

130 lines
3 KiB
Nim

import std/[os,osproc,strutils]
import std/json
import std/rdstdin
import marshal
type
Info* = object
title*: string
full_text*: string
html_text*: string
short_text*: string
border*: string
color*: string
selected_color*: string
background*: string
selected_background*: string
type
i3BarInput* = object
button*: int
x*: int
y*: int
const background* = "#000000"
const backgroundalt* = "#bb222222"
const backgroundalt2* = "#bb333333"
const foreground* = "#dfdfdf"
const foregroundalt* = "#777"
const foregroundalt2* = "#ccc"
const black* = "#000000"
const white* = "#FFFFFF"
const yellow* = "#ffb52a"
const red* = "#e60053"
const purple* = "#9f78e1"
const blue* = "#0a6cf5"
const lightblue* = "#7296EF"
const lighterblue* = "#B5DDF7"
const green* = "#4b9901"
const lightgreen* = "#00ff00"
const grey* = "#dfdfdf"
const darkgrey* = "#444"
const primary* = yellow
const secondary* = red
const alert* = "#bd2c40"
var loop* = true
var stoploop* = false
var dmenu = false
var rofi = false
proc newInfo*(): Info =
return Info(
title: "Info : ",
full_text: "",
short_text: "",
color: foreground,
selected_color: background,
border: white,
selected_background: white,
background: black
)
proc debugLog*(str: string) =
let f = open("/tmp/debug.txt",fmAppend)
defer: f.close()
f.writeLine(str)
proc parseInput*(): i3BarInput =
let input = readLineFromStdin("")
try:
let jsonNode = parseJson(input)
let i3input = to(jsonNode, i3BarInput)
return i3input
except:
return i3BarInput()
proc clearInput*(count: int = 1) =
for x in countup(1, count):
discard readLineFromStdin("")
proc getArguments*(): seq[string] =
let args = commandLineParams()
return args
proc runDMenu*(data: Info, opts: varargs[string], rofi: bool = false): string =
discard execCmd("i3-msg mode \"default\"")
var cmd = "echo -e \"" & $data.full_text & "\n"
for opt in opts:
cmd = cmd & opt & "\n"
cmd.removeSuffix("\n")
if not rofi:
cmd = cmd & "\" | dmenu"
else:
cmd = cmd & "\" | rofi -dmenu"
cmd = cmd & " -l " & $(len(opts) + 1)
cmd = cmd & " -p \"" & $data.title & "\""
cmd = cmd & " -nb \"" & $background & "\""
cmd = cmd & " -nf \"" & $foreground & "\""
cmd = cmd & " -sb \"" & $data.selected_background & "\""
cmd = cmd & " -sf \"" & $data.selected_color & "\""
cmd = cmd & " -fn Hermit-10"
#echo "Dmenu :", cmd
let output = execCmdEx(cmd)
let option:string = strip(output[0])
return option
proc outputJSON*(data: Info, args: varargs[string]): string {.discardable.} =
var output = ""
if dmenu:
output = runDMenu(data, args)
elif rofi:
output = runDmenu(data,args, rofi = true)
else:
var j_data = data
if j_data.html_text != "":
j_data.full_text = j_data.html_text
echo $$j_data
return output
let args = getArguments()
for arg in args:
if arg == "noloop":
stoploop = true
if arg == "dmenu":
stoploop = true
dmenu = true
if arg == "rofi":
stoploop = true
rofi = true