wmtools/base.nim

130 lines
3 KiB
Nim
Raw Normal View History

import std/[os,osproc,strutils]
import std/json
import std/rdstdin
2022-02-06 21:10:06 +01:00
import marshal
type
2022-04-28 21:58:40 +02:00
Info* = object
2022-04-28 13:50:07 +02:00
title*: string
2022-02-06 21:10:06 +01:00
full_text*: string
html_text*: string
2022-02-06 21:10:06 +01:00
short_text*: string
border*: string
2022-04-28 21:58:40 +02:00
color*: string
selected_color*: string
2022-02-12 16:19:43 +01:00
background*: string
2022-04-28 21:58:40 +02:00
selected_background*: string
2022-02-06 21:10:06 +01:00
type
i3BarInput* = object
button*: int
2022-02-12 16:19:43 +01:00
x*: int
y*: int
2022-02-06 21:10:06 +01:00
const background* = "#000000"
const backgroundalt* = "#bb222222"
const backgroundalt2* = "#bb333333"
const foreground* = "#dfdfdf"
const foregroundalt* = "#777"
const foregroundalt2* = "#ccc"
2022-02-12 16:19:43 +01:00
const black* = "#000000"
2022-02-27 15:57:40 +01:00
const white* = "#FFFFFF"
2022-02-06 21:10:06 +01:00
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"
2022-04-28 13:50:07 +02:00
var loop* = true
var stoploop* = false
var dmenu = false
var rofi = false
2022-02-06 21:10:06 +01:00
2022-04-28 21:58:40 +02:00
proc newInfo*(): Info =
return Info(
2022-04-28 13:50:07 +02:00
title: "Info : ",
2022-02-27 15:57:40 +01:00
full_text: "",
short_text: "",
color: foreground,
2022-04-28 21:58:40 +02:00
selected_color: background,
2022-02-27 15:57:40 +01:00
border: white,
2022-04-28 21:58:40 +02:00
selected_background: white,
2022-02-27 15:57:40 +01:00
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()
2022-04-28 13:34:09 +02:00
2022-02-12 14:20:21 +01:00
proc clearInput*(count: int = 1) =
2022-02-12 16:19:43 +01:00
for x in countup(1, count):
2022-02-12 14:20:21 +01:00
discard readLineFromStdin("")
2022-02-06 21:10:06 +01:00
proc getArguments*(): seq[string] =
let args = commandLineParams()
return args
2022-04-29 10:59:28 +02:00
proc runDMenu*(data: Info, opts: varargs[string], rofi: bool = false): string =
2022-04-28 21:58:40 +02:00
discard execCmd("i3-msg mode \"default\"")
var cmd = "echo -e \"" & $data.full_text & "\n"
for opt in opts:
cmd = cmd & opt & "\n"
2022-04-28 21:58:40 +02:00
cmd.removeSuffix("\n")
2022-04-29 10:59:28 +02:00
if not rofi:
cmd = cmd & "\" | dmenu"
else:
cmd = cmd & "\" | rofi -dmenu"
2022-04-28 21:58:40 +02:00
cmd = cmd & " -l " & $(len(opts) + 1)
2022-04-28 13:50:07 +02:00
cmd = cmd & " -p \"" & $data.title & "\""
2022-04-28 21:58:40 +02:00
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
2022-04-28 21:58:40 +02:00
proc outputJSON*(data: Info, args: varargs[string]): string {.discardable.} =
var output = ""
if dmenu:
output = runDMenu(data, args)
elif rofi:
2022-04-29 10:59:28 +02:00
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
2022-04-28 13:50:07 +02:00
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
2022-04-28 13:50:07 +02:00