loads of changes - will now give options in dmenu entries to change configs etc, volume, brights....

This commit is contained in:
Paul Wilde 2022-04-28 15:52:45 +01:00
parent 31f2b20d03
commit 89b73b613c
17 changed files with 318 additions and 142 deletions

View file

@ -1,4 +1,4 @@
import std/[os,osproc]
import std/[os,osproc,strutils]
import std/json
import std/rdstdin
import marshal
@ -7,6 +7,7 @@ type
i3BarData* = object
title*: string
full_text*: string
html_text*: string
short_text*: string
color*: string
border*: string
@ -41,7 +42,8 @@ const secondary* = red
const alert* = "#bd2c40"
var loop* = true
var stoploop* = false
var dmenu* = false
var dmenu = false
var rofi = false
proc newi3BarData*(): i3BarData =
return i3BarData(
@ -74,20 +76,43 @@ proc getArguments*(): seq[string] =
let args = commandLineParams()
return args
proc outputJSON*(data: i3barData) =
echo $$data
proc runDMenu*(data: i3BarData) =
var cmd = "echo " & $data.full_text & " | dmenu "
proc runDMenu*(data: i3BarData, opts: varargs[string]): string =
var cmd = "echo -e \"" & $data.full_text & "\n"
for opt in opts:
cmd = cmd & opt & "\n"
cmd = cmd & "\" | dmenu"
#cmd = cmd & " -l 2 "
cmd = cmd & " -p \"" & $data.title & "\""
cmd = cmd & " -nb \"" & $data.background & "\""
cmd = cmd & " -nf \"" & $data.color & "\""
cmd = cmd & " -sb \"" & $data.border & "\""
cmd = cmd & " -sf \"" & $data.color & "\""
#echo "Dmenu :", cmd
let output = execCmdEx(cmd)
let option:string = strip(output[0])
discard execCmd("i3-msg mode \"default\"")
return option
proc runRofi*(data: i3BarData) =
var cmd = "rofi"
cmd = cmd & " -e \"" & $data.full_text & "\""
#echo "Rofi : ", cmd
discard execCmd(cmd)
discard execCmd("i3-msg mode \"default\"")
proc outputJSON*(data: i3barData, args: seq[string]): string {.discardable.} =
var output = ""
if dmenu:
output = runDMenu(data, args)
elif rofi:
runRofi(data)
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":
@ -95,5 +120,8 @@ for arg in args:
if arg == "dmenu":
stoploop = true
dmenu = true
if arg == "rofi":
stoploop = true
rofi = true

View file

@ -24,7 +24,7 @@ proc get_charge(): int =
charge = parseInt(chg)
return charge
proc get_design(charge: int, state: bool): (string, string, string, string) =
proc get_design(charge: int, state: bool): (string, string, string, string, string) =
var icon = ""
var icon_colour = lightgreen
var col = foreground
@ -60,13 +60,16 @@ proc get_design(charge: int, state: bool): (string, string, string, string) =
else:
icon = "x "
let main_text = icon & " " & $charge & "%"
let text = "<span foreground=\"" & icon_colour & "\">" & icon & "</span>" & $charge & "%"
return (text, col, bg, border)
return (text,main_text, col, bg, border)
proc get_output(charge: int, state: bool): i3barData =
let (text,col,bg_col,bord_col) = get_design(charge, state)
let (text,main_text,col,bg_col,bord_col) = get_design(charge, state)
let data = i3barData(
full_text: text,
title: "Battery : ",
full_text: main_text,
html_text: text,
color: col,
border: bord_col,
background: bg_col
@ -85,6 +88,8 @@ proc get_battery_info() =
outputJSON(data)
last_charge = charge
last_state = state
if stoploop:
break
sleep(1000)
proc main() =

View file

@ -3,7 +3,6 @@ import strutils
import std/osproc
import std/math
import i3bar_base
import std/threadpool
proc getLimit(): int
@ -31,7 +30,7 @@ proc getDesign(pcnt: float): string =
let text = icon & " " & $percent & "%"
return text
proc get_brightness(run_once: bool = false) =
proc get_brightness*(run_once: bool = false) =
var last_pcnt: float = 0
while true:
let current = parseInt(strip(readFile("/sys/class/backlight/intel_backlight/actual_brightness")))
@ -39,32 +38,28 @@ proc get_brightness(run_once: bool = false) =
if pcnt != last_pcnt:
let text = getDesign(pcnt)
var data = newi3BarData()
data.title = "Brightness : "
data.full_text = text
data.border = yellow
outputJSON(data)
let args = @["up", "down"]
let option = outputJSON(data,args)
case option:
of "up":
discard execCmd("xbacklight -inc 5")
get_brightness(true)
of "down":
discard execCmd("xbacklight -dec 5")
get_brightness(true)
if run_once:
break
if stoploop:
break
last_pcnt = pcnt
sleep(1000)
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 1,4:
let state = execCmd("xbacklight -inc 5")
get_brightness(true)
of 3,5:
let state = execCmd("xbacklight -dec 5")
get_brightness(true)
else:
let no = false
clearInput(2)
proc main() =
spawn get_brightness()
spawn await_click_info()
sync()
get_brightness()
main()
if isMainModule:
main()

View file

@ -0,0 +1,27 @@
import i3bar_base
import i3bar_brightness
import std/threadpool
import std/osproc
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 1,4:
let state = execCmd("xbacklight -inc 5")
get_brightness(true)
of 3,5:
let state = execCmd("xbacklight -dec 5")
get_brightness(true)
else:
let no = false
clearInput(2)
proc main() =
spawn get_brightness()
spawn await_click_info()
sync()
if isMainModule:
main()

View file

@ -2,56 +2,65 @@ import std/os
import std/times
import std/osproc
import std/re
import std/threadpool
import i3bar_base
let args = getArguments()
proc getObject(date: string): i3barData =
let data = i3barData(
full_text: date,
color: foreground,
border: blue
)
var data = newi3barData()
data.title = "Date : "
data.full_text = date
data.border = blue
return data
#proc openCalendar(datestr: string) =
proc openCalendar(input: i3barInput) =
proc newCalendar(): string =
var c = """yad --calendar \
--undecorated --fixed --close-on-unfocus --no-buttons \
--width="222" --height="193" \
--posx="%pos_x" --posy="%pos_y" \
--title="yad-calendar" --borders 0 > /dev/null
"""
return c
proc openCalendar*(input: i3barInput) =
var c = newCalendar()
c = replace(c,re"%pos_x", $(input.x - 111))
c = replace(c,re"%pos_y", $input.y)
discard execCmd(c)
proc getDate() =
proc dmenuCalendar() =
var c = newCalendar()
c = replace(c,re"%pos_x", "100")
c = replace(c,re"%pos_y", "0")
discard execCmd(c)
proc getDate*() =
var last_date = ""
while true:
let now = now()
let d = now.format("yyyy-MM-dd")
if d != last_date:
let data = getObject(d)
outputJSON(data)
let args = @["open calendar"]
case outputJSON(data, args):
of "open calendar":
dmenuCalendar()
last_date = d
if stoploop:
break
sleep(5000)
proc await_click_info() =
while true:
let input = parseInput()
if input.button == 1:
openCalendar(input)
proc main() =
spawn getDate()
spawn await_click_info()
sync()
getDate()
main()
if isMainModule:
main()
#DATE="$(date +"%a %d %H:%M")"
#if [ "$(xdotool getwindowfocus getwindowname)" = "yad-calendar" ]; then

18
i3bar_date_threads.nim Normal file
View file

@ -0,0 +1,18 @@
import std/threadpool
import i3bar_date
import i3bar_base
proc await_click_info() =
while true:
let input = parseInput()
if input.button == 1:
openCalendar(input)
proc main() =
spawn getDate()
spawn await_click_info()
sync()
if isMainModule:
main()

View file

@ -74,7 +74,7 @@ proc getObject(time: string): i3barData =
var data = newi3barData()
data.title = "Fuzzy Time :"
data.full_text = time
#data.color = foreground
data.color = foreground
data.border = lightblue
return data
@ -85,12 +85,12 @@ proc main() =
let time = get_fuzzytime()
if time != last_time:
let data = getObject(time)
if dmenu:
runDMenu(data)
break
outputJSON(data)
if stoploop:
break
last_time = time
sleep(2000)
main()
if isMainModule:
main()

View file

@ -1,7 +1,6 @@
import i3bar_base
import std/os
import std/osproc
import std/threadpool
import strutils
const nics: seq[string] = @["wlan0", "enp3s0","wlp2s0","enp0s20f0u3"]
@ -25,29 +24,31 @@ proc get_net(nic: string): (string, string) =
proc getObject(conn: string, nic: string): i3barData =
var data = newi3barData()
data.title = "IP :"
data.full_text = conn
data.border = purple
return data
proc get_net_info(nic: string) =
proc get_net_info*(nic: string) =
var last_ip = ""
var last_state = ""
while true:
let (ip, state) = get_net(nic)
if ip != last_ip or state != last_state:
let data = getObject(state & " " & ip, nic)
outputJSON(data)
let args = @["nmtui-connect"]
let option = outputJSON(data, args)
case option:
of "nmtui-connect":
discard execCmd("alacritty -e nmtui-connect")
last_ip = ip
last_state = state
if stoploop:
break
sleep(1000)
proc await_click_info() =
while true:
let input = parseInput()
if input.button == 1:
discard execCmd("alacritty -e nmtui-connect")
proc get_nic(): string =
proc get_nic*(): string =
for nic in nics:
if dirExists("/sys/class/net/" & nic):
return nic
@ -56,9 +57,7 @@ proc get_nic(): string =
proc main() =
let mynic = get_nic()
if dirExists("/sys/class/net/" & mynic):
spawn get_net_info(mynic)
spawn await_click_info()
sync()
get_net_info(mynic)
else:
echo "No NIC"

25
i3bar_nic_threads.nim Normal file
View file

@ -0,0 +1,25 @@
import i3bar_base
import i3bar_nic
import std/os
import std/osproc
import std/threadpool
import strutils
proc await_click_info() =
while true:
let input = parseInput()
if input.button == 1:
discard execCmd("alacritty -e nmtui-connect")
proc main() =
let mynic = get_nic()
if dirExists("/sys/class/net/" & mynic):
spawn get_net_info(mynic)
spawn await_click_info()
sync()
else:
echo "No NIC"
if isMainModule:
main()

View file

@ -68,14 +68,12 @@ proc main() =
let ping = get_ping()
if ping != last_ping:
let data = getObject(ping)
if dmenu:
runDMenu(data)
else:
outputJSON(data)
last_ping = ping
outputJSON(data)
last_ping = ping
loop = not stoploop
if loop:
sleep(time_secs * 1000)
main()
if isMainModule:
main()

View file

@ -41,11 +41,13 @@ proc getObject(temp: int): i3barData =
colour = green
icon = ""
let text = "<span foreground='" & colour & "'>" & icon & "</span> " & $temp & "°C"
let data = i3barData(
full_text: text,
color: foreground,
border: colour
)
let main_text = icon & " " & $temp & "°C"
var data = newi3barData()
data.title = "Temp : "
data.full_text = main_text
data.html_text = text
data.color = foreground
data.border = colour
return data
proc main() =
@ -57,6 +59,8 @@ proc main() =
let data = getObject(temp)
outputJSON(data)
last_temp = temp
if stoploop:
break
sleep(10000)
main()

View file

@ -3,13 +3,11 @@ import i3bar_base
import std/re
import std/httpclient
import std/os
import std/json
import std/times
import std/threadpool
import std/osproc
const url = "https://www.tidetimes.org.uk/%LOC-tide-times"
const loc = "exmouth-dock"
const url* = "https://www.tidetimes.org.uk/%LOC-tide-times"
const loc* = "exmouth-dock"
const icon: string = "🌊"
type
@ -86,9 +84,7 @@ proc getDesign(tides: seq[Tide]): i3barData =
return data
proc getTides() {.gcsafe.}=
proc getTides*() {.gcsafe.}=
var mytides = TideList()
var last_data = ""
while true:
@ -99,20 +95,11 @@ proc getTides() {.gcsafe.}=
if $data != last_data:
outputJSON(data)
last_data = $data
if stoploop:
break
sleep(10000)
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 1:
let state = execCmd("xdg-open " & replace(url,re"\%LOC",loc))
else:
let no = false
proc main() =
spawn getTides()
spawn await_click_info()
sync()
getTides()
main()

21
i3bar_tides_threads.nim Normal file
View file

@ -0,0 +1,21 @@
import i3bar_tides
import i3bar_base
import std/re
import std/threadpool
import std/osproc
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 1:
let state = execCmd("xdg-open " & replace(url,re"\%LOC",loc))
else:
let no = false
proc main() =
spawn getTides()
spawn await_click_info()
sync()
main()

View file

@ -3,7 +3,6 @@ import strutils
import std/osproc
import std/math
import i3bar_base
import std/threadpool
proc get_current_volume(): string {.gcsafe.}
@ -15,11 +14,11 @@ proc check_volume(volume: string): string =
vol = check_volume(vol)
return vol
proc getDesign(volume: string): string =
proc getDesign(volume: string): (string,string) =
let vol = check_volume(volume)
var icon = ""
if vol == "muted":
return icon & "muted"
return (icon & "muted", "")
let pcnt = parseInt(strip(vol))
case pcnt:
of 85..100:
@ -32,8 +31,9 @@ proc getDesign(volume: string): string =
icon = ""
else:
icon = ""
let main_text = icon & $pcnt & "%"
let text = "<span size=\"x-large\">" & icon & "</span>" & $pcnt & "%"
return text
return (text, main_text)
proc get_current_volume(): string =
let mute = execCmdEx("pamixer --get-mute")
@ -42,49 +42,47 @@ proc get_current_volume(): string =
let vol = execCmdEx("pamixer --get-volume")
return vol.output
proc get_volume(run_once: bool = false) =
proc get_volume*(run_once: bool = false) =
var last_vol: string = ""
while true:
let vol = get_current_volume()
if vol != last_vol or true:
let text = getDesign(vol)
let (text, main_text) = getDesign(vol)
let data = i3barData(
full_text: text,
title: "Volume : ",
html_text: text,
full_text: main_text,
color: foreground,
border: green,
background: black
)
outputJSON(data)
let args = @["up", "down", "mute", "ncpamixer", "pavucontrol"]
let option = outputJSON(data,args)
case option:
of "up":
discard execCmd("pamixer -i 5")
get_volume()
of "down":
discard execCmd("pamixer -d 5")
get_volume()
of "mute":
discard execCmd("pamixer -t")
get_volume()
of "ncpamixer":
discard execCmd("ncpamixer")
of "pavucontrol":
discard execCmd("pavucontrol")
if run_once:
break
last_vol = vol
sleep(1000)
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 4:
discard execCmd("pamixer -i 5")
get_volume(true)
clearInput(2)
of 5:
discard execCmd("pamixer -d 5")
get_volume(true)
clearInput(2)
of 1:
discard execCmd("pamixer -t")
get_volume(true)
of 3:
discard execCmd("alacritty -e ncpamixer")
get_volume(true)
else:
let no = false
if stoploop:
break
proc main() =
spawn get_volume()
spawn await_click_info()
sync()
get_volume()
main()
if isMainModule:
main()

34
i3bar_volume_threads.nim Normal file
View file

@ -0,0 +1,34 @@
import i3bar_base
import i3bar_volume
import std/threadpool
import std/osproc
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 4:
discard execCmd("pamixer -i 5")
get_volume(true)
clearInput(2)
of 5:
discard execCmd("pamixer -d 5")
get_volume(true)
clearInput(2)
of 1:
discard execCmd("pamixer -t")
get_volume(true)
of 3:
discard execCmd("alacritty -e ncpamixer")
get_volume(true)
else:
let no = false
proc main() =
spawn get_volume()
spawn await_click_info()
sync()
if isMainModule:
main()

View file

@ -1,10 +1,9 @@
import i3bar_base
import std/os
import std/osproc
import std/threadpool
import strutils
const wlan_nic: string ="wlan0"
const wlan_nic*: string ="wlan0"
# /sys/class/net/wlp2s0/operstate up or down if connected
@ -29,18 +28,26 @@ proc get_wifi(): (string, string) =
proc getObject(conn: string): i3barData =
var data = newi3barData()
data.title = "WiFi : "
data.full_text = conn
data.border = purple
return data
proc get_wifi_info() =
proc get_wifi_info*() =
var last_qual = ""
while true:
let (essid, quality) = get_wifi()
if quality != last_qual:
let data = getObject(quality & " " & essid)
outputJSON(data)
let args = @["nmtui-connect"]
let output = outputJSON(data, args)
echo output
case output:
of "nmtui-connect":
discard execCmd("alacritty -e nmtui-connect")
last_qual = quality
if stoploop:
break
sleep(1000)
proc await_click_info() =
@ -51,10 +58,9 @@ proc await_click_info() =
proc main() =
if dirExists("/sys/class/net/" & wlan_nic):
spawn get_wifi_info()
spawn await_click_info()
sync()
get_wifi_info()
else:
echo "No WLAN"
main()
if isMainModule:
main()

22
i3bar_wlan_threads.nim Executable file
View file

@ -0,0 +1,22 @@
import i3bar_base
import i3bar_wlan
import std/[os,osproc]
import std/threadpool
proc await_click_info() =
while true:
let input = parseInput()
if input.button == 1:
discard execCmd("alacritty -e nmtui-connect")
proc main() =
if dirExists("/sys/class/net/" & wlan_nic):
spawn get_wifi_info()
spawn await_click_info()
sync()
else:
echo "No WLAN"
if isMainModule:
main()