Re: [dev] Announcing a couple small X11 utilities

2023-08-17 Thread Lars Lindqvist


tor 2023-08-17 10:19:01 skrev Max Schillinger:
> On Tue Jul 4, 2023 at 3:51 PM CEST, NRK wrote:
> > sx4
> > ===
> >
> > This one is a selection tool. It outputs the selection rectangle to
> > stdout which can then be used for other purposes, such as screenshoting
> > or screen-recording a specific area.
> 
> All these tools are great! Thanks for sharing.
> 
> I don't do much image processing but when I do, it's most often cropping 
> images. Of course, GIMP can do this. But it's huge. I usually use nomacs 
> instead. But now the nomacs package for Arch Linux is broken because of 
> dependencies. Now I use gThumb as a replacement but it's also big.
> 
> When I heard about sx4, I was wondering if it's somehow possible to 
> connect sxiv, sx4 and ImageMagick. I could open an image with sxiv (or 
> feh or something simple) without any zoom, then I could select a region 
> with sx4 and pass it to ImageMagick. For this I would have to correct 
> the coordinates by the offset of the canvas relative to the screen. This 
> seems to be the missing link.
> 
> Any ideas how to achieve this?

I think this can be done with (image|graphics)magick import, simply
`import cropped.png` and select the region you want.

I do a lot of cropping using some ugly hacks, attached diff is for
nsxiv master and uses ctrl-q to crop the active image to what is
currenly visible, regardless of zoom (a 100x100 window at 400% zoom
will give a 25x25 image). (Replace 'mogrify -crop' with some wrapper
script if you don't want to accidentally destroy images..)

> 
> Regards,
> Max
> 


diff --git a/commands.c b/commands.c
index 97cadf2..ff80201 100644
--- a/commands.c
+++ b/commands.c
@@ -253,6 +253,24 @@ bool cg_change_contrast(arg_t d)
return change_color_modifier(d, );
 }
 
