screenshurrt working

This commit is contained in:
Paul Wilde 2023-11-23 12:14:24 +00:00
parent 6f5f255b66
commit bd5b314cf1
7 changed files with 144 additions and 51 deletions

View file

@ -11,6 +11,7 @@ import util/remminurr
import util/passwurrd
import util/pw_generaturr
import util/temperaturr
import util/screenshurrt
proc dispatch*(cfg: Config) =
case cfg.run
@ -36,5 +37,7 @@ proc dispatch*(cfg: Config) =
pw_generaturr.go()
of Temperaturr:
temperaturr.go()
of Screenshurrt:
screenshurrt.go()
else:
echo "No valid run command given"

View file

@ -2,6 +2,7 @@ import os
import parsetoml
import tool
import screenshot
type
Config* = ref object
@ -9,6 +10,7 @@ type
run*: Tool
max_lines*: int
prepend*: bool
screenshot_tool*: ScreenshotTool
let config_dir* = getHomeDir() & ".config/wm_tools/"
let config_file* = config_dir & "config.toml"
@ -16,6 +18,7 @@ let config_file* = config_dir & "config.toml"
proc `$`(c: Config): string =
var str = "exec = \"" & c.exec & "\"\n"
str &= "prepend = " & $c.prepend & "\n"
str &= "screenshot_tool = \"" & $c.screenshot_tool & "\"\n"
str &= "max_lines = " & $c.max_lines
str &= "\n"
return str
@ -24,6 +27,7 @@ proc newConfig*(): Config =
var cfg = Config()
cfg.exec = "rofi -dmenu"
cfg.prepend = true
cfg.screenshot_tool = Maim
cfg.max_lines = 20
discard existsOrCreateDir(config_dir)
@ -37,6 +41,8 @@ proc newConfig*(): Config =
cfg.exec = toml["exec"].getStr
if toml.hasKey("max_lines"):
cfg.max_lines = toml["max_lines"].getInt
if toml.hasKey("screenshot_tool"):
cfg.screenshot_tool = toml["screenshot_tool"].getStr.toScreenshotTool
except:
echo "Error with Config File:"
echo getCurrentExceptionMsg()

72
src/model/screenshot.nim Normal file
View file

@ -0,0 +1,72 @@
import sequtils
import strutils
type
Screenshot* = object
size*: ScreenshotSize
tool*: ScreenshotTool
ScreenshotSize* = enum
None = ""
Region = "region",
Full = "fullscreen",
Window = "window"
ScreenshotTool* = enum
None = ""
Maim = "maim"
proc newScreenshot*(): Screenshot =
var ss = Screenshot()
ss.tool = Maim
ss.size = None
return ss
proc toScreenshotSize*(str: string): ScreenshotSize =
case str
of "region": return Region
of "fullscreen": return Full
of "window": return Window
else: return None
proc isScreenshotSize*(str: string): bool =
return str.toScreenshotSize != None
proc ScreenshotSizes*(): seq[string] =
var sizes: seq[string] = @[]
for item in ScreenshotSize.toSeq:
if item != None:
sizes.add($item)
return sizes
proc toScreenshotTool*(str: string): ScreenshotTool =
case str
of "maim": return Maim
else: return None
proc isScreenshotTool*(str: string): bool =
return str.toScreenshotTool != None
proc command*(tool: ScreenshotTool): string =
case tool
of Maim: return "maim -u %s --format png %f"
else: return ""
proc activeWindowCommand*(tool: ScreenshotTool): string =
var cmd = tool.command()
# where %s is an extra flag or process, i.e. xdotool for getting active window
case tool
of Maim:
cmd = cmd.replace("%s","-i $(xdotool getactivewindow)")
else: return cmd
return cmd
proc regionCommand*(tool: ScreenshotTool): string =
var cmd = tool.command()
case tool
of Maim:
cmd = cmd.replace("%s","-s")
else: return cmd
return cmd
const CLIPBOARD_CMD* = "xclip -selection clipboard -t image/png"

View file

@ -12,4 +12,5 @@ type
Remminurr,
Passwurrd,
PasswurrdGeneraturr,
Temperaturr
Temperaturr,
Screenshurrt

View file

