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

View file

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

View file

@ -5,6 +5,7 @@ import argparse
import common import common
import model/pwgen import model/pwgen
import model/volume import model/volume
import model/screenshot
proc parseArgs*() = proc parseArgs*() =
let params = commandLineParams() let params = commandLineParams()
@ -37,6 +38,8 @@ proc parseArgs*() =
myConfig.run = PasswurrdGeneraturr myConfig.run = PasswurrdGeneraturr
of "temperaturr", "temperature", "temp": of "temperaturr", "temperature", "temp":
myConfig.run = Temperaturr myConfig.run = Temperaturr
of "screenshurrt", "screenshot", "screeny":
myConfig.run = Screenshurrt
else: else:
echo p.help echo p.help
quit(1) quit(1)
@ -100,3 +103,24 @@ proc parsePWGenArgs*(): PWGen =
stderr.writeLine getCurrentExceptionMsg() stderr.writeLine getCurrentExceptionMsg()
quit(1) quit(1)
return gen 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 times
import std/[times,os,osproc,strutils,sequtils] 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 DATE_FORMAT = "yyyyMMdd-hhmmss"
const FILENAME = "Screenshot-%d.png" const FILENAME = "Screenshot-%d.png"
const TEMP_DIR = "/tmp/" 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) 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) = proc saveToClipboard(filename: string) =
let cmd = "cat " & filename & " | " & CLIP_CMD let cmd = "cat " & filename & " | " & CLIPBOARD_CMD
let status = execCmd(cmd) let status = execCmd(cmd)
if status == 0 and fileExists(filename): if status == 0 and fileExists(filename):
removeFile(filename) removeFile(filename)
@ -54,42 +52,44 @@ proc showScreenshotSaveSel(filename: string) =
openFile(filename) openFile(filename)
return return
proc showScreenshotTypeSel() = proc showScreenshotSizeSel(): ScreenshotSize =
let info = newInfo("Screenshurrt type") let info = newInfo("Screenshurrt type")
let args = concat(TYPES,@["---","exit"]) let args = concat(ScreenshotSizes(),@["---","exit"])
let choice = outputData(info,args) let choice = outputData(info,args)
if choice in TYPES: if choice.isScreenshotSize():
screenshot_type = choice return choice.toScreenshotSize()
elif choice == "---": elif choice == "---":
showScreenshotTypeSel() return showScreenshotSizeSel()
elif choice == "exit": elif choice == "exit":
return quit(0)
return else:
quit(0)
proc takeScreenshot() = proc takeScreenshot(ss: Screenshot) =
let filename = TEMP_DIR & FILENAME.replace("%d",DATE_STR) let filename = TEMP_DIR & FILENAME.replace("%d",DATE_STR)
var cmd = RUN_CMD.replace("%f",filename) var cmd = ss.tool.command
case screenshot_type: case ss.size:
of "window": of Window:
cmd = cmd.replace("%s",ACTIVE_WINDOW_CMD) cmd = ss.tool.activeWindowCommand()
of "region": of Region:
cmd = cmd.replace("%s",REGION_CMD) cmd = ss.tool.regionCommand()
else: #fullscreen else: #fullscreen
cmd = cmd.replace("%s","") 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) sleep(1*500)
cmd = cmd.replace("%f",filename)
echo "Running command:\n" & cmd
let status = execCmd(cmd) let status = execCmd(cmd)
if status == 0: if status == 0:
showScreenshotSaveSel(filename) showScreenshotSaveSel(filename)
return return
proc go*() =
var ss = parseScreenshotArgs()
if ss.size == None:
ss.size = showScreenshotSizeSel()
if ss.size != None:
ss.takeScreenshot()
if isMainModule: if isMainModule:
for arg in args: go()
if arg in TYPES:
screenshot_type = arg
break
if screenshot_type == "":
showScreenshotTypeSel()
if screenshot_type != "":
takeScreenshot()

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"