From bd5b314cf1e13dd49147a2e75d1777d3c5fa5b1c Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 23 Nov 2023 12:14:24 +0000 Subject: [PATCH] screenshurrt working --- src/dispatcher.nim | 3 + src/model/config.nim | 6 ++ src/model/screenshot.nim | 72 ++++++++++++++++++ src/model/tool.nim | 3 +- src/parser.nim | 24 ++++++ .../{screenshurrt/src => }/screenshurrt.nim | 74 +++++++++---------- src/util/screenshurrt/screenshurrt.nimble | 13 ---- 7 files changed, 144 insertions(+), 51 deletions(-) create mode 100644 src/model/screenshot.nim rename src/util/{screenshurrt/src => }/screenshurrt.nim (55%) delete mode 100644 src/util/screenshurrt/screenshurrt.nimble diff --git a/src/dispatcher.nim b/src/dispatcher.nim index e7fc768..ed59021 100644 --- a/src/dispatcher.nim +++ b/src/dispatcher.nim @@ -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" diff --git a/src/model/config.nim b/src/model/config.nim index eff2278..9dd4174 100644 --- a/src/model/config.nim +++ b/src/model/config.nim @@ -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() diff --git a/src/model/screenshot.nim b/src/model/screenshot.nim new file mode 100644 index 0000000..006deac --- /dev/null +++ b/src/model/screenshot.nim @@ -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" + diff --git a/src/model/tool.nim b/src/model/tool.nim index 661b292..9b1a794 100644 --- a/src/model/tool.nim +++ b/src/model/tool.nim @@ -12,4 +12,5 @@ type Remminurr, Passwurrd, PasswurrdGeneraturr, - Temperaturr + Temperaturr, + Screenshurrt diff --git a/src/parser.nim b/src/parser.nim index 8382288..cf970f9 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -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 diff --git a/src/util/screenshurrt/src/screenshurrt.nim b/src/util/screenshurrt.nim similarity index 55% rename from src/util/screenshurrt/src/screenshurrt.nim rename to src/util/screenshurrt.nim index bc118a1..8408405 100644 --- a/src/util/screenshurrt/src/screenshurrt.nim +++ b/src/util/screenshurrt.nim @@ -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() diff --git a/src/util/screenshurrt/screenshurrt.nimble b/src/util/screenshurrt/screenshurrt.nimble deleted file mode 100644 index 5f18bf3..0000000 --- a/src/util/screenshurrt/screenshurrt.nimble +++ /dev/null @@ -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"