@ -5,6 +5,7 @@ import argparse
import common
import model/pwgen
import model/volume
import model/screenshot
proc parseArgs*() =
let params = commandLineParams()
@ -37,6 +38,8 @@ proc parseArgs*() =
myConfig.run = PasswurrdGeneraturr
of "temperaturr", "temperature", "temp":
myConfig.run = Temperaturr
of "screenshurrt", "screenshot", "screeny":
myConfig.run = Screenshurrt
else:
echo p.help
quit(1)
@ -100,3 +103,24 @@ proc parsePWGenArgs*(): PWGen =
stderr.writeLine getCurrentExceptionMsg()
quit(1)
return gen
proc parseScreenshotArgs*(): Screenshot =
var ss = newScreenshot()
ss.tool = myConfig.screenshot_tool
let params = commandLineParams()
var p = newParser:
help("Args for screenshurrt")
arg("screenshurrt",help="can only ever be 'screenshurrt' as you won't have gotten this far otherwise")
option("-s","--size",help="size/region i.e. region, fullscreen or window")
try:
var opts = p.parse(params)
if opts.size != "":
ss.size = opts.size.toScreenshotSize()
except ShortCircuit as err:
if err.flag == "argparse_help":
echo err.help
quit(1)
except UsageError:
stderr.writeLine getCurrentExceptionMsg()
quit(1)
return ss

View file

@ -1,23 +1,21 @@
import ../../globurrl
import std/[times,os,osproc,strutils,sequtils]
import times
import os
import osproc
import strutils
import sequtils
import ../common
import ../output
import ../parser
import ../model/screenshot
var screenshot_type = ""
const TYPES = @["region", "fullscreen", "window"]
const DATE_FORMAT = "yyyyMMdd-hhmmss"
const FILENAME = "Screenshot-%d.png"
const TEMP_DIR = "/tmp/"
const SCREENSHOT_CMD = "maim -u %s --format png %f"
var RUN_CMD = SCREENSHOT_CMD
let DATE_STR = now().format(DATE_FORMAT)
# where %s is an extra flag or process, i.e. xdotool for getting active window
const ACTIVE_WINDOW_CMD = "-i $(xdotool getactivewindow)"
const REGION_FLAG = "-s"
var REGION_CMD = REGION_FLAG
const CLIPBOARD_CMD = "xclip -selection clipboard -t image/png"
var CLIP_CMD = CLIPBOARD_CMD
proc saveToClipboard(filename: string) =
let cmd = "cat " & filename & " | " & CLIP_CMD
let cmd = "cat " & filename & " | " & CLIPBOARD_CMD
let status = execCmd(cmd)
if status == 0 and fileExists(filename):
removeFile(filename)
@ -54,42 +52,44 @@ proc showScreenshotSaveSel(filename: string) =
openFile(filename)
return
proc showScreenshotTypeSel() =
proc showScreenshotSizeSel(): ScreenshotSize =
let info = newInfo("Screenshurrt type")
let args = concat(TYPES,@["---","exit"])
let args = concat(ScreenshotSizes(),@["---","exit"])
let choice = outputData(info,args)
if choice in TYPES:
screenshot_type = choice
if choice.isScreenshotSize():
return choice.toScreenshotSize()
elif choice == "---":
showScreenshotTypeSel()
return showScreenshotSizeSel()
elif choice == "exit":
return
return
quit(0)
else:
quit(0)
proc takeScreenshot() =
proc takeScreenshot(ss: Screenshot) =
let filename = TEMP_DIR & FILENAME.replace("%d",DATE_STR)
var cmd = RUN_CMD.replace("%f",filename)
case screenshot_type:
of "window":
cmd = cmd.replace("%s",ACTIVE_WINDOW_CMD)
of "region":
cmd = cmd.replace("%s",REGION_CMD)
var cmd = ss.tool.command
case ss.size:
of Window:
cmd = ss.tool.activeWindowCommand()
of Region:
cmd = ss.tool.regionCommand()
else: #fullscreen
cmd = cmd.replace("%s","")
# sleep for a bit otherwise the screen shot grabs dmenu as well
# sleep for a bit otherwise the screen shot could grabs dmenu as well
sleep(1*500)
cmd = cmd.replace("%f",filename)
echo "Running command:\n" & cmd
let status = execCmd(cmd)
if status == 0:
showScreenshotSaveSel(filename)
return
proc go*() =
var ss = parseScreenshotArgs()
if ss.size == None:
ss.size = showScreenshotSizeSel()
if ss.size != None:
ss.takeScreenshot()
if isMainModule:
for arg in args:
if arg in TYPES:
screenshot_type = arg
break
if screenshot_type == "":
showScreenshotTypeSel()
if screenshot_type != "":
takeScreenshot()
go()

View file

@ -1,13 +0,0 @@
# Package
version = "0.1.0"
author = "Paul Wilde"
description = "A screenshot client, works on both X11 and Wayland"
license = "GPL-3.0-or-later"
srcDir = "src"
bin = @["screenshurrt"]
# Dependencies
requires "nim >= 1.6.6"