+bool ci_crop(arg_t _)
+{
+   char geo[64];
+   char *argv[5];
+   int x, y, w, h;
+
+   x = MAX(0, -img.x / img.zoom);
+   y = MAX(0, -img.y / img.zoom);
+   w = MIN(img.w, win.w / img.zoom);
+   h = MIN(img.h, win.h / img.zoom);
+   if (x == 0 && y == 0 && w == img.w && h == img.h)
+   return false;
+   construct_argv(argv, ARRLEN(argv), "mogrify", "-crop", geo, 
files[fileidx].path, NULL);
+   waitpid(spawn(NULL, NULL, argv), NULL, 0);
+   img.dirty = true;
+   return true;
+}
+
 bool ci_navigate(arg_t n)
 {
if (prefix > 0)
diff --git a/commands.h b/commands.h
index 4e694f0..530c363 100644
--- a/commands.h
+++ b/commands.h
@@ -25,6 +25,7 @@ bool cg_unmark_all(arg_t);
 bool cg_zoom(arg_t);
 /* image mode */
 bool ci_alternate(arg_t);
+bool ci_crop(arg_t);
 bool ci_cursor_navigate(arg_t);
 bool ci_drag(arg_t);
 bool ci_fit_to_win(arg_t);
@@ -72,6 +73,7 @@ bool ct_select(arg_t);
 
 /* image mode */
 #define i_alternate { ci_alternate, MODE_IMAGE }
+#define i_crop { ci_crop, MODE_IMAGE }
 #define i_cursor_navigate { ci_cursor_navigate, MODE_IMAGE }
 #define i_drag { ci_drag, MODE_IMAGE }
 #define i_fit_to_win { ci_fit_to_win, MODE_IMAGE }
diff --git a/config.def.h b/config.def.h
index 7fbfb17..c530e8a 100644
--- a/config.def.h
+++ b/config.def.h
@@ -137,6 +137,7 @@ static const keymap_t keys[] = {
{ 0,XK_Right, t_move_sel,   DIR_RIGHT },
{ 0,XK_R, t_reload_all, None },
 
+   { ControlMask,  XK_q, i_crop,   None },
{ 0,XK_n, i_navigate,   +1 },
{ 0,XK_n, i_scroll_to_edge, DIR_LEFT | 
DIR_UP },
{ 0,XK_space, i_navigate,   +1 },



[dev] psgck - read only pwck/grpck that doesn't suck as much

2016-08-02 Thread Lars Lindqvist
I've just replaced the shadow package on my slackware systems with the
su, passwd etc programs from ubase, but was missing pwck and grpck. So
I threw something together, maybe not particularly suckless (wc -l *.c
gives 548..), but if somebody might find it useful, here [0] it is.

cheers
Lars

[0] https://github.com/e5150/psgck



Re: [dev] [sbase] Problem tar archiving the output file

2013-07-21 Thread Lars Lindqvist
This might lead to false positive if the tar file is written to another device.

$ ~/.sbase/bin/tar cf /mnt/backup/test.tar .config
ignoring '.config/zathura/zathurarc'

$ find .config/zathura/zathurarc /mnt/backup/test.tar -printf %i %D %p\n
1259 2050 .config/zathura/zathurarc
1259 2081 /mnt/backup/test.tar

Attached diff also checks st_dev, which ought to cover all cases.

Regards,
Lars.

On 20/07-13, Roberto E. Vargas Caballero wrote:
 This patch fixes this issue using the inode number of the tar
 file.
 
 
 
 -- 
 Roberto E. Vargas Caballero
 
 k...@shike2.com
 http://www.shike2.com

 From e5b989e7899c87f316597e6b8ed39adfa97959b2 Mon Sep 17 00:00:00 2001
 From: Roberto E. Vargas Caballero k...@shike2.com
 Date: Sat, 20 Jul 2013 18:08:58 +0200
 Subject: [PATCH] Avoid infinite loop in tar
 
 When the tar file is written in one directory archived by tar
 the function archive enters in an infinite loop due to de
 tar file written. This patch avoid this case checking the
 inode of the tar file before of adding it to the archive.
 ---
  tar.c | 10 ++
  1 file changed, 10 insertions(+)
 
 diff --git a/tar.c b/tar.c
 index 87b7236..0f3ce33 100644
 --- a/tar.c
 +++ b/tar.c
 @@ -47,6 +47,7 @@ static void c(char *);
  static void xt(int (*)(char*, int, char[Blksiz]));
  
  static FILE *tarfile;
 +static ino_t tarinode;
  
  static void
  usage(void)
 @@ -117,9 +118,14 @@ main(int argc, char *argv[])
   usage();
  
   if(file) {
 + struct stat st;
 +
   tarfile = fopen(file, (mode == 'c') ? wb : rb);
   if(!tarfile)
   eprintf(tar: open '%s':, file);
 + if (lstat(file, st)  0)
 + eprintf(tar: stat '%s':, file);
 + tarinode = st.st_ino;
   } else {
   tarfile = (mode == 'c') ? stdout : stdin;
   }
 @@ -155,6 +161,10 @@ archive(const char* path, const struct stat* sta, int 
 type)
   mode_t mode;
  
   lstat(path, st);
 + if (st.st_ino == tarinode) {
 + fprintf(stderr, ignoring '%s'\n, path);
 + return 0;
 + }
   pw = getpwuid(st.st_uid);
   gr = getgrgid(st.st_gid);
  
 -- 
 1.8.3.2
 

--- tar-orig.c  2013-07-22 00:29:33.078595937 +0200
+++ tar.c   2013-07-22 00:30:41.786328335 +0200
@@ -48,6 +48,7 @@
 
 static FILE *tarfile;
 static ino_t tarinode;
+static dev_t tardev;
 
 static void
 usage(void)
@@ -126,6 +127,7 @@
if (lstat(file, st)  0)
eprintf(tar: stat '%s':, file);
tarinode = st.st_ino;
+   tardev = st.st_dev;
} else {
tarfile = (mode == 'c') ? stdout : stdin;
}
@@ -161,7 +163,7 @@
mode_t mode;
 
lstat(path, st);
-   if (st.st_ino == tarinode) {
+   if (st.st_ino == tarinode  st.st_dev == tardev) {
fprintf(stderr, ignoring '%s'\n, path);
return 0;